Is there a way to send keystrokes to a specific window using PowerShell ?
Right now I'm opening an IE window.
Then, by calling the link, a download window is opening.
I'm sending an ALT+TAB to switch in the download window, and LEFT+ENTER to confirm the download.
Is there a way to send this LEFT+ENTER to a specific window without having focus on it?
My code looks like that:
$ie=new-object -com internetexplorer.application
$ie.navigate2($url)
$ie.visible=$true
while($ie.busy) {Start-Sleep 1}
start-sleep -seconds 1
[System.Windows.Forms.SendKeys]::SendWait("%{TAB}")
start-sleep -seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{LEFT}")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
I want to send the keystrokes to a specific window to make sure the download window gets the keystrokes even if the focus isn't on it.
I know sending keystrokes isn't the best way to download the file, but I've also tried this:
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.Networkcredential($user, $password)
$webclient.DownloadFile($urlWeb,$path)
I always get an 403 Error and haven't found a solution yet to solve this problem...
I'm not really a fan of SendKeys cause it almost always fail. If I use the provided link, I'm able to download the file with this Powershell code:
$userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"
$target = "http://download.thinkbroadband.com/5MB.zip"
$folder = "C:\TEMP\5MB.zip"
$web = New-Object System.Net.WebClient
$web.Headers.Add("user-agent", $userAgent)
$web.DownloadFile($target, $folder)
You might also need to add network credentials.
Related
I accidently did this command in powershell windows 10 and my mouse buttons are swapped.
rundll32.exe user32.dll,SwapMouseButton
How to fix it?
I was testing this command like in a youtube shorts. But I don't know how to fix it.
SwapMouseButton is a native Windows API function that can be called from PowerShell using P/Invoke.
Add the P/Invoke definition using Add-Type and then call the API function, passing $false to revert to normal mouse button behavior:
# Add the P/Invoke API definition
$api = Add-Type -PassThru -Namespace Win32 -Name Win32SwapMouseButton -MemberDefinition #'
[DllImport("user32.dll")]
public static extern bool SwapMouseButton(bool fSwap);
'#
# Call the API to restore normal mouse button behavior
$api::SwapMouseButton($false)
Likewise, you can pass $true to swap mouse buttons again, without having to resort to the rundll32 hack.
Working code in Windows 10 with IE
$mod_nums = #(19, 22)
$oIE = New-Object -ComObject 'InternetExplorer.Application'
$my_arr = New-Object System.Collections.Generic.List[System.Object]
foreach ($mod_num in $mod_nums) {
if ($null -eq $mod_num) {
break
}
$oIE.Navigate("https://wgmods.net/" + "$mod_num")
for ($i = 0; $i -le 5; $i++) {
do {
Start-Sleep -Milliseconds 100
} until ((-not $oIE.Busy) -and ($oIE.ReadyState -eq $READYSTATE_COMPLETE))
}
$oDocument = $oIE.Document
$oHtmlElement = $oDocument.GetElementsByClassName("ModDetails_hidden--2Rtru")[0]
$a = "$($oHtmlElement.href)"
$my_arr += $a
}
BUT in windows 11 IE deleted and $oIE.Navigate just open new window in Edge. Events, Methods, Properties dont work. Trying set IE mode in Edge - nothing changes.
Invoke-WebRequest is not suitable bec wgmods.net checks browser
Interested in any ideas about this. tnx!
IE is discontinued in Windows 11 and it's not expected that the old COM objects will continue to function. Per Internet Explorer mode and the DevTools:
If you have an existing application that uses the InternetExplorer object to automate Internet Explorer 11, but the Internet Explorer 11 desktop application isn't available, your application won't work. Internet Explorer 11 will be retired on June 15, 2022.
Despite the date however, Windows 11 does not have IE baked in so IE-specific automation won't work. The solution as mentioned in the same document above is to use the WebBrowser control to drive browser automation, which looks to include the IE mode use case:
Applications that require IE mode for the website (or app) content to function correctly should use the WebBrowser control. The WebBrowser control uses the Internet Explorer platform (MSHTML/Trident) to render web content, and will work even if the Internet Explorer 11 desktop application isn't available.
The good news is you have a few months until Windows 11 is released to get your automation updated, plus however long it takes for you to start using it in your environment.
I am using PowerShell to copy sensitive information to the Windows clipboard.
Since Windows 10 Vs. 1809 we have the enhanced clipboard with a history function. I don't want to have my sensitive information in the history.
The Set-Clipboard cmdlet doesn't have any helpful parameters. Even in C# I doesn't seem to have an easy way to do this.
It turns out you can access the Universal Windows Platform (UWP) APIs directly in PowerShell:
Function Set-ClipboardWithoutHistory([string]$Value)
{
[int]$RequestedOperationCopy = 1
$null = [Windows.ApplicationModel.DataTransfer.DataPackage,Windows.ApplicationModel.DataTransfer,ContentType=WindowsRuntime]
$null = [Windows.ApplicationModel.DataTransfer.ClipboardContentOptions,Windows.ApplicationModel.DataTransfer,ContentType=WindowsRuntime]
$null = [Windows.ApplicationModel.DataTransfer.Clipboard,Windows.ApplicationModel.DataTransfer,ContentType=WindowsRuntime]
$dataPackage = [Windows.ApplicationModel.DataTransfer.DataPackage]::new()
$cOptions = [Windows.ApplicationModel.DataTransfer.ClipboardContentOptions]::new()
$cOptions.IsAllowedInHistory = $false
$cOptions.IsRoamable = $false
$dataPackage.RequestedOperation = $RequestedOperationCopy
$dataPackage.SetText($Value)
[Windows.ApplicationModel.DataTransfer.Clipboard]::SetContentWithOptions($dataPackage, $cOptions) | Out-Null
}
As of December 2019, PowerShell is still not able to naively prevent adding a new clipboard item to the clipboard history.
The APIs to do this are available for the Universal Windows Platform (UWP).
During 2019 Microsoft made it much easier to access certain APIs in UWP from Windows Desktop Apps, and as it turns out from PowerShell.
So I wrote a small .Net assembly that I can use in PowerShell to prevent the usage of the history feature when copying text to the Windows clipboard.
The trick is to use:
Microsoft.Windows.SDK.Contracts
to access the UWP APIs, then use:
using uwp = Windows.ApplicationModel.DataTransfer;
uwp.DataPackage dataPackage = new uwp.DataPackage { RequestedOperation = uwp.DataPackageOperation.Copy };
dataPackage.SetText("text to copy");
uwp.Clipboard.SetContentWithOptions(dataPackage, new uwp.ClipboardContentOptions() { IsAllowedInHistory = false, IsRoamable = false });
uwp.Clipboard.Flush();
I am trying to use AutoHotkey (v1.1.15.02) to automatically use one of my search engines in Firefox (31.0).
I thought the best way to call the search engine is to use my predefined key words. Usually, if I type "d awesome", Firefox will open "http://dictionary.reference.com/browse/awesome" for me.
Now I just want to get the same behaviour when I call Firefox from AutoHotkey. I thought it should work with this code:
#รค::
send ^c
IfWinExist, ahk_class MozillaWindowClass
{ WinActivate
Send ^t{Space}
SetKeyDelay 100,100
sleep 500
Send d ^v{Enter}
}
else
{
Run firefox
sleep 2500
Send {F6}d ^v{Enter}
}
return
It does almost everything it should, except firefox keeps using google to search "d awesome" instead of calling my predefined search engine. Any ideas on how Firefox can recognize the key?
In the end I realized what I was trying to do is a lot faster if I just call the search directly as an URL in firefox. I'm posting my solution in the hope that other autohotkey users may find it beneficial:
#^e::
send ^c
Clipwait
sleep 200
Run C:\Program Files (x86)\Mozilla Firefox\firefox.exe "http://dictionary.reference.com/browse/%ClipBoard%"
return
It is important for performance to call firefox with the full path!
I know a little bit about programming. I wanted to know if there was a way to get a message box pop up every time someone plugs in a usb drive saying something like "is this an approved device?" . I was wondering if there was a way to insert this in a registry entry or something? Or maybe you have an idea on how to do this.
You can detect USB device inserts using the Win32_DeviceChangeEvent WMI event. There are other ways, like WM_DEVICECHANGE, but PowerShell already knowns how to handle WMI Events.
$query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"
Register-WMIEvent -Query $query -Action { Write-Host "A device has been inserted"}
Source: here and here
Showing GUI messages could be done using WPF or WinForms.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show('Message', 'Title')
Source: here and here