Jump to content
Sign in to follow this  
goss34

Powershell In AutoIT

Recommended Posts

goss34

Hi Guys,

I have had a look through the various threads talking about powershell but am struggling to get my script to launch properly.

I intend to have a serious of combo selection boxes populated from an INI file and then when a user hits the "launch" button it uses the selected options to launch the powershell script with those parameters.

This line works great from a command prompt:

C:\windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit -STA -Command "& 'C:\Program Files\ApplicaionDir\TAME\HRMenu\Start\Start.ps1' -engine Server01.domain.local -session TAME_TEST -stage DEV -useCredential 'Server01\'"

Iv tried running it as a single line but that doesnt seem to work for me as it doesnt seem to pass through properly. Iv resorted to using it as a group of variables so i can check the command is passed correctly:

$start = '"C:\Windows\System32\windowspowershell\v1.0\powershell.exe" -noexit -STA -Command'

$middle = '"C:\Program Files\ApplicationDir\TAME\HRMenu\Start\Start.ps1"'

$end = '-engine Server01.domain.local -session TAME_TEST -stage DEV -useCredential "Server01\"'

ConsoleWrite(@ComSpec & ' /c ' & $start & ' ' & $middle & ' ' & $end)

Run(@ComSpec & ' /c ' & $start & ' ' & $middle & ' ' & $end, @SystemDir, @SW_HIDE)

The console write above looks correct but the run command doesnt run.

Can anyone help me correct it? (I am assuming the quotes are half of the problem)

Apologies for the lack of knowledge I have vague knowledge of how AutoIt works but have always had issues getting commands passed through correctly. Powershell however.... this is a first attempt but that part is working fine i just cant launch it using AutoIT at the moment.

Thanks

Dan

Share this post


Link to post
Share on other sites
UEZ

Try this:

$start = "C:\Windows\System32\windowspowershell\v1.0\powershell.exe -noexit -STA -Command "
$middle = "C:\Program Files\ApplicationDir\TAME\HRMenu\Start\Start.ps1 "
$end = '-engine Server01.domain.local -session TAME_TEST -stage DEV -useCredential Server01\'

ConsoleWrite(@ComSpec & ' /c ' & $start & $middle & $end)
Run(@ComSpec & ' /k ' & $start & $middle & $end, @SystemDir, @SW_SHOW)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
goss34

Hi UEZ,

I got back a powershell error so something is still slightly off:

The string starting:

At line:1 char:144

+ C:Program FilesApplicationDirTAMEHRMenuStartStart.ps1 -engine Server01.

domain.local -session TAME_TEST -stage DEV -useCredential Server01 <<<< "

is missing the terminator: ".

At line:1 char:145

+ C:Program FilesApplicationDirTAMEHRMenuStartStart.ps1 -engine Server01.

domain.local -session TAME_TEST -stage DEV -useCredential Server01" <<<<

+ CategoryInfo : ParserError: (:String) [], ParentContainsErrorRe

cordException

+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

At the end of the batch file the -useCredential input is specified in quotes, on the console write it looks like it is too but then appears to fail passing it through after that. I think the problem is here but i cant figure out how to send exactly what it wants.

I noticed on another topic there was a . after the -Command but wasnt sure if that was relevant or not, maybe its something AutoIT requires?

Thanks

Dan

Share this post


Link to post
Share on other sites
Jos

Try:

