0

I am currently writing a PowerShell script to stop a few processes, start a program, and then restart those processes. The problem arises when I feed the processes into an array variable to restart them later.

My code:

$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;
}

When debugging the $processesDefined.Count displays 2 and the $processesDefined displays as expected yet when it gets to tjhe time to start the processes I get:

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

I've done a lot of searching but can't find anything of real help. Any help with this would be greatly appreciated. Thank you.

Output of $processesDefined:

[DBG]: PS D:\Desktop>>> $processesDefined

Path                                                                           
----                                                                           
C:\Program Files\Windows Media Player\wmpnetwk.exe                             
C:\Windows\explorer.exe                                                        



_____________________________________________________________
  • can you put up the contents of `$processedDefined`? I think it might be an issue with the contents of `$processesDefined` – mrwhale Jun 13 '14 at 01:32

1 Answers1

0

Try doing it this way. The way you are doing it leaves the variable $processesDefined with a table heading (PATH) and this will screw things up.

$processesDefined += (get-process -name $i).Path

This way will just give you the path with no table heading

mrwhale
  • 311
  • 2
  • 6