Jump to content

How to integrate powershell script with AutoIT


Recommended Posts

Looking to integrate a powershell script that has commands for building a mailbox but I am not sure how to get the variables I have in AutoIT to translate over to the script:

Under comment create mailbox, I want to call the powershell script to come up and create the mailbox for the users who are defined by $sUser and $data

Also, how do I get the code to pull the firstname.lastname of the user from AD to correctly create the SIP account? Or do I have to create a separate field that would require entering the user's First name and Last name?

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <AD14100.au3>
#include <MsgBoxConstants.au3>

If @error Then Exit MsgBox($MB_ICONERROR, "Encountered an Error", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Auto Prehire Tool", 288, 438, 2295, 257)
$EmployeeID = GUICtrlCreateInput("EmployeeID", 64, 104, 129, 21)
$Radio1 = GUICtrlCreateRadio("Check for F drive Creation", 48, 216, 185, 17)
$Database = GUICtrlCreateInput("Database", 32, 160, 209, 21)
$Button1 = GUICtrlCreateButton("Create Employee", 72, 320, 163, 57)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
;Press Button
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
        $sUser=GUICTrlRead($EmployeeID)
        $data=GUICTrlRead($Database)
        $mailboxdata="-alias '"&$sUser&"' -database '"&$data&"'"
; Let the user confirm the data before creating the mailbox
        If MsgBox($MB_YESNOCANCEL, "Confirm Information", "Are you sure that you want to connect to" & @CRLF & "  " & $data & @CRLF & "and create a mailbox for user" & @CRLF & "  " & $sUser) <> $IDYES Then Exit
; Create the mailbox
        
; Add SWA Logon script in AD
            $CMD = "dsquery user -samid "&$sUser&"| dsmod user -loscr SWA"
            RunWait(@ComSpec & " /c " & $CMD)
; Create F drive
        if GUICtrlRead($Radio1) = 1 Then
;~          $CMD = "dsquery user -samid "&$id&"| dsmod user -hmdir \\sdcpflr002\"&$id&" -hmdrv F:"
            RunWait("\\disk24\tech_svcs-shared\DST\Tools\HomeDirBld\DE-HomeDirectoryBuilder.exe")
        EndIf
        Sleep(5000)
        MsgBox (0, "Pre-Hire Complete!", "The Account has been successfully created, if F drive was selected, please wait for other confirmation box before continuing!")
    EndSwitch
WEnd

 

Powershell Script:

Enable-Mailbox -Identity $sUser -alias $sUser -database $data

Set-Mailbox $sUser -EmailAddresses SIP:$firstname.$lastname@wnco.com

Set-CASMailbox $sUser -PopEnabled $False
Set-CASMailbox $sUser -ImapEnabled $False

 

Link to comment
Share on other sites

I have managed to fix most of this but how do I actually execute the PS file after it writes to it then opens up the Exchange Management Shell?

 

 

#RequireAdmin
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <AD14100.au3>
#include <MsgBoxConstants.au3>

_AD_Open()

If @error Then Exit MsgBox($MB_ICONERROR, "Encountered an Error", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Auto Prehire Tool", 288, 438, 2295, 257)
$EmployeeID = GUICtrlCreateInput("EmployeeID", 64, 104, 129, 21)
$Radio1 = GUICtrlCreateRadio("Check for F drive Creation", 48, 216, 185, 17)
$Database = GUICtrlCreateInput("Database", 32, 160, 209, 21)
$Button1 = GUICtrlCreateButton("Create Employee", 72, 320, 163, 57)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
;Press Button
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
        $sUser=GUICTrlRead($EmployeeID)
        $data=GUICTrlRead($Database)
        $mailboxdata="-alias '"&$sUser&"' -database '"&$data&"'"
; Let the user confirm the data before creating the mailbox
    If MsgBox($MB_YESNOCANCEL, "Confirm Information", "Are you sure that you want to connect to" & @CRLF & "  " & $data & @CRLF & "and create a mailbox for user" & @CRLF & "  " & $sUser) <> $IDYES Then Exit
; Create the mailbox
        $filename="psfile.ps1"
        $file2=FileOpen(@ScriptDir&'\'&$filename,2)
        $command = ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; 'c:\Matt\psfile.ps1'"
;~      MsgBox(0, "error text", 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -version 2.0 -noexit -command "'&$command&'"')
        FileWriteLine($file2, "Enable-Mailbox -Identity "&$sUser &" -alias "&$sUser &" -database "&$data)
        $sn=_AD_GetObjectAttribute($sUser, "sn")
        $givenname=_AD_GetObjectAttribute($sUser, "givenName")
        FileWriteLine($file2, "Set-Mailbox "&$sUser &" -EmailAddresses SIP:"&StringStripWS($givenname,8)&"."&StringStripWS($sn,8)&"@wnco.com")
        FileWriteLine($file2, "Set-CASMailbox "&$sUser &" -PopEnabled $False")
        FileWriteLine($file2, "Set-CASMailbox "&$sUser &" -ImapEnabled $False")
        Run('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -version 2.0 -noexit -command "'&$command&'"')
; Add SWA Logon script in AD
        $CMD = "dsquery user -samid "&$sUser&"| dsmod user -loscr SWA"
        RunWait(@ComSpec & " /c " & $CMD)
; Create F drive
    if GUICtrlRead($Radio1) = 1 Then
;~      $CMD = "dsquery user -samid "&$id&"| dsmod user -hmdir \\sdcpflr002\"&$id&" -hmdrv F:"
        RunWait("\\disk24\tech_svcs-shared\DST\Tools\HomeDirBld\DE-HomeDirectoryBuilder.exe")
    EndIf
        Sleep(5000)
;~      MsgBox (0, "Pre-Hire Complete!", "The Account has been successfully created, if F drive was selected, please wait for other confirmation box before continuing!")
    EndSwitch
WEnd

 

Link to comment
Share on other sites

Is there a reason why you created a second thread asking the same question?

 

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

Yes that previous one was how to create a mailbox using the Active Directory UDF which I can't use because of no access to the servers

So I had to switch gears and just create a PS1 file to run which is where I am stuck, getting the PS1 file to execute after it connects to Exchange

Link to comment
Share on other sites

We used the following code snippet to create mailboxes:

$sCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command . " & _
            "'D:\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Enable-Mailbox -Identity " & _
            $sUserID & " -Alias " & $sUserID
    $pid = Run($sCMD, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
    StdinWrite($pid, @CRLF)
    StdinWrite($pid)
    ; STDOUT
    $sSTDOUT = ""
    While 1
        $sOutput = StdoutRead($pid)
        If @error Then ExitLoop
        If $sOutput <> "" Then $sSTDOUT = $sSTDOUT & @CRLF & $sOutput
    WEnd
    ; STDERR
    $sSTDERR = ""
    While 1
        $sOutput = StderrRead($pid)
        If @error Then ExitLoop
        If $sOutput <> "" Then $sSTDERR = $sSTDERR & @CRLF & $sOutput
    WEnd
    If StringStripWS($sSTDERR, 3) = "" Then
        _FileWriteLog($sLogFile, "I (" & @AutoItPID & ") " & $sUserID & ": Successfully created mailbox.")
    Else
        Exit _FileWriteLog($sLogFile, "Error when creating Exchange mailbox!" & @CRLF & $sSTDERR)
    EndIf

But you need to locally install the Exchange components (here: D:\Exchange Server\...)

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

Thanks this works,

One issue I am running into, and maybe you know how to fix, is I have it to where my program automatically creates a SIP account but it doesn't know how to take into account if there is someone already with the same name in the company.

For example if I create an account for John Smith in my program, it needs to know if there is another John Smith in the company. It already creates John.Smith2@domain.com but it needs to know to add the same as a SIP account. Currently I have the exchange commands create the SIP account automatically but it does not account for the (2) or whatever number depending on how many John Smiths are already in the company.

Link to comment
Share on other sites

Should be easy. Use _AD_GetObjectsInOU and check for the name fields (displayname or givenName and sn).

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

So I currently have:

$sn=_AD_GetObjectAttribute($sUser, "sn")
$givenname=_AD_GetObjectAttribute($sUser, "givenName")
FileWriteLine($file2, "Set-Mailbox "&$sUser &" -EmailAddresses SIP:"&StringStripWS($givenname,8)&"."&StringStripWS($sn,8)&"@wnco.com")

This creates a SIP account but it just creates the account based on their first name and last name and does not include the 2 or 3 or whatever if they are the 2nd or 3rd John Smith in the company. So it creates the mailbox john.smith2@domain.com but still only creates the SIP as john.smith@domain .com which could cause problems with our communication tools like Jabber or WebEx. What am I missing here?

Link to comment
Share on other sites

I would use sn and GivenName of the user you want to create and call _AD_GetObjectsInOU to search for all the users with the same sn and GivenName. Then extract the SIP of all the returned records and calculate the max used number. Increment by one and create the new SIP.

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

$aObjects = _AD_GetObjectsInOU("", "(&(objectcategory=person)(objectclass=user)(sn=" & $sSN & ")(givenName=" & $sGivenName & "))", 2, "sAMAccountName,displayname")

 

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

Alright, I was able to get this semi working using _AD_GetObjectAttribute

Not sure if this is roughly the same as what you are going with. It pretty much works flawlessly the only problem is a delay. I have to wait for the mailbox to be created,

wait about a minute, then run the command to change the SIP based on what the user's email address is but I don't see any other way around that anyways.

 

$filename="psfile.ps1"
        $filename2="SIPfile.ps1"
        $file2=FileOpen(@ScriptDir&'\'&$filename,2)
        $file3=FileOpen(@ScriptDir&'\'&$filename2,2)
        $command = ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; .\psfile.ps1;"
        $command2 = ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; .\SIPfile.ps1;"
;~      MsgBox(0, "error text", 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -version 2.0 -noexit -command "'&$command&'"')
        FileWriteLine($file2, "Enable-Mailbox -Identity "&$sUser &" -alias "&$sUser &" -database "&$data)
        $sn=_AD_GetObjectAttribute($sUser, "sn")
        $givenname=_AD_GetObjectAttribute($sUser, "givenName")
        FileWriteLine($file2, "Set-CASMailbox "&$sUser &" -PopEnabled $False")
        FileWriteLine($file2, "Set-CASMailbox "&$sUser &" -ImapEnabled $False")
        FileClose($file2)
        Run('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -version 2.0 -noexit -command "'&$command&'"')
        Sleep(100000)
        $mail=_AD_GetObjectAttribute($sUser,"mail")
        FileWriteLine($file3, "Set-Mailbox "&$sUser &" -EmailAddresses SIP:"&$mail)
        FileClose($file3)
        Run('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -version 2.0 -noexit -command "'&$command2&'"')

 

Link to comment
Share on other sites

The following script checks for users with the same name and givenname.
You can then process the array to identify the mail addresses already in use.

#include <AD.au3>

_AD_Open()
Global $sName = "Doe"
Global $sGivenName = "John"
Global $aObjects = _AD_GetObjectsInOU("", "(&(objectcategory=person)(objectclass=user)(sn=" & $sName & ")(givenName=" & $sGivenName & "))", 2, "sAMAccountName,distinguishedname,sn,givenName,mail")
_ArrayDisplay($aObjects)
_AD_Close()

 

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

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...