powershell - 无法验证参数“路径”上的参数参数为空或为空
问题描述
我试图创建一个简单的脚本,该脚本将转到服务器并获取文件夹的 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}
它可能非常简单,但我会很感激帮助。
解决方案
问题是因为您尝试使用 $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}
推荐阅读
- java - 从 Android 到 Web 服务器的间歇性超时问题
- ios - 在设备上运行应用程序时查看本地数据库
- solr - 如何使用 ext:solr 索引整个 TYPO3 存储?
- rabbitmq - 如何将开放跟踪跨度注入 RabbitMQ?
- java - 使用 ImageViews 和 switch case 减少冗余代码
- git - Git - 分支之间的差异概述
- java - 我该如何解决线程“main”中的异常 org.hibernate.MappingException:未知实体
- python - python正则表达式匹配有效的c表达式
- php - 通知php
- python - 熊猫将标量值添加到数字列?