【问题标题】:Using ruby variable in a PowerShell script with winrm在带有 winrm 的 PowerShell 脚本中使用 ruby​​ 变量
【发布时间】:2020-05-18 19:49:06
【问题描述】:

编辑: 我想通过虚拟机管理器 PowerShell cmdlet 计算存储容器的文件/文件夹。

我遇到了类似的问题,但仍然在语法上苦苦挣扎。 我有一个在远程服务器上执行 PowerShell 脚本的 ruby​​ 脚本。 我想在 Powershel 脚本中使用 ruby​​ 变量。 例如

path_str = "\\XXX\YYY\" #This is the ruby var

PSoutput = shell.run(" #This part is executing the PS script
            $Files = Get-ChildItem -Recurse -File -Path #{path_str}  | Measure-Object | %{$_.Count}" | stdout

如何在 PS 脚本中使用 ruby​​ 变量 path_str? 我试过了

# {path_str}

\" " + path_str + " \"

双引号和单引号

没有什么对我有用。

【问题讨论】:

标签: ruby powershell powershell-remoting winrm


【解决方案1】:

我发现有几件事会导致问题。

  1. # 是 powershell 中的保留字符。当您使用 # 时,后面的任何内容都是注释。
  2. 您正在将Get-ChildItem 的输出分配给$files。 shell.run() 将没有输出分配给 PSoutput,因为 cmdlet 的输出被分配给 $files
  3. Get-ChildItem 是特定于 powershell 的命令,而不是您可以在 shell 中执行的命令行/ dos 命令,而无需先调用 powershell 可执行文件。 (这个,我有点怀疑,但很确定它是正确的)。

你可以从 ruby​​ 做的是,应该工作,

PSoutput = system("powershell get-childitem -Recurse -File -Path " + #{path_str} + "  | Measure-Object | % {$_.Count}")

命令执行后,#path_str 目录下应该有一个文件总数。

【讨论】:

    猜你喜欢
    • 2021-01-30
    • 2010-10-20
    • 1970-01-01
    • 2022-10-17
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    相关资源
    最近更新 更多