PowerShell:使用从Get进程解析的变量启动进程
我目前正在编写一个PowerShell脚本来停止几个进程,启动一个程序,然后重新启动这些进程。当我将进程馈送到数组变量中以便稍后重新启动它们时,问题就出现了 我的代码:PowerShell:使用从Get进程解析的变量启动进程,powershell,powershell-2.0,powershell-ise,Powershell,Powershell 2.0,Powershell Ise,我目前正在编写一个PowerShell脚本来停止几个进程,启动一个程序,然后重新启动这些进程。当我将进程馈送到数组变量中以便稍后重新启动它们时,问题就出现了 我的代码: $direc = "C:\Program Files (x86)\PathTo\Program.exe"; $arguments = "/theseArguments1", "/theseArguments2"; $processesDefined = @(); $processes = "notepad", "expl
$direc = "C:\Program Files (x86)\PathTo\Program.exe";
$arguments = "/theseArguments1", "/theseArguments2";
$processesDefined = @();
$processes = "notepad", "explorer";
ForEach ($i in $processes)
{
$processesDefined += get-process -name $i | select-object path;
get-process -name $i | stop-process -force;
}
start-process -filepath $direc -ArgumentList $arguments -NoNewWindow -wait;
ForEach ($i in $processesDefined)
{
start-process -filepath $i;
}
调试时,$processesDefined.Count显示2,$processesDefined按预期显示,但到了启动我得到的进程的时间:
Start-Process : This command cannot be executed due to the error: The system ca
nnot find the file specified.
At D:\Desktop\Aion.ps1:17 char:18
+ start-process <<<< -FilePath $i;
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOp
erationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.C
ommands.StartProcessCommand
试着这样做。这样做会让变量$processes定义一个表标题路径,这会把事情搞砸
$processesDefined += (get-process -name $i).Path
这种方式只会提供一个没有表标题的路径您可以将$processedDefined的内容放进去吗?我想这可能是$processes定义的内容有问题。非常感谢。从BASH切换到PowerShell是一个相当大的飞跃。
$processesDefined += (get-process -name $i).Path