带有空格的Powershell打开文件路径 [英] Powershell opening file path with whitespaces
问题描述
我是我的 PS 脚本,我希望能够通过执行以下操作在另一个 PS 实例中运行另一个脚本:
I'm my PS script I want to be able to run another script in another PS instance by doing the following:
$filepath = Resolve-Path "destruct.ps1"
start-process powershell.exe "$filepath"
destruct.ps1
与此脚本位于同一文件夹中.
destruct.ps1
is in the same folder as this script.
但是,在包含空格的位置 ("C:\My Scripts\"
) 中运行此脚本时,我将收到以下错误:
However when running this script in a location which includes spaces ("C:\My Scripts\"
) I will get the following error:
术语C:\My"未被识别为 cmdlet、函数、可运行程序或脚本文件.验证条款并重试.
The term 'C:\My' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
我知道通过将 '&'
与 Invoke-Expression 方法结合使用可以解决这个问题,但使用 start-process 方法如何才能做到这一点?
I know by using a '&'
with the Invoke-Expression method solves this problem, how can I do the same but by using the start-process method?
推荐答案
试试这个:
start-process -FilePath powershell.exe -ArgumentList "-file `"$filepath`""
评论后
start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path)`""
附注:
$filepath
是 [pathinfo]
类型而不是 [string]
类型.
$filepath
is a [pathinfo]
type and not a [string]
type.
这篇关于带有空格的Powershell打开文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!