Jump to content

run() behaving unexpectedly


Recommended Posts

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.

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...