编辑:我想通过虚拟机管理器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 +“ \”
双引号和单引号
对我没有任何帮助。
我看到一些导致问题的原因。
#
是powershell中的保留字符。当您使用#时,其后的所有内容均为注释。Get-ChildItem
的输出分配给$files
。 shell.run()将没有输出分配给PSoutput,因为cmdlet的输出已分配给$files
。Get-ChildItem
是Powershell特定的命令,而不是您可以在不首先调用Powershell可执行文件的情况下在Shell中执行的命令行/ dos命令。 (对此,我有点怀疑,但很确定其正确性。)>您可以从红宝石中得到的是,应该起作用,
PSoutput = system("powershell get-childitem -Recurse -File -Path " + #{path_str} + " | Measure-Object | % {$_.Count}")
一旦执行该命令,#path_str目录下的文件总数应为。