Reputation: 1422
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.
Upvotes: 0
Views: 1988
Reputation: 36332
I would like to suggest an alternative. If you are running this at login, why not run it from the Task Scheduler, and run it as the account that you want to use the credentials for? When creating the task you will want to set a few things.
On the General Tab
On the Triggers Tab
On the Actions Tab
Then just hit OK. It will prompt you for the password for the account that you are trying to run the script as, and Windows will manage it from there.
Upvotes: 1