在带有winrm的PowerShell脚本中使用ruby变量

问题描述 投票:0回答:1

编辑:我想通过虚拟机管理器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个回答
0
投票

我看到一些导致问题的原因。

  1. #是powershell中的保留字符。当您使用#时,其后的所有内容均为注释。
  2. 您正在将Get-ChildItem的输出分配给$files。 shell.run()将没有输出分配给PSoutput,因为cmdlet的输出已分配给$files
  3. Get-ChildItem是Powershell特定的命令,而不是您可以在不首先调用Powershell可执行文件的情况下在Shell中执行的命令行/ dos命令。 (对此,我有点怀疑,但很确定其正确性。)>
  4. 您可以从红宝石中得到的是,应该起作用,

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

一旦执行该命令,#path_str目录下的文件总数应为。

© www.soinside.com 2019 - 2024. All rights reserved.