【发布时间】:2015-03-29 20:20:04
【问题描述】:
如何将哈希表中收集的参数用于Invoke-Command 上的ArgumentList?
$CopyParams = @{
Source = 'E:\DEPARTMENTS\CBR\SHARE\Target'
Destination = 'E:\DEPARTMENTS\CBR\SHARE\Target 2'
Structure = 'yyyy-MM-dd'
}
Invoke-Command -Credential $Cred -ComputerName 'SERVER' -ScriptBlock ${Function:Copy-FilesHC} -ArgumentList @CopyParams
无论我尝试什么,它总是抱怨“来源”:
Cannot validate argument on parameter 'Source'. The "Test-Path $_" validation script for the argument with
value "System.Collections.Hashtable" did not return true. Determine why the validation script failed
这个blog 谈到了一个类似的问题,但我无法让它工作。
对于Invoke-Command 中的简单Copy-Item 也是如此,例如:
Invoke-Command -Credential $Cred -ComputerName 'SERVER' -ScriptBlock {Copy-Item} -ArgumentList @CopyParams
Invoke-Command : Missing an argument for parameter 'ArgumentList'. Specify a parameter of type 'System.Obj
ect[]' and try again.
At line:11 char:89
+ ... ck {Copy-Item} -ArgumentList @CopyParams
感谢您的帮助。
【问题讨论】:
-
有助于查看
ScriptBlock中的内容 - 假设这就是拉动args[0]并期望它成为哈希的原因?还假设这是错误消息的来源? -
感谢arco444的回复。
ScriptBlock只是一个在[String]Source和[String]Destination上的参数为Test-path的函数。在此之后,它只使用Copy-Item来复制内容。将它们作为-ArgumentList $Source, $Destination传递时它工作正常,但不是飞溅。 -
如果你用我最后一个例子
Copy-Item试试,你会发现它不起作用。甚至没有美元符号。
标签: powershell argument-passing invoke-command