Jump to content
Sign in to follow this  
SpinningCone

run() behaving unexpectedly

Recommended Posts

SpinningCone

For some reason the autoit Run() command is behaving differently from the windows Run functionality. :

i'm trying to build an autoit script that can run some exchange powershell commands. ex

$ID = "user";
$psCommand = "Enable-MailUser -Identity '" & $ID &"' -Alias '"& $ID &"' -ExternalEmailAddress 'SMTP:"& $ID & "@domain.com'"
Run( "Powershell.exe -noexit Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin;" & $psCommand)

when this runs the powershell console opens and immediatly tells me that my snap in is not installed.

however if i do this

$ID = "user";
$psCommand = "Enable-MailUser -Identity '" & $ID &"' -Alias '"& $ID &"' -ExternalEmailAddress 'SMTP:"& $ID & "@domain.com'"
ConsoleWrite( "Powershell.exe -noexit Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin;" & $psCommand)

and i copy paste the console output into the run dialoge box in windows it opens up a powershell script and runs properly (finds the snapin etc) and that's with a standard console too, autoit is running as admin so its even getting an admin powershell console and it still can't do it!

I have no idea what's going on here. one powershell instance is somehow different than the other since it can't find the snap in.

Share this post


Link to post
Share on other sites
water

This is the code snippet I use to mail enable a user:

$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)
I grab the output and check for error messages. This works fine here. Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
SpinningCone

This is the code snippet I use to mail enable a user:

$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)
I grab the output and check for error messages. This works fine here.

i tried putting the snapin code into a ps1 file and running that however it still throws the same error. powershell loses its brain when I instantiate it from autoIt , the command i'm executing is valid but unrecognized until i run the snap-in. your code has the same probelm just says snap in not installed.

ShellExecute also exhibits the same behavior. powershell opens but refuses to recognize the existance of the snap-in

Share this post


Link to post
Share on other sites
water

I can only imagine that the "Working Directory" makes the difference. You can specify the working directory as parameter 2 for the Run statement.

Can you try:

$ID = "user";
$psCommand = "Enable-MailUser -Identity '" & $ID &"' -Alias '"& $ID &"' -ExternalEmailAddress 'SMTP:"& $ID & "@domain.com'"
Run( "Powershell.exe -noexit Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin;" & $psCommand, @SystemDir)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
SpinningCone

I can only imagine that the "Working Directory" makes the difference. You can specify the working directory as parameter 2 for the Run statement.

Can you try:

$ID = "user";
$psCommand = "Enable-MailUser -Identity '" & $ID &"' -Alias '"& $ID &"' -ExternalEmailAddress 'SMTP:"& $ID & "@domain.com'"
Run( "Powershell.exe -noexit Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin;" & $psCommand, @SystemDir)

i was thining the sme but even designating @SystemDir still has the same problem.

even if i just type into the opern powershel terminal and manually change to a directory and try to load the snap-in i get nothing. it only seems to work if i run powershell from windows not autoit. I think i'm boned on this one.

Share this post


Link to post
Share on other sites
SpinningCone

OK found the problem, hit a lucky post while googling. apprarantly the snap in is 64 bit only an i'm running 32 bit autoit. which is calling a 32 bit powershell.

if i compile the script into a 64bit exe it works. guess i need to figure out if i can run autoIt as 64 bit, or just compile it ever time ineed to test it.

Edit: i mean run code straight out of SciTE as 64 bit since that's the IDE i use to code,

Edited by SpinningCone

Share this post


Link to post
Share on other sites
water

64-Bit that would have been the next thing I'd have suggested to try. I have a note in my script that I need to compile it for 64 bit in my environment.

You can add a directive at the top of your script so it will always be compiled for 64 bit: #AutoIt3Wrapper_UseX64=y


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
SpinningCone

64-Bit that would have been the next thing I'd have suggested to try. I have a note in my script that I need to compile it for 64 bit in my environment.

You can add a directive at the top of your script so it will always be compiled for 64 bit: #AutoIt3Wrapper_UseX64=y

yeah compiling isnt a big deal i generally only do that once, currently researching how to make SciTE run the code in 64 bit when i hit "GO" , it slows development a lot if i have to compile an exe every time i need to test a new function. looking at the AutoIt3Wrapper that SciTE uses it shoudl automatically detect the OS and run the 64 bit version but for some reason it isnt.

Share this post


Link to post
Share on other sites
BrewManNH

Put that line at the top of your script and Scite (technically it's the AutoIt3Wrapper) will run the 64Bit version of AutoIt.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
water

If you add the directive I mentioned before it is honored by ScITE Go (F5) as well. Check the console output:

>Running:(3.3.8.1):C:Program Files (x86)AutoIt3autoit3_x64.exe "C:TempTest.au3"

Edit: BrewManNH was quicker!

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
SpinningCone

got it working, though still not sure why it's needed the wrapper code seems to indicate that it should pick 64 bit automatically, weird

Share this post


Link to post
Share on other sites
BrewManNH

When you installed AutoIt, did you specify "Using native x64 tools by default"? Or did you opt for the x86 option when you installed it? There's a registry setting that controls which is the default that AutoIt3Wrapper looks for, and if it's set to the x86 tools, it's only going to run the x64 tools if you specify it in the wrapper directives in your script.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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  

×