Search Results for 'sendkeys'

Welcome Forums Search Search Results for 'sendkeys'

Viewing 15 results - 31 through 45 (of 52 total)
  • Author
    Search Results
  • #70735
    Keymaster
    Topics: 18
    Replies: 4872
    Points: 1,903
    Helping HandTeam Member
    Rank: Community Hero

    So, because I know there’s no PowerShell command for this, I searched “.NET record microphone”. You might try that – there are some good examples. You’re going to need to translate from C# or VB, because you’re basically going to be coding .NET.

    FWIW, I also searched “.net set volume” and got some good results, which might give you something more reliable than that SendKeys approach you’re using. That’s almost 20 years old and isn’t going to be reliable across a lot of different systems.

    Del
    Participant
    Topics: 22
    Replies: 19

    I’m working on a script, to go and unmute the speaks on a local or remote computer, send a message using [Speech] Class
    I’ll show you the code below. But I was wondering if it’s possible to use PowerShell to capture a (Record) Microphone input ??

    # Set Speaker function
    Function Set-Speaker($Volume) {
    $wshShell = new-object -com wscript.shell;1..50 | % {$wshShell.SendKeys([char]174)};1..$Volume | %{$wshShell.SendKeys([char]175)}}
    Set-Speaker -Volume 50
    #
    Invoke-Command -ComputerName $PC -scriptblock {

    Add-Type -AssemblyName System.speech
    $talker = New-Object System.Speech.Synthesis.SpeechSynthesizer
    $talker.speak(“Hi there .. Your your password expires today. Please change your password now”)
    }

    #67303
    Ian Hicks
    Participant
    Topics: 1
    Replies: 0

    I’ve been working on this script and it works on my PC but I am positive it can be broken if the timing is changed slightly.

    The first problem is I can’t figure out how to sendkeys to the program in order to make it start running and simultaneously keep the program from closing via the /w or | out-null commands. When I try to add the out-null lines the script will still close the app before it is done running.

    The 2nd issue is that I have inserted pauses in order to navigate the apps via sendkey commands but obviously this is gonna cause a problem if the app run speed varies even slightly.

    So yeah….. I am sure there are other problems that will arise but these are the two I can see right now. Any advice would be greatly appreciated. Thank you.

    & start “C:\program.lnk”
    sleep 1
    $wshell = New-Object -ComObject wscript.shell;
    $wshell.AppActivate(‘C:\program.lnk’)
    Sleep 1
    $wshell.SendKeys(‘{right}’)
    Sleep 1
    $wshell.SendKeys(‘~’)
    Sleep 1
    $wshell.SendKeys(‘{right}’)
    Sleep 1
    $wshell.SendKeys(‘~’)
    sleep 5
    get-process program | %{ $_.closemainwindow() }
    & start “C:\program.lnk”
    Sleep 3
    $wshell.SendKeys(‘~’)
    sleep 5
    get-process program | %{ $_.closemainwindow() }
    $wshell.SendKeys(‘{right}’)
    Sleep 3
    $wshell.SendKeys(‘~’)
    & start “C:\program.lnk”
    sleep 3
    $wshell = New-Object -ComObject wscript.shell;
    $wshell.AppActivate(‘C:\program.lnk’)
    sleep 1
    $wshell = New-Object -ComObject wscript.shell;
    $wshell.SendKeys(‘{tab}’)
    sleep 1
    $wshell.SendKeys(‘{tab}’)
    sleep 1
    $wshell.SendKeys(‘{tab}’)
    sleep 1
    $wshell.SendKeys(‘{tab}’)
    sleep 1
    $wshell.SendKeys(‘~’)
    sleep 1
    $wshell.SendKeys(‘~’)
    sleep 200
    get-process program | %{ $_.closemainwindow() }
    sleep 2
    & start “C:\program.lnk”
    $wshell = New-Object -ComObject wscript.shell;
    sleep 10
    $wshell.SendKeys(‘~’)
    sleep 10
    $wshell.SendKeys(‘~’)

    Anonymous
    Inactive
    Topics: 4
    Replies: 0

    I am using System.Windows.Forms and New-Object to have a pop up GUI from my PowerShell script. The pop up often appears behind the PowerShell ISE window. It is hidden this way. Is there a trick to make it always come to the front? I was thinking of using SendKeys. But I want other users to use it. There is no telling where they might click. I might be able to do something with SendKeys. But I think someone else has encountered this problem before.

    #59148
    Participant
    Topics: 0
    Replies: 61
    Points: -11
    Rank: Member

    Hi

    I’m not any means expert on SendKeys so I don’t comment on that, but I would recommend to save and get the password otherwise. Seems that the password is now in plain text in that .txt file.

    Also if you want to “save some time” move the $executable inside the if so it would only be executed when needed. Also I would change all “Outlook” text to $process so when you change $process you don’t need to type those into the subject and body etc.

    One approach to this could be also Try Catch Finally, where you could try if it is running then do what you need to do and in Catch you’ll do what you’ll need to do when it is not running and if needed add Finally where you could always send mail/make log files what has happened. Of course if mail send is only wanted when it is not running then add it to Catch and skip Finally, or make logging there has it been running or not.

    Jake

    #59107
    Participant
    Topics: 1
    Replies: 1
    Points: 0
    Rank: Member

    Alright, well this is to check for a process, if its not running, start it, and in one case, automatically log into the software. I’ve managed to cobble this together by googling and its my first effort at doing anything at all in PowerShell. On the plus side, it works exactly as I want it, I was just wondering if there are any possible undesirable effects this might have.

    You’ll have to forgive the mass of sendkeys, there was no other way of getting it to run the program via a launcher. If I ran the executable directly it would start but be unresponsive to input so I was forced to navigate the launcher and start the software from there.

    $process = "TheProgram"
    $check = Get-Process $process
    
    if(!($check)) {
    
    add-type -AssemblyName System.Windows.Forms
    
    $encrypted = get-content C:\cred.txt
    $user = "UserName"
    $Password = ConvertTo-SecureString -string $encrypted
    $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $Password
    ConvertFrom-SecureString $Password
    $NonSecure = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
    
    #Unfortunately I didn't know of a way to make powershell interact directly with the software, the provider "doesnt support powershell" and wouldnt help me
    #So the below is a set of keystrokes with as little time as possible between them to allow for windows to load and such
    
    Invoke-Item "C:\Program Files\TheFile\WithTheProgram\LauncherExecutable.exe"
    start-sleep -Seconds 2
    
    [System.Windows.Forms.SendKeys]::SendWait(“{Right}{Right}”)
    Start-Sleep -Seconds 1
    
    [System.Windows.Forms.SendKeys]::SendWait(“(+{F10})”)
    Start-Sleep -Seconds 1
    
    [System.Windows.Forms.SendKeys]::SendWait(“{R}{Enter}”)
    Start-Sleep -Seconds 8
    
    [System.Windows.Forms.SendKeys]::SendWait(“$user{Tab}$NonSecure{Enter}”)
    Start-Sleep -Seconds 10
    
    [System.Windows.Forms.SendKeys]::SendWait(“{Enter}”)
    
    #With an email message to the IT Dept stating that the program needed relaunching
    
    Send-MailMessage -From "The Automated PC" -To "IT Dept" -Subject "TheProgram has been restarted" -Body "TheProgram was not running on MachineName and has been restarted." -SMTP "mail.com"
    }
    
    #Then more of the same with outlook
    #Although the $check variable is identical to above the script broke if it wasn't redefined here for the next section though I have no idea why
    
    $process = "outlook"
    $check = Get-Process $process
    $executable = "C:\Program Files (x86)\Microsoft Office\Office14\outlook.exe"
    
    if(!($check)) {
    Start-Process $executable
    
    Send-MailMessage  -From "The Automated PC" -To "IT Dept" -Subject "Outlook was restarted" -Body "Outlook was not running on MachineName and has been restarted." -SMTP "mail.com"
    }
    #57233
    Participant
    Topics: 2
    Replies: 4
    Points: 0
    Rank: Member

    Ok, I found a work around. The best way for me to paste at the cursor location is to send the CTRL+v keys directly. Here is a MWE. Thank you again for your help!

    # Load System.Windows.Forms to use SendKeys
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    
    # Copy some text to the clipboard
    "Testing again: 1, 2, 3..." | clip.exe
    
    # Delay for 2 seconds while moving the cursor
    Start-Sleep -s 2
    
    # Paste using CTRL+v 3 times just for verification
    [System.Windows.Forms.SendKeys]::SendWait("^{v}") 
    [System.Windows.Forms.SendKeys]::SendWait("^{v}") 
    [System.Windows.Forms.SendKeys]::SendWait("^{v}") 
    
    # Finish up with the command window
    Write-Host "Done!"
    Read-Host "Press enter to close this window..."
    
    Keymaster
    Topics: 18
    Replies: 4872
    Points: 1,903
    Helping HandTeam Member
    Rank: Community Hero

    Have a look at SendKeys in .NET. There’ve also been several posts to that effect here… https://powershell.org/forums/search/sendkeys/

    #41920
    Keymaster
    Topics: 18
    Replies: 4872
    Points: 1,903
    Helping HandTeam Member
    Rank: Community Hero

    Apologies – very few of us seem to be working on GUI automation using PowerShell. SendKeys is actually part of the .NET Framework’s Visual Basic libraries, not PowerShell per se. Unfortunately, PowerShell isn’t really designed with GUI automation in mind, so I’m not sure we’re going to be a lot of help to you.

    Amar Helloween
    Participant
    Topics: 23
    Replies: 17

    Hi All,

    I am using Send keys in powershell . For example [System.Windows.Forms.SendKeys]::SendWait(“%{TAB}”) I am using it in my script as “ALT+TAB” – Switch screen.

    Now the requirement is there is a text box in a webpage. I want to send Numeric values using Send keys in powershell to that text box in webpage. Please guide me on this.

    Keymaster
    Topics: 18
    Replies: 4872
    Points: 1,903
    Helping HandTeam Member
    Rank: Community Hero

    PowerShell doesn’t have an equivalent to SendKeys, and isn’t really designed for GUI automation. PowerShell also isn’t a web browser; sites that are intended only for interaction with a web browser aren’t going to be easy to automate.

    You’ll need to learn a bit about HTTP (which is beyond what this forum is about). It’s possible that the web page you’re attempting to access is sending an HTTP auth request. The browser handles that by displaying the dialog; in PowerShell, you could just send the HTTP auth response instead of using a dialog. But I don’t want to imply that this is simple; you’re basically re-building part of what a web browser is and does. It’s potentially complex, and PowerShell might not be the best language for it.

    #32639
    Participant
    Topics: 1
    Replies: 302
    Points: 148
    Helping Hand
    Rank: Participant

    I was having a think about this and figured that you could probably ‘hit’ F12 with SendKeys. I had a problem changing the focus of the Window but this stackoverflow post helped there. Does this help at all?

    Add-Type @"
      using System;
      using System.Runtime.InteropServices;
      public class Tricks {
         [DllImport("user32.dll")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool SetForegroundWindow(IntPtr hWnd);
      }
    "@
    
    $ie = Start-Process iexplore.exe -PassThru | Get-Process
    
    [void][Tricks]::SetForeGroundWindow($ie.MainWindowHandle)
        
        Start-Sleep -Seconds 2
    
    [System.Windows.Forms.SendKeys]::SendWait("{F12}")
    
    #28650
    Participant
    Topics: 12
    Replies: 1645
    Points: 2,674
    Helping Hand
    Rank: Community Hero

    If you are using SendKeys as a solution, it is most likely the wrong solution. In vbScript forums, I used to continually talk folks out of using it because there is no way to ensure the script is doing what it’s supposed to. You’re sending commands blindly to the screen and assuming that the Window you expect opened and is active when you are sending commands.

    With that said, there is a programmatic way to manage hiding windows updates. Take a look at this article: http://www.powershellmagazine.com/2014/03/19/how-to-view-and-restore-hidden-windows-updates-with-powershell/

    I used to create the below that hides the annoying Windows 7 language packs in my labs:

    Function Get-WindowsUpdate {
     
        [Cmdletbinding()]
        Param()
     
        Process {
            try {
                Write-Verbose "Getting Windows Update"
                $Session = New-Object -ComObject Microsoft.Update.Session            
                $Searcher = $Session.CreateUpdateSearcher()            
                $Criteria = "IsInstalled=0 and DeploymentAction='Installation' or IsPresent=1 and DeploymentAction='Uninstallation' or IsInstalled=1 and DeploymentAction='Installation' and RebootRequired=1 or IsInstalled=0 and DeploymentAction='Uninstallation' and RebootRequired=1"           
                $SearchResult = $Searcher.Search($Criteria)           
                $SearchResult.Updates
            } catch {
                Write-Warning -Message "Failed to query Windows Update because $($_.Exception.Message)"
            }
        }
    }
     
    Function Show-WindowsUpdate {
        Get-WindowsUpdate |
        Select Title,isHidden,
            @{l='Size (MB)';e={'{0:N2}' -f ($_.MaxDownloadSize/1MB)}},
            @{l='Published';e={$_.LastDeploymentChangeTime}} |
        Sort -Property Published
    }
    
    Function Set-WindowsHiddenUpdate {
     
        [Cmdletbinding()]
     
        Param(
            [Parameter(ValueFromPipeline=$true,Mandatory=$true)]
            [System.__ComObject[]]$Update,
     
            [Parameter(Mandatory=$true)]
            [boolean]$Hide
        )
     
        Process {
            $Update | ForEach-Object -Process {
                if (($_.pstypenames)[0] -eq 'System.__ComObject#{c1c2f21a-d2f4-4902-b5c6-8a081c19a890}') {
                    try {
                        $_.isHidden = $Hide
                        Write-Verbose -Message "Dealing with update $($_.Title)"
                    } catch {
                        Write-Warning -Message "Failed to perform action because $($_.Exception.Message)"
                    }
                } else {
                    Write-Warning -Message "Ignoring object submitted"
                }
            }
        }
    }
    
    $updates = Get-WindowsUpdate
    $updates | Where{$_.Title -like "*language*" -and $_.IsHidden -eq $false} | Set-WindowsHiddenUpdate -Hide $True
    
    #28647
    Nathan Combs
    Participant
    Topics: 1
    Replies: 0

    I am almost finished with my script that gets rid of certain windows updates and hides them but after an unistall it asks if you want to restart I cannot seem to get my

    $wshell = New-Object -ComObject wscript.shell;
    $wshell.AppActivate(‘Windows Update Standalone Installer’)
    Sleep 2
    $wshell.SendKeys(‘~’)

    could I put two keys one to tab and one to enter? maybe an IF statement to say if true for unistalling then hit these two keys? I am newer to this so I don’t have advanced skills for powershell. Thanks!

    #23079

    In reply to: SendKeys

    Participant
    Topics: 1
    Replies: 1
    Points: 0
    Rank: Member

    Great topic! While I’m out digging on my own, I’m hoping you can point towards the modules/functions that would allow me to write to the console command line.
    (I’m trying to build a facility to allow recall and editing of previous commands. I have a version working with SendKeys, but it is pretty wobbly with almost all shifted keystrokes — ‘$’, for instance.)

    Thanks —

Viewing 15 results - 31 through 45 (of 52 total)