首页 > 解决方案 > 无法验证参数“路径”上的参数参数为空或为空

问题描述

我试图创建一个简单的脚本,该脚本将转到服务器并获取文件夹的 acl 详细信息。我测试了命令:

Invoke-command -Computername Servername -ScriptBlock {(Get-Acl "\\Server\Folder\user folders").access | ft- auto}

这工作正常。但是,当我试图将它放入允许我通过变量输入路径的脚本中时,我总是得到:

Cannot validate argument on parameter 'Path'. The argument is null or empty. Supply an argument that is not null or empty and then 
try the command again.
    + CategoryInfo          : InvalidData: (:) [Get-Acl], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetAclCommand

这是我的脚本:

#get folder permissions from remote computer
$serverName = read-host "Please enter the name of the target server"
$folderPath = "\\server_name\Folder\user folders"
#read-host "Please enter the full path to the target folder"
Invoke-command -ComputerName $serverName -ScriptBlock {(get-acl $folderPath).access | ft -wrap} 

它可能非常简单,但我会很感激帮助。

标签: powershell

解决方案


问题是因为您尝试使用 $folderPath 变量,但在远程计算机上该变量不存在。

您需要将其作为参数传递。有多种方法可以做到这一点,以下两种方法:

# Add desired variable to ArgumentList and define it as a parameter
Invoke-command -ComputerName $serverName -ArgumentList $folderPath -ScriptBlock {
  param($folderPath)  
  (get-acl $folderPath).access | ft -wrap
}

或者

# In PS ver >= 3.0 we can use 'using'
Invoke-command -ComputerName $serverName $folderPath -ScriptBlock {(get-acl $using:folderPath).access | ft -wrap}

推荐阅读