Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Parsing PowerShell:使用$ExecutionContext.InvokeCommand.ExpandString($var)的双引号_Parsing_Variables_Powershell - Fatal编程技术网

Parsing PowerShell:使用$ExecutionContext.InvokeCommand.ExpandString($var)的双引号

Parsing PowerShell:使用$ExecutionContext.InvokeCommand.ExpandString($var)的双引号,parsing,variables,powershell,Parsing,Variables,Powershell,好的,我已经编写了一个在Windows8中完美运行的脚本。我切换到Windows7,采集各种血液。经过一些调试,我发现了问题。Win 8使用PowerShell 3.0,Win 7使用PowerShell 2.0,PowerShell 3.0允许您在使用以下各项时使用双引号: $ExecutionContext.InvokeCommand.ExpandString($var) 由于我的代码严重依赖于此,许多字符串都使用双引号,因此我试图找到一个解决方案 此处字符串不起作用,因为我的一些字符串如

好的,我已经编写了一个在Windows8中完美运行的脚本。我切换到Windows7,采集各种血液。经过一些调试,我发现了问题。Win 8使用PowerShell 3.0,Win 7使用PowerShell 2.0,PowerShell 3.0允许您在使用以下各项时使用双引号:

$ExecutionContext.InvokeCommand.ExpandString($var)
由于我的代码严重依赖于此,许多字符串都使用双引号,因此我试图找到一个解决方案

此处字符串不起作用,因为我的一些字符串如下所示:

$var = 'this is a "sample" of $one of my strings'
here字符串解决方法仅在输出到文本文件时有效,我不希望这样,因为我在函数中大量使用此代码


我在微软的网站上发现了一个与此相关的bug。它在PowerShell 3.0中的工作方式就是它最初的工作方式。此外,将Windows 7升级到PowerShell 3.0是不可能的。

您可以测试低于3的PowerShell版本,然后转义任何双引号,如下所示:

C:\PS> if ($PSVersionTable.PSVersion.Major -lt 3) { $var = $var -replace '"','`"' }

C:\PS> $ExecutionContext.InvokeCommand.ExpandString($var)
this is a "sample" of one baby of my strings

把$var用双引号括起来怎么样?

和往常一样,基思,你的解决方案非常出色。非常感谢,这正是我所需要的。我刚刚升级了我的Powershell版本,现在运行的是带有Powershell 3的windows 7,字符串已正确展开。