Sign in to follow this  
Followers 0
ragupta

Pass an input to program triggered under cmd prompt

3 posts in this topic

I have a shortcut which trigger powershell.exe with some arguments. After launching this shortcut I need to fire some commands on this window and get the output to make sure if the commands are working fine or not.

Let say I have a shorcut icon desctop ... i.e. xyz.lnk

When I doble click it launches console with below content

-----------

C:\Program Files (x86)\xyz>C:\Windows\System32\windowspowershell\v1.0\powershell.exe -NoExit -ExecutionPolicy RemoteSigned -File .\StartxyzPS.ps1

Windows PowerShell

Copyright © 2009 Microsoft Corporation. All rights reserved.

PS C:/>

---------

I can launch using shellexecute but not sure how to pass commands to it

ShellExecute("C:\Users\Public\Desktop\xyz.lnk")

Also when I use Run , cmd prompt closes and script exit before I could use stdinwrite function.

Local $pid = Run(@ComSpec & " /c " & '"C:\Users\Public\Desktop\xyz.lnk"', "", @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD)

Please advise.

Share this post


Link to post
Share on other sites



How about running the program directly? I don't have PowerShell or your OS (I'm on XP), so I can't test this code:

shellexecute("C:\Windows\System32\windowspowershell\v1.0\powershell.exe", "-NoExit -ExecutionPolicy RemoteSigned -File .\StartxyzPS.ps1")

RAID Calculator | Software Installer

The truth has been suppressed since the dawn of time.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I would too suggest to run the program directly from AutoIt. It's easier to grab the output.

This little code snippet shows how I create an Exchange mailbox for a user and check the output to determine if the mailbox was usccessfully created.

$sCMD = "C:\Windows\System32\WindowsPowerShellv1.0powershell.exe -command . " & _
    "'D:Exchange ServerV14binRemoteExchange.ps1'; Connect-ExchangeServer -auto; Enable-Mailbox -Identity " & _
    $Kurzzeichen & " -Alias " & $Kurzzeichen & " -Database " & $sEXDatabase & $sSMTPAddress
$PID = Run($sCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
StdinWrite($PID, @CRLF)
StdinWrite($PID)
; Process STDOUT
$sSTDOUT = ""
While 1
    $sOutput = StdoutRead($pid)
    If @error Then ExitLoop
    If $sOutput <> "" Then $sSTDOUT = $sSTDOUT & @CRLF & $sOutput
WEnd
; Process STDERR
$sSTDERR = ""
While 1
    $sOutput = StdErrRead($pid)
    If @error Then ExitLoop
    If $sOutput <> "" Then $sSTDERR = $sSTDERR & @CRLF & $sOutput
WEnd
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0