【问题标题】:cannot validate argument on parameter "Path" The argument is null or empty无法验证参数“路径”上的参数参数为空或空
【发布时间】:2020-05-22 11:22:54
【问题描述】:

我试图创建一个简单的脚本,该脚本将转到服务器并获取文件夹的 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


    【解决方案1】:

    问题是因为您尝试使用 $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}
    

    【讨论】:

    • 感谢您的回复。我知道这很简单,但我该怎么做呢?
    • 不用担心,我已经添加了两个示例来说明如何实现这一点。
    • 非常感谢!第一个例子解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多