当路径为变量时,Get-ChildItem发生错误
这有效:
PS D:\DMH> Get-ChildItem -Path '\\MHRZRSEFS501\F$\NewSkies FileShares\FRBatch$\Test\Test.txt'
Directory: \\MHRZRSEFS501\F$\NewSkies FileShares\FRBatch$\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 5/19/2019 2:45 AM 18 Test.txt
但是,如果我将Path放入变量中,它将不起作用:
PS D:\DMH> $SPath = "'\\MHRZRSEFS501\F$\NewSkies FileShares\FRBatch$\Test\Test.txt'"
Get-ChildItem -Path $SPath
Write-Host "SPath =$SPath"
Get-ChildItem : Cannot find path 'D:\DMH\'\MHRZRSEFS501\F$\NewSkies FileShares\FRBatch$\Test\Test.txt'' because it does not exist.
***
SPath ='\\MHRZRSEFS501\F$\NewSkies FileShares\FRBatch$\Test\Test.txt'
D:\DMH\
显然是我从中启动PowerShell
的目录,但是它是如何进入命令路径的?
答案 0 :(得分:1)
双引号要求PowerShell评估字符串。试试这个(不加双引号):
$SPath = '\\MHRZRSEFS501\F$\NewSkies FileShares\FRBatch$\Test\Test.txt'