我在win10 10-64位下使用powershell。我在-path参数中的"Set-location“函数下输入了一个路径。我的path变量是来自System.Array的一个属性,它是"$list.name“。在循环中,我递归地调用".\$list.name$i“作为路径,但失败了。我想正确地调用嵌套变量。
PS C:\Users\admin\Documents\Rainmeter\Skins> $list
label name link sort
----- ---- ---- ----
1 chrome C:\Program Files (x86)\Google\Chrome\Application\chrome.exe 1
1 matlab C:\Program Files\MATLAB\R2019a\bin\matlab.exe 3
PS C:\Users\admin\Documents\Rainmeter\Skins> $i=0PS C:\Users\admin\Documents\Rainmeter\Skins> Set-Location -Path .\$list.name[$i]
Set-Location : could not find path “.\ .name[0]”,the path is not existed.
+ Set-Location -Path .\$list.name[0]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\ .name[0]:String) [Set-Location], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommandSet-Location -Path ".\$list.name[$i]"$file = $list.name[$i]
Set-Location -Path $file我想正确地调用循环下的-path参数中的嵌套变量,并知道错误的原因。
谢谢你的帮助。
发布于 2019-05-13 08:51:56
尝试将其包装在)中。在替换字符串之前,PowerShell将在括号内执行。例如:
Set-Location -Path ".\$($list.name[$i])"发布于 2019-05-13 09:22:55
不能将位置设置为文件,请使用Split-Path获取链接的父级,而不是名称。
Set-Location -Path (Split-Path -Path $list.link[$i] -Parent)https://stackoverflow.com/questions/56108740
复制相似问题