Reputation: 57
I am new to scripting in general and am trying to tackle the task of writing a PowerShell script to automate accepting RSA keys from PuTTY across some 15,000 servers in my organization. I have the servers saved in a .bat file and when running that it will auto login through PuTTY. The issue is when it logs in A RSA security window will pop up requiring me to hit "y" I have that part and closing PuTTY so the next instance will be loaded, the only issue is I cant get the process to loop. I am looking for some guidance on the issue.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#Variables
$batFile = Start-Process -FilePath "C:\Users\UID\OneDrive - CompanyA\PS Scripts\puttyRSA.bat";
New-Object -ComObject wscript.shell;
#opens the "puttyRSA.bat" file
$batFile
#Loops everything
do{
# Will click "Y"
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('title of the application window')
Start-Sleep -Seconds 3
$wshell.SendKeys('y')
#Waits and closes putty
Start-Sleep -Seconds 3
Stop-Process -name putty
}
While (-FilePath puttyRSA.bat=running)here
Upvotes: 1
Views: 353
Reputation: 21408
Assuming you want to keep running until $batFile
finishes, your while
clause isn't valid PowerShell. You'll have to make two changes here.
First, you'll need to kick off $batFile
with Start-Process
so you can get the PID to wait on:
# -PassThru is required because by default Start-Process doesn't return an object
$processId = ( Start-Process -FilePath $batFile -PassThru ).Id
Then, for your while
clause:
} while ( Get-Process -Id $processId 2>$null )
This will keep your loop running until the process belonging to $batFile
ends. The 2>$null
redirects the error stream to $null
, so it won't display an error when the process can no longer be found.
You can read more on output streams and redirection on my answer here.
Upvotes: 1