PS 调用命令和 writeallbyes
PS invoke-command and writeallbyes
所以..如主题中所述 - 我正在使用 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
作为参数发送到远程系统。
我会使用 using
来访问变量的内容,例如:
[System.IO.File]::WriteAllBytes($using:FileTo, $Contents)
所以..如主题中所述 - 我正在使用 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
作为参数发送到远程系统。
我会使用 using
来访问变量的内容,例如:
[System.IO.File]::WriteAllBytes($using:FileTo, $Contents)