文件夹路径中带有GUID的Get-ChildItem会抛出ParserError

时间:2016-02-24 12:22:31

标签: powershell powershell-v2.0 powershell-v4.0

我在计算机 vm_remote 上使用Powershell 2 .0。在这里我运行命令

$output = "H:\scripts\" + (get-Date -uformat %Y%m%d) + "output_files.txt"
(get-childitem G:\dum\didum\{12481b3f-12e2-4b55-a5d5-84d3b702036b} -include *.*).count >> $output

这会导致错误

Bad numeric constant: 30. At H:\scripts\count_files.ps1:4 char:40
+ (get-childitem G:\dum\didum\{30 <<<< f81b3f-12e2-4b55-a5d5-84d3b702036b} -include *.*).count >> $output
    + CategoryInfo          : ParserError: (30:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : BadNumericConstant

使用`或“”来转义GUID的字符不起作用。

运行命令

Invoke-Command -Computername vm_remote -scriptblock { get-childitem "G:\dum\didum\{12481b3f-12e2-4b55-a5d5-84d3b702036b}" -recurse -include *.* -exclude *.latest }

来自另一台使用远程访问的计算机(使用Powershell 4 .0)我得到以下内容:

Bad numeric constant: 30.
    + CategoryInfo          : ParserError: (30:String) [], ParseException
    + FullyQualifiedErrorId : BadNumericConstant
    + PSComputerName        : vm_remote

注意:子文件夹也包含GUID。

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

Curly大括号在PowerShell中具有特殊含义。我认为正在发生的事情是它将GUID视为INFO: Requested resource [/VAADIN/widgetsets/pl.lajtovo.myproj.MyAppWidgetset/pl.app.myproj.MyAppWidgetset.nocache.js] not found from filesystem or through class loader. Add widgetset and/or theme JAR to your classpath or add files to WebContent/VAADIN folder. 并尝试将其作为代码执行。  您应该引用该路径字符串或将其放在字符串变量中以避免此问题。我在2.0中得到了相同的问题,在4.0中这样做我没有输出。在两种情况下引用字符串似乎都解决了这个问题。

scriptblock

虽然不确定第二个例子。只要引用路径,它对我来说就可以正常工作。