Powershell script to send keys alt tab - powershell

I have an .exe file that I'm running using Start-Process. Once my application opens, I want to send first tab key and then do alt + tab.
I'm using following commands, but so far only my application is launched.
Start-Process -FilePath $Exe -WorkingDirectory $Path
Start-Sleep 10
[System.Windows.Forms.SendKeys]::SendWait(“{TAB}”)
Start-Sleep 2
[System.Windows.Forms.SendKeys]::SendWait(“%{TAB}”)

Use this:
Start-Process -FilePath $Exe -WorkingDirectory $Path
$wsh = New-Object -ComObject Wscript.Shell
$wsh.AppActivate("Window title of $exe")
Start-Sleep 10
$wsh.SendKeys("{TAB}")
Start-Sleep 2
$wsh.Sendkeys("%{TAB}")
You need to activate the exe file's window with its window title. Also prefer Wscript.Shell to send keys. Replace "Window title of $exe" with its window title.

Related

Trying to loop a set of commands in PowerShell

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
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.

PowerShell open Internet Explorer and connect to Website with Credentials

Is it possible to create a PowerShell Script that opens Internet Explorer, and connect to an internal IIS Website with credentials?
First part is easy:
$ie = New-Object -Com InternetExplorer.Application
$ie.visible = $true
$ie.navigate("http://mysite")
Now I have no idea how to solve the second part. IE starts and security popup comes up.
Anyone an idea how to handover the credentials? Thanks in advance.
PS: It's an internal IIS Server. I can choose from Basic or Windows Authentication.
If integrated Auth is helpful, how about using Start-Process with the -Credential parameter.
Start-Process -FilePath "C:\Program Files (x86)\Internet Explorer\iexplore.exe" -ArgumentList "http://mysite" -Credential $YourCreds
Or you wrap that to get the correct folder if this will be running on different computers.
if ($arch = $env:PROCESSOR_ARCHITECTURE -eq 'AMD64') {
Start-Process -FilePath "C:\Program Files (x86)\Internet Explorer\iexplore.exe" -ArgumentList "http://mysite" -Credential $YourCreds
}
else {
Start-Process -FilePath "C:\Program Files\Internet Explorer\iexplore.exe" -ArgumentList "http://mysite" -Credential $YourCreds
}

How to start and stop processes in PowerShell?

This should work fine in PowerShell older than 3. I need to run two processes: wuapp.exe and desk.cpl with ScreenSaver's tab. The problem I have is that once I start wuapp.exe the process name shows up in Task Manager as explorer.exe - the current code works for wuapp.exe but not on every machine. The other thing is when I kill rundll32 other apps will close also. What do you suggest?
$wshell = New-Object -ComObject Wscript.Shell
Start-Process wuapp.exe
(New-Object -comObject Shell.Application).Windows() | foreach-object {$_.quit()}
Start-Process rundll32 desk.cpl,InstallScreenSaver
Stop-Process -ProcessName rundll32* -Force
I'm not entirely sure what you mean by wuapp.exe showing up as explorer.exe, but keeping track of the rundll32 process is fairly straightforward.
Use Start-Process with the PassThru parameter to have it return the process it creates:
$ScreenSaverCpl = Start-Process rundll32 desk.cpl,InstallScreenSaver -PassThru
# Call Stop-Process when no longer needed:
Stop-Process $ScreenSaverCpl

powershell open url in a hidden window except IE

I would like to run a power shell command like Start-Process with browsers except IE to open in a hidden window. Is there any way to set size of browser to make it hidden when we run the powershell command.
Like; Start-Process -SomeCommandToOpenChromeHidden "chrome.exe" "www.google.com"
Any idea?
Thanks
RESOLVED:
I updated my IE to 11 then now it is fine with following command:
$ie = new-object -comobject InternetExplorer.Application $ie.visible = $false
There is -WindowStyle Parameter. Values can be: Normal, Hidden, Minimized, and Maximized. The default value is Normal.
So you need to use:
Start-Process -WindowStyle Hidden "chrome.exe" "www.google.com"

PowerShell - How to use 'Start-Process' and rename the newly launched windowtitle

I am trying to use the PowerShell (version 2) Start-Process and rename the newly launched window title. I have the following snippet of code that works fine, it launches a new PowerShell window and tails logs ($c contains the log file to tail):-
Start-Process powershell.exe -Argument "Get-Content", $c, "-wait"
I am not sure how to include the following so that the newly launched window title can be changed.
$host.ui.RawUI.WindowTitle = 'New window title rename example text'
Cheers.
A "dirty" solution would be:
start-process powershell.exe -argument "`$host.ui.RawUI.WindowTitle = 'New window title rename example text'; get-content -Path $c -wait"
I would recommend creating a script for you commands and use parameters for input.
Untitled2.ps1
param($c)
$host.ui.RawUI.WindowTitle = 'New window title rename example text'
Get-Content -Path $c -Wait
Script
$script = Get-Item ".\Desktop\Untitled2.ps1"
$c = Get-Item ".\Desktop\t.txt"
Start-Process powershell.exe -ArgumentList "-File $($script.FullName) -c $($c.FullName)"
A cleaner powershell solution, especially if you're going to run further commands at the time you spawn the process, might be like this.
$StartInfo = new-object System.Diagnostics.ProcessStartInfo
$StartInfo.FileName = "$pshome\powershell.exe"
$StartInfo.Arguments = "-NoExit -Command `$Host.UI.RawUI.WindowTitle=`'Your Title Here`'"
[System.Diagnostics.Process]::Start($StartInfo)
if anyone need a easy way ,you can try this in powershell:
cmd.exe /c "start ""my app"" powershell.exe -NoExit -Command ""dotnet myapp"""
it also means this command in cmd:
start "myapp" powershell.exe -NoExit -Command "dotnet myapp"