【发布时间】:2018-12-04 20:29:10
【问题描述】:
所以.. 如主题 - 我正在使用 Invoke-Command 将文件复制到远程位置。
不用多说,直接上代码:
$Contents = [System.IO.File]::ReadAllBytes("$workingDir\$FileToCopy")
[string]$FileTo = "C:\$installer"
$result = Invoke-Command -Computername $servername -Credential $Credentials -Scriptblock {
param(
[byte[]]
$Contents
)
[System.IO.File]::WriteAllBytes($FileTo, $Contents)
return $?
} -ArgumentList @( ,$Contents )
$result
问题是,如果我将 $FileTo 替换为手动输入,如下所示:
[System.IO.File]::WriteAllBytes("C:\MyFileToCopy.txt", $Contents)
一切都很好,否则当我将文件的路径放入字符串变量时 - 我收到以下错误:
Exception calling "WriteAllBytes" with "2" argument(s): "Empty path name is not legal."
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException
为什么?
【问题讨论】:
-
您没有将
$FileTo传递到脚本块中。
标签: powershell powershell-remoting