【问题标题】:PS invoke-command and writeallbyes [duplicate]PS调用命令和writeallbyes [重复]
【发布时间】: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


【解决方案1】:

您没有将$FileTo 作为参数发送到远程系统。

我会使用using 来访问变量的内容,例如:

[System.IO.File]::WriteAllBytes($using:FileTo, $Contents)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    相关资源
    最近更新 更多