There are 2 PowerShell scripts that run at startup, 1 is in my control and the other is out of my control.
Both of our scripts use Start-Process. Strange issue is if both of our Start-Process's happen to execute at the same time, I get the following error from my script:
Start-Process : This command cannot be run due to the error: An instance of the service is already running.
Is there a way to see if another "Start-Process" is already running? I know about "Get-Process" but only shows PowerShell running, not which cmdlet is currently running.
I'm not in favor of NOT using Start-Process.
As a temporary workaround, I've told my script to sleep for 60 seconds, but would rather KNOW when the other Start-Process is done.
Here is my code:
$pw = ConvertTo-SecureString -AsPlainText -Force -String "myPassword"
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "Administrator",$pw
$psArgList = "-ExecutionPolicy Bypass","-file"
$psArgList += $args
Start-Process powershell.exe -Wait -Credential $cred -ArgumentList $psArgList
This allows me to run another PowerShell script with full privileges: as Admin with ExecutionPolicy as Bypass.