Run( @comspec & ' /k C:\windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit -STA -Command "& ''C:\Program Files\ApplicaionDir\TAME\HRMenu\Start\Start.ps1'' -engine Server01.domain.local -session TAME_TEST -stage DEV -useCredential 'Server01\'"'

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
UEZ

I think the problem is "Server01".

Try it without quotes.

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
goss34

Hi UEZ,

Without the quotes on Server01 i get an error earlier in the script:

The term 'C:Program' is not recognized as the name of a cmdlet, function, scri

pt file, or operable program. Check the spelling of the name, or if a path was

included, verify that the path is correct and try again.

At line:1 char:11

+ C:Program <<<< FilesApplicationDirTAMEHRMenuStartStart.ps1 -engine Ser

ver01.domain.local -session TAME_TEST -stage DEV -useCredential Server01

+ CategoryInfo : ObjectNotFound: (C:Program:String) [], CommandN

otFoundException

+ FullyQualifiedErrorId : CommandNotFoundException

Hi Jos,

Using yours i have pretty much the same issue with the quotes, before i run it i can see using the script editor the syntax isnt correct as its highlighted in black as opposed to red like the rest of the text.

When i run it, it returns:

C:PowerShell Test.au3(48,244) : ERROR: syntax error

Run(@comspec & ' /k C:\Windows\System32\WindowsPowerShellv1.0PowerShell.exe -noexit -STA -Command "& ''C:Program FilesApplicationDirTAMEHRMenuStartStart.ps1'' -engine Server01.domain.local -session TAME_TEST -stage DEV -useCredential 'Server01

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:PowerShell Test.au3 - 1 error(s), 0 warning(s)

!>16:55:48 AU3Check ended. Press F4 to jump to next error.rc:2

>Exit code: 2 Time: 0.313

Thanks for your help,

Dan

Share this post


Link to post
Share on other sites
Jos

I missed the single qouotes around the server at the end:

Run(@comspec & ' /k C:\windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit -STA -Command "& ''C:\Program Files\ApplicationDir\TAME\HRMenu\Start\Start.ps1'' -engine Server01.domain.local -session TAME_TEST -stage DEV -useCredential ''Server01\''"')
Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
goss34

I missed the single qouotes around the server at the end:

Run(@comspec & ' /k C:\windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit -STA -Command "& ''C:\Program Files\ApplicationDir\TAME\HRMenu\Start\Start.ps1'' -engine Server01.domain.local -session TAME_TEST -stage DEV -useCredential ''Server01\''"')

Excellent Jos, that works great and its exactly how i wanted it to run in the first place.

I can now continue with the rest of the GUI and functions and hopefully i wont run into anymore syntax issues!

Thanks to you both for your prompt help.

Dan

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  

  • Similar Content

    • hemichallenger
      By hemichallenger
      Not sure if this is possible or if someone has already tackled this. Is it possible to embed PowerShell into a GUI? Autoit is great for quick function but sometime I need to run a command thru PowerShell. It be great to have PowerShell console embedded into the gui.
    • BigDaddyO
      By BigDaddyO
      After much searching I finally found a method to get eMails from an Exchange eMail account inbox.  I was hoping for AutoIT or vbscript but I couldn't find any that would read the inbox messages without using Outlook.
      I needed this because I'm testing a web-form that generates an eMail sent to a shared mailbox "not what my current outlook is configured for".  so, I needed to connect to a different account, then get the inbox messages, and see if the auto-generated eMail message body contains what I submitted in the form.
       
      I found a PowerShell script that was close and modified it to do just what I want, but I'd still like it to run in AutoIT but I'm not sure how to use the Microsoft.Exchange.WebServices.dll
      Anybody have some ideas? 
      #To Launch! # C:\Windows\System32> powershell -ExecutionPolicy ByPass #This launches PowerShell and allows execution of .ps1 files # PS C:\Windows\System32> . "C:\Temp\eMail\getInbox.ps1" #The period . in front of the .ps1 file forces PS to display results on-screen # Where is the EWS .DLL file that you are using # Get the installer from https://www.microsoft.com/en-us/download/details.aspx?id=42022 # We only need 2 dll's from the install and they can be stored anywhere: "Microsoft.Exchange.WebServices.Auth.dll" & "Microsoft.Exchange.WebServices.dll" $EWSdll = "C:\Temp\eMail\Microsoft.Exchange.WebServices.dll" # Where do you want the output text file to be saved $Output = "C:\Temp\eMails.txt" # replace with your email address $email = "MyemailAddress@work.net" # only need to populate these if you're impersonating... $username = "myemail" $password = "Sup3rS3cre+" $domain = "ad.work.net" # load the assembly : point to the dll in the location you have the .dll file [void] [Reflection.Assembly]::LoadFile($EWSdll) # set ref to exchange, first references 2007, 2nd is 2010 (default) #$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1) $s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService # use first option if you want to impersonate, otherwise, grab your own credentials with the 3rd one. not sure what the 2nd one is for $s.Credentials = New-Object Net.NetworkCredential($username, $password, $domain) ##$s.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials #$s.UseDefaultCredentials = $true # discover the url from your email address $s.AutodiscoverUrl($email) # get a handle to the inbox $inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox) #create a property set (to let us access the body & other details not available from the FindItems call) $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties) $psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text; # If you have a set number of items you want to get, use this and insert the # in the () # $items = $inbox.FindItems(5) # If you want to retrieve all items (Server limit is usually at 1000) then use this line # Details on the max returned by server: https://blogs.msdn.microsoft.com/exchangedev/2010/03/12/throttling-policies-and-the-ewsfindcountlimit/ $items = $inbox.FindItems($inbox.TotalCount) # Put some counts at the top of the output Write-host "Total Inbox count: $($inbox.TotalCount)" Write-host "Unread count: $($inbox.UnreadCount)" #These two lines, write the output to the specified text file Add-Content $Output "Total Inbox count: $($inbox.TotalCount)" Add-Content $Output "Unread count: $($inbox.UnreadCount)" foreach ($item in $items.Items) { # load the property set to allow us to get to the body $item.load($psPropertySet) # Get the Body text as-is $bod = $item.Body.Text #if you only want a short summary of the Body, then comment the above line and un-comment these 4 lines # $bod = $item.Body.Text -replace '\s+', ' ' # $bodCutOff = (100,$bod.Length | Measure-Object -Minimum).Minimum # $bod = $bod.Substring(0,$bodCutOff) # $bod = "$bod..." # output the results - first of all the From, Subject, References and Message ID write-host "====================================================================" Write-host "From: $($item.From.Name)" Write-host "Subject: $($item.Subject)" Write-host "Body: $($bod)" write-host "====================================================================" "" # Output the results to the specified Text file Add-Content $Output "" Add-Content $Output "====================================================================" Add-Content $Output "From: $($item.From.Name)" Add-Content $Output "Subject: $($item.Subject)" Add-Content $Output "Body:",$($bod) Add-Content $Output "====================================================================" Add-Content $Output "" } #see these URLs for more info # EWS Stuff # folder members: https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.folder_members%28v=exchg.80%29.aspx # exporting headers: https://www.allabout365.com/2010/10/export-email-headers-exchange-powershell/ # read emails with EWS: https://social.technet.microsoft.com/Forums/en-US/3fbf8348-2945-43aa-a0bc-f3b1d34da27c/read-emails-with-ews?forum=exchangesvrdevelopment  
      Thanks,
      Mike
    • Jibberish
      By Jibberish
      Hi all,
      I need to read a log file into an array, but the log file is encoded as $FO_UTF16_BE_NOBOM (2048) = Use Unicode UTF16 Big Endian (without BOM) per FileGetEncoding (it returns 2048).
      I have searched how to convert these log files to UTF-8 and finally found a Powershell command. Since then I have been racking my brain trying to get the function to work. The command itself works from a Powerscript prompt:
      C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command Get-Content C:\Logs\Myplayer_10-10-17-02-31.log | Set-Content -Encoding utf8 C:\Logs\Myplayer1.log This is my sandbox;
      #include <array.au3> #include <File.au3> Local $aArrayLogFile Local $sLogDir = "C:\Logs\" Local $sLogFile = "Myplayer_10-10-17-02-31.log" Local $sConvertedLog = "ConvertedLog.log" Local $sLogDirFile = $sLogDir&$sLogFile RunWait("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command Get-Content "&$sLogDirFile&" | Set-Content -Encoding utf8 "&$sConvertedLog,$sLogDir) _FileReadToArray($sLogDirFile, $aArrayLogFile) _ArrayDisplay($aArrayLogFile) Also tried
      RunWait("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command Get-Content "&$sLogDirFile&" | Set-Content -Encoding utf8 "&$sConvertedLog,$sLogDir) and
      ShellExecuteWait("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"," -Command Get-Content "&$sLogDirFile&" | Set-Content -Encoding utf8 "&$sConvertedLog,$sLogDir) Tried without -Command and a bunch of other parameters that were sprinkled throughout the internet from people trying to get this to work.
      Thanks
      Jibs
    • ur
      By ur
      I am maintaining all the reusable code in a separate file as library.au3.
      In that file I have referenced some dependent files using fileinstall, so that they will be extracted when necessary.
       
      Problem is, if I use a function in the library.au3 in another script which doesn't require this dependent file, as I am including the whole file using include tag, it is embedding that file also.
      Is there any way to exclude that.
       
    • david1337
      By david1337
      Hi guys,
      I need a little help here
      I have this simple Powershell script, that is able to set "sig.htm" as signature in OWA for the mail account specified.
      First part of the script just loads the Exchange snapin, since this is needed for the Get-Mailbox command.
      #Add Exchange 2010/2013 snapin if not already loaded in the PowerShell session if (!(Get-PSSnapin | where {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"})) { try { Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction STOP } catch { #Snapin was not loaded Write-Warning $_.Exception.Message EXIT } . $env:ExchangeInstallPath\bin\RemoteExchange.ps1 Connect-ExchangeServer -auto -AllowClobber } $mailboxes = Get-Mailbox -Identity user@domain.com $mailboxes| foreach {$file= "sig.htm"; Set-MailboxMessageConfiguration -identity $_.alias -SignatureHtml "$(Get-Content -Path $file -ReadCount 0)"} What I want is for AutoIt to use Powershell.exe to run the script somehow. This way I would be able to use AutoIt variables and arrays together with the PS script.
       I guess the solution would be to run Powershell.exe with command line arguments?
      If that is not possible, maybe have the script stored as a "ps1" file, and interact with it that way? (not preferred)
×