SendKeys Method in Powershell












2














I have batch file for telnet a server automatically, I want to do the same thing with PowerShell



Batch File named Script.bat :



:: Open a Telnet window
start telnet.exe 10.84.10.85
:: Run the script
cscript SendKeys.vbs


Command File named SendKeys.vbs :



set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 1000
OBJECT.SendKeys "myPassword{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "7{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "1{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "Y{ENTER}"
WScript.sleep 3000
OBJECT.SendKeys ""









share|improve this question




















  • 2




    start-process pathtelnet.exe -argumentlist 10.84.10.85 to start telnet, $obj = New-Object -com Wscript.Shell to create the object, $obj.SendKeys("x") to send the keys, sleep -ms 1000to sleep
    – SimonS
    Sep 13 '17 at 12:44










  • that's what I'm looking for exact !! merci :)
    – yazan
    Sep 13 '17 at 13:41










  • @SimonS please make an aswer from your comment.
    – JosefZ
    Sep 13 '17 at 16:10
















2














I have batch file for telnet a server automatically, I want to do the same thing with PowerShell



Batch File named Script.bat :



:: Open a Telnet window
start telnet.exe 10.84.10.85
:: Run the script
cscript SendKeys.vbs


Command File named SendKeys.vbs :



set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 1000
OBJECT.SendKeys "myPassword{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "7{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "1{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "Y{ENTER}"
WScript.sleep 3000
OBJECT.SendKeys ""









share|improve this question




















  • 2




    start-process pathtelnet.exe -argumentlist 10.84.10.85 to start telnet, $obj = New-Object -com Wscript.Shell to create the object, $obj.SendKeys("x") to send the keys, sleep -ms 1000to sleep
    – SimonS
    Sep 13 '17 at 12:44










  • that's what I'm looking for exact !! merci :)
    – yazan
    Sep 13 '17 at 13:41










  • @SimonS please make an aswer from your comment.
    – JosefZ
    Sep 13 '17 at 16:10














2












2








2


1





I have batch file for telnet a server automatically, I want to do the same thing with PowerShell



Batch File named Script.bat :



:: Open a Telnet window
start telnet.exe 10.84.10.85
:: Run the script
cscript SendKeys.vbs


Command File named SendKeys.vbs :



set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 1000
OBJECT.SendKeys "myPassword{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "7{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "1{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "Y{ENTER}"
WScript.sleep 3000
OBJECT.SendKeys ""









share|improve this question















I have batch file for telnet a server automatically, I want to do the same thing with PowerShell



Batch File named Script.bat :



:: Open a Telnet window
start telnet.exe 10.84.10.85
:: Run the script
cscript SendKeys.vbs


Command File named SendKeys.vbs :



set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 1000
OBJECT.SendKeys "myPassword{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "7{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "1{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "Y{ENTER}"
WScript.sleep 3000
OBJECT.SendKeys ""






powershell telnet






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 15 at 5:43









Pimp Juice IT

22.9k113869




22.9k113869










asked Sep 13 '17 at 12:29









yazan

921116




921116








  • 2




    start-process pathtelnet.exe -argumentlist 10.84.10.85 to start telnet, $obj = New-Object -com Wscript.Shell to create the object, $obj.SendKeys("x") to send the keys, sleep -ms 1000to sleep
    – SimonS
    Sep 13 '17 at 12:44










  • that's what I'm looking for exact !! merci :)
    – yazan
    Sep 13 '17 at 13:41










  • @SimonS please make an aswer from your comment.
    – JosefZ
    Sep 13 '17 at 16:10














  • 2




    start-process pathtelnet.exe -argumentlist 10.84.10.85 to start telnet, $obj = New-Object -com Wscript.Shell to create the object, $obj.SendKeys("x") to send the keys, sleep -ms 1000to sleep
    – SimonS
    Sep 13 '17 at 12:44










  • that's what I'm looking for exact !! merci :)
    – yazan
    Sep 13 '17 at 13:41










  • @SimonS please make an aswer from your comment.
    – JosefZ
    Sep 13 '17 at 16:10








2




2




start-process pathtelnet.exe -argumentlist 10.84.10.85 to start telnet, $obj = New-Object -com Wscript.Shell to create the object, $obj.SendKeys("x") to send the keys, sleep -ms 1000to sleep
– SimonS
Sep 13 '17 at 12:44




start-process pathtelnet.exe -argumentlist 10.84.10.85 to start telnet, $obj = New-Object -com Wscript.Shell to create the object, $obj.SendKeys("x") to send the keys, sleep -ms 1000to sleep
– SimonS
Sep 13 '17 at 12:44












that's what I'm looking for exact !! merci :)
– yazan
Sep 13 '17 at 13:41




that's what I'm looking for exact !! merci :)
– yazan
Sep 13 '17 at 13:41












@SimonS please make an aswer from your comment.
– JosefZ
Sep 13 '17 at 16:10




@SimonS please make an aswer from your comment.
– JosefZ
Sep 13 '17 at 16:10










2 Answers
2






active

oldest

votes


















4














PowerShell has no built-in functionality to emulate keystrokes.



Practically, you have two options: COM-Automation and Interop.




  1. SendKeys via COM


Like in VB(S)) you can create a Shell-Object and SendKeys. Here is the PowerShell way to do it.



$wshell = New-Object -ComObject wscript.shell;
$wshell.SendKeys('a')


If you would like to send a keystroke to a window, you have to activate it first:



$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('title of the application window')
Sleep 1
$wshell.SendKeys('~')


Some keystrokes have special variables like ~ for RETURN. Here is a complete list.

After activating a window it's often necessary to wait a second until it becomes responsive, otherwise it'll send the key to the PowerShell window, or to nowhere.
The scripting Host's SendKeys method can be unreliable, but luckily there is a better approach.




  1. SendKeys via Interop


Like in C#, you can use the SendWait method from the .NET Framework in PowerShell.



[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("x")


If you want to activate a window it can be don like this:



[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
[Microsoft.VisualBasic.Interaction]::AppActivate("Internet Explorer - Windows")


To Sleep, you can use the Start-Sleep Cmdlet.



Regarding your original problem, I would suggest the following solution:



# Open a Telnet window
Start-Process telnet.exe -ArgumentList 10.84.10.85
# Run the keystrokes
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('myPassword{ENTER}')
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait('7{ENTER}')
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait('1{ENTER}')
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait('Y{ENTER}')
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait('')


WARNING: Be extra careful if you're using this method to send a password, because activating a different window between invoking AppActivate and invoking SendKeys will cause the password to be sent to that different window in plain text (e.g. your favorite messenger)!






share|improve this answer





























    0














    I did some of modification in the script, I have a list of IP server which have the same password and I want to telnet the list automatically and sendKey for deactivate or activate the FTP server .



    my script is :



      ## - List of IP
    $printers = get-content "C:Dir2servers.txt"

    foreach ($IPAddress in $printers){

    ## - Start Telnet Session:
    start-process C:WindowsSystem32telnet.exe -argumentlist $IPAddress

    ## - SendKey for each IP
    $obj = New-Object -com Wscript.Shell
    sleep -s 3
    $obj.SendKeys("MyPassword{ENTER}")
    sleep -s 3
    $obj.SendKeys("7{ENTER}")
    sleep -s 3
    $obj.SendKeys("1{ENTER}")
    sleep -s 3
    $obj.SendKeys("{ENTER}")
    sleep -s 3
    $obj.SendKeys("{ENTER}")
    sleep -s 3
    $obj.SendKeys("Y{ENTER}")
    sleep -s 3
    $obj.SendKeys("{ENTER}")
    sleep -s 3
    }





    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "3"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1249976%2fsendkeys-method-in-powershell%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      PowerShell has no built-in functionality to emulate keystrokes.



      Practically, you have two options: COM-Automation and Interop.




      1. SendKeys via COM


      Like in VB(S)) you can create a Shell-Object and SendKeys. Here is the PowerShell way to do it.



      $wshell = New-Object -ComObject wscript.shell;
      $wshell.SendKeys('a')


      If you would like to send a keystroke to a window, you have to activate it first:



      $wshell = New-Object -ComObject wscript.shell;
      $wshell.AppActivate('title of the application window')
      Sleep 1
      $wshell.SendKeys('~')


      Some keystrokes have special variables like ~ for RETURN. Here is a complete list.

      After activating a window it's often necessary to wait a second until it becomes responsive, otherwise it'll send the key to the PowerShell window, or to nowhere.
      The scripting Host's SendKeys method can be unreliable, but luckily there is a better approach.




      1. SendKeys via Interop


      Like in C#, you can use the SendWait method from the .NET Framework in PowerShell.



      [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
      [System.Windows.Forms.SendKeys]::SendWait("x")


      If you want to activate a window it can be don like this:



      [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
      [Microsoft.VisualBasic.Interaction]::AppActivate("Internet Explorer - Windows")


      To Sleep, you can use the Start-Sleep Cmdlet.



      Regarding your original problem, I would suggest the following solution:



      # Open a Telnet window
      Start-Process telnet.exe -ArgumentList 10.84.10.85
      # Run the keystrokes
      Add-Type -AssemblyName System.Windows.Forms
      [System.Windows.Forms.SendKeys]::SendWait('myPassword{ENTER}')
      Start-Sleep 1
      [System.Windows.Forms.SendKeys]::SendWait('7{ENTER}')
      Start-Sleep 1
      [System.Windows.Forms.SendKeys]::SendWait('1{ENTER}')
      Start-Sleep 1
      [System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
      Start-Sleep 1
      [System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
      Start-Sleep 1
      [System.Windows.Forms.SendKeys]::SendWait('Y{ENTER}')
      Start-Sleep 1
      [System.Windows.Forms.SendKeys]::SendWait('')


      WARNING: Be extra careful if you're using this method to send a password, because activating a different window between invoking AppActivate and invoking SendKeys will cause the password to be sent to that different window in plain text (e.g. your favorite messenger)!






      share|improve this answer


























        4














        PowerShell has no built-in functionality to emulate keystrokes.



        Practically, you have two options: COM-Automation and Interop.




        1. SendKeys via COM


        Like in VB(S)) you can create a Shell-Object and SendKeys. Here is the PowerShell way to do it.



        $wshell = New-Object -ComObject wscript.shell;
        $wshell.SendKeys('a')


        If you would like to send a keystroke to a window, you have to activate it first:



        $wshell = New-Object -ComObject wscript.shell;
        $wshell.AppActivate('title of the application window')
        Sleep 1
        $wshell.SendKeys('~')


        Some keystrokes have special variables like ~ for RETURN. Here is a complete list.

        After activating a window it's often necessary to wait a second until it becomes responsive, otherwise it'll send the key to the PowerShell window, or to nowhere.
        The scripting Host's SendKeys method can be unreliable, but luckily there is a better approach.




        1. SendKeys via Interop


        Like in C#, you can use the SendWait method from the .NET Framework in PowerShell.



        [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
        [System.Windows.Forms.SendKeys]::SendWait("x")


        If you want to activate a window it can be don like this:



        [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
        [Microsoft.VisualBasic.Interaction]::AppActivate("Internet Explorer - Windows")


        To Sleep, you can use the Start-Sleep Cmdlet.



        Regarding your original problem, I would suggest the following solution:



        # Open a Telnet window
        Start-Process telnet.exe -ArgumentList 10.84.10.85
        # Run the keystrokes
        Add-Type -AssemblyName System.Windows.Forms
        [System.Windows.Forms.SendKeys]::SendWait('myPassword{ENTER}')
        Start-Sleep 1
        [System.Windows.Forms.SendKeys]::SendWait('7{ENTER}')
        Start-Sleep 1
        [System.Windows.Forms.SendKeys]::SendWait('1{ENTER}')
        Start-Sleep 1
        [System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
        Start-Sleep 1
        [System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
        Start-Sleep 1
        [System.Windows.Forms.SendKeys]::SendWait('Y{ENTER}')
        Start-Sleep 1
        [System.Windows.Forms.SendKeys]::SendWait('')


        WARNING: Be extra careful if you're using this method to send a password, because activating a different window between invoking AppActivate and invoking SendKeys will cause the password to be sent to that different window in plain text (e.g. your favorite messenger)!






        share|improve this answer
























          4












          4








          4






          PowerShell has no built-in functionality to emulate keystrokes.



          Practically, you have two options: COM-Automation and Interop.




          1. SendKeys via COM


          Like in VB(S)) you can create a Shell-Object and SendKeys. Here is the PowerShell way to do it.



          $wshell = New-Object -ComObject wscript.shell;
          $wshell.SendKeys('a')


          If you would like to send a keystroke to a window, you have to activate it first:



          $wshell = New-Object -ComObject wscript.shell;
          $wshell.AppActivate('title of the application window')
          Sleep 1
          $wshell.SendKeys('~')


          Some keystrokes have special variables like ~ for RETURN. Here is a complete list.

          After activating a window it's often necessary to wait a second until it becomes responsive, otherwise it'll send the key to the PowerShell window, or to nowhere.
          The scripting Host's SendKeys method can be unreliable, but luckily there is a better approach.




          1. SendKeys via Interop


          Like in C#, you can use the SendWait method from the .NET Framework in PowerShell.



          [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
          [System.Windows.Forms.SendKeys]::SendWait("x")


          If you want to activate a window it can be don like this:



          [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
          [Microsoft.VisualBasic.Interaction]::AppActivate("Internet Explorer - Windows")


          To Sleep, you can use the Start-Sleep Cmdlet.



          Regarding your original problem, I would suggest the following solution:



          # Open a Telnet window
          Start-Process telnet.exe -ArgumentList 10.84.10.85
          # Run the keystrokes
          Add-Type -AssemblyName System.Windows.Forms
          [System.Windows.Forms.SendKeys]::SendWait('myPassword{ENTER}')
          Start-Sleep 1
          [System.Windows.Forms.SendKeys]::SendWait('7{ENTER}')
          Start-Sleep 1
          [System.Windows.Forms.SendKeys]::SendWait('1{ENTER}')
          Start-Sleep 1
          [System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
          Start-Sleep 1
          [System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
          Start-Sleep 1
          [System.Windows.Forms.SendKeys]::SendWait('Y{ENTER}')
          Start-Sleep 1
          [System.Windows.Forms.SendKeys]::SendWait('')


          WARNING: Be extra careful if you're using this method to send a password, because activating a different window between invoking AppActivate and invoking SendKeys will cause the password to be sent to that different window in plain text (e.g. your favorite messenger)!






          share|improve this answer












          PowerShell has no built-in functionality to emulate keystrokes.



          Practically, you have two options: COM-Automation and Interop.




          1. SendKeys via COM


          Like in VB(S)) you can create a Shell-Object and SendKeys. Here is the PowerShell way to do it.



          $wshell = New-Object -ComObject wscript.shell;
          $wshell.SendKeys('a')


          If you would like to send a keystroke to a window, you have to activate it first:



          $wshell = New-Object -ComObject wscript.shell;
          $wshell.AppActivate('title of the application window')
          Sleep 1
          $wshell.SendKeys('~')


          Some keystrokes have special variables like ~ for RETURN. Here is a complete list.

          After activating a window it's often necessary to wait a second until it becomes responsive, otherwise it'll send the key to the PowerShell window, or to nowhere.
          The scripting Host's SendKeys method can be unreliable, but luckily there is a better approach.




          1. SendKeys via Interop


          Like in C#, you can use the SendWait method from the .NET Framework in PowerShell.



          [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
          [System.Windows.Forms.SendKeys]::SendWait("x")


          If you want to activate a window it can be don like this:



          [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
          [Microsoft.VisualBasic.Interaction]::AppActivate("Internet Explorer - Windows")


          To Sleep, you can use the Start-Sleep Cmdlet.



          Regarding your original problem, I would suggest the following solution:



          # Open a Telnet window
          Start-Process telnet.exe -ArgumentList 10.84.10.85
          # Run the keystrokes
          Add-Type -AssemblyName System.Windows.Forms
          [System.Windows.Forms.SendKeys]::SendWait('myPassword{ENTER}')
          Start-Sleep 1
          [System.Windows.Forms.SendKeys]::SendWait('7{ENTER}')
          Start-Sleep 1
          [System.Windows.Forms.SendKeys]::SendWait('1{ENTER}')
          Start-Sleep 1
          [System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
          Start-Sleep 1
          [System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
          Start-Sleep 1
          [System.Windows.Forms.SendKeys]::SendWait('Y{ENTER}')
          Start-Sleep 1
          [System.Windows.Forms.SendKeys]::SendWait('')


          WARNING: Be extra careful if you're using this method to send a password, because activating a different window between invoking AppActivate and invoking SendKeys will cause the password to be sent to that different window in plain text (e.g. your favorite messenger)!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 13 '17 at 16:19









          wp78de

          966215




          966215

























              0














              I did some of modification in the script, I have a list of IP server which have the same password and I want to telnet the list automatically and sendKey for deactivate or activate the FTP server .



              my script is :



                ## - List of IP
              $printers = get-content "C:Dir2servers.txt"

              foreach ($IPAddress in $printers){

              ## - Start Telnet Session:
              start-process C:WindowsSystem32telnet.exe -argumentlist $IPAddress

              ## - SendKey for each IP
              $obj = New-Object -com Wscript.Shell
              sleep -s 3
              $obj.SendKeys("MyPassword{ENTER}")
              sleep -s 3
              $obj.SendKeys("7{ENTER}")
              sleep -s 3
              $obj.SendKeys("1{ENTER}")
              sleep -s 3
              $obj.SendKeys("{ENTER}")
              sleep -s 3
              $obj.SendKeys("{ENTER}")
              sleep -s 3
              $obj.SendKeys("Y{ENTER}")
              sleep -s 3
              $obj.SendKeys("{ENTER}")
              sleep -s 3
              }





              share|improve this answer


























                0














                I did some of modification in the script, I have a list of IP server which have the same password and I want to telnet the list automatically and sendKey for deactivate or activate the FTP server .



                my script is :



                  ## - List of IP
                $printers = get-content "C:Dir2servers.txt"

                foreach ($IPAddress in $printers){

                ## - Start Telnet Session:
                start-process C:WindowsSystem32telnet.exe -argumentlist $IPAddress

                ## - SendKey for each IP
                $obj = New-Object -com Wscript.Shell
                sleep -s 3
                $obj.SendKeys("MyPassword{ENTER}")
                sleep -s 3
                $obj.SendKeys("7{ENTER}")
                sleep -s 3
                $obj.SendKeys("1{ENTER}")
                sleep -s 3
                $obj.SendKeys("{ENTER}")
                sleep -s 3
                $obj.SendKeys("{ENTER}")
                sleep -s 3
                $obj.SendKeys("Y{ENTER}")
                sleep -s 3
                $obj.SendKeys("{ENTER}")
                sleep -s 3
                }





                share|improve this answer
























                  0












                  0








                  0






                  I did some of modification in the script, I have a list of IP server which have the same password and I want to telnet the list automatically and sendKey for deactivate or activate the FTP server .



                  my script is :



                    ## - List of IP
                  $printers = get-content "C:Dir2servers.txt"

                  foreach ($IPAddress in $printers){

                  ## - Start Telnet Session:
                  start-process C:WindowsSystem32telnet.exe -argumentlist $IPAddress

                  ## - SendKey for each IP
                  $obj = New-Object -com Wscript.Shell
                  sleep -s 3
                  $obj.SendKeys("MyPassword{ENTER}")
                  sleep -s 3
                  $obj.SendKeys("7{ENTER}")
                  sleep -s 3
                  $obj.SendKeys("1{ENTER}")
                  sleep -s 3
                  $obj.SendKeys("{ENTER}")
                  sleep -s 3
                  $obj.SendKeys("{ENTER}")
                  sleep -s 3
                  $obj.SendKeys("Y{ENTER}")
                  sleep -s 3
                  $obj.SendKeys("{ENTER}")
                  sleep -s 3
                  }





                  share|improve this answer












                  I did some of modification in the script, I have a list of IP server which have the same password and I want to telnet the list automatically and sendKey for deactivate or activate the FTP server .



                  my script is :



                    ## - List of IP
                  $printers = get-content "C:Dir2servers.txt"

                  foreach ($IPAddress in $printers){

                  ## - Start Telnet Session:
                  start-process C:WindowsSystem32telnet.exe -argumentlist $IPAddress

                  ## - SendKey for each IP
                  $obj = New-Object -com Wscript.Shell
                  sleep -s 3
                  $obj.SendKeys("MyPassword{ENTER}")
                  sleep -s 3
                  $obj.SendKeys("7{ENTER}")
                  sleep -s 3
                  $obj.SendKeys("1{ENTER}")
                  sleep -s 3
                  $obj.SendKeys("{ENTER}")
                  sleep -s 3
                  $obj.SendKeys("{ENTER}")
                  sleep -s 3
                  $obj.SendKeys("Y{ENTER}")
                  sleep -s 3
                  $obj.SendKeys("{ENTER}")
                  sleep -s 3
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 14 '17 at 8:00









                  yazan

                  921116




                  921116






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Super User!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1249976%2fsendkeys-method-in-powershell%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Plaza Victoria

                      In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

                      How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...