Jump to content

Calling PowerShell from Autoit


MattZ
 Share

Recommended Posts

I've got an issue that I think is kind of strange. I am calling PowerShell from Autoit and having write some output to a csv file. My AutoIt script works perfectly as long as I am running it from somewhere on the C drive of the server. I move my AutoIT script to a mapped network drive and my powershell script no longer creates the output file. Has anyone seen this? Have any ideas?

Thanks

Link to comment
Share on other sites

I've got an issue that I think is kind of strange. I am calling PowerShell from Autoit and having write some output to a csv file. My AutoIt script works perfectly as long as I am running it from somewhere on the C drive of the server. I move my AutoIT script to a mapped network drive and my powershell script no longer creates the output file. Has anyone seen this? Have any ideas?

Thanks

You may want to post your code ;) so someone can see what the potential fault is

Link to comment
Share on other sites

AutoIT Script

$LaunchExchangePowershell = "C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile " & '"C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1"  -command ' & ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'"

$Cmd = @ScriptDir & "\Exchange\sizes.ps1"

RunWait($LaunchExchangePowershell & "; " & $Cmd, "", @SW_HIDE)

Powershell Script

$exchangeservers = Get-ExchangeServer |where-object {$_.admindisplayversion.major -eq 8 -and $_.IsMailboxServer -eq $true }
$AllServers = @()
foreach ($server in $exchangeservers)
{
    $db = Get-MailboxDatabase -server $server
    foreach ($objItem in $db)
    {
    $edbfilepath = $objItem.edbfilepath
    $path = "`\`\" + $server + "`\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2)
    $dbsize = Get-ChildItem $path
    $start = $path.LastIndexOf('\')
    $dbpath = $path.Substring($start +1).remove($path.Substring($start +1).length -4)
    $mailboxpath = "$server\$dbpath"
    $mailboxcount = Get-MailboxStatistics -database "$mailboxpath" |measure-object
    $ReturnedObj = New-Object PSObject
    $ReturnedObj | Add-Member NoteProperty -Name "Server\StorageGroup\Database" -Value $objItem.Identity
    $ReturnedObj | Add-Member NoteProperty -Name "Size (GB)" -Value ("{0:n2}" -f ($dbsize.Length/1048576KB))
    $AllServers += $ReturnedObj
    }
}

$AllServers |export-csv C:\Temp\sizes.csv -notype -force
Exit

So what this should do is launch the exchange PowerShell command line, then run the sizes.ps1 script that goes and grabs the db sizes from all the exchange servers. It should then write sizes.csv to C:\Temp. This works fine as long as the AutoIT script is running from the C drive of the server, when I move it to a mapped network drive it quits creating the csv file.

Link to comment
Share on other sites

Hi Mattz

Are you executing the Autoit script from the server side which resides on a folder share? and does the server you are executing from have "@ProgramFilesDir & '\Microsoft\Exchange Server\bin\Exchange.ps1" and exshell.psc1

just try this you may need to adjust it place some msgbox() in the script to see if the proper syntax is being passed to call for the ps scripts; unfortunate I do not have access to a machine where M$PS is installed and configured.

$LaunchExchangePowershell = @SystemDir & '\windowspowershell\v1.0\powershell.exe -PSConsoleFile'
$exshell_psc1 = @ProgramFilesDir & 'Microsoft\Exchange Server\bin\exshell.psc1 -command' & ' ' & '.' & ' ' & @ProgramFilesDir & '\Microsoft\Exchange Server\bin\Exchange.ps1'

$sizes_ps1 = @ScriptDir & "\Exchange\sizes.ps1"

Run(@ComSpec & ' /c ' & $LaunchExchangePowershell & ' ' & $exshell_psc1 & ' ' & $sizes_ps1, @SystemDir, @SW_HIDE)

In some instances you do not need to to use absolute paths to powershell.exe as it is configured through the operating systems Environment variables, this means you could just execute "ps" from anywhere within the OS.

Edited by failedtocompile
Link to comment
Share on other sites

I was hoping to have my script have no install on a server, but I've decided to just leave the auitit.exe, a config file; that lists the location of the share, and the exchange powershell script folder. This works without issues. I already have to install a bunch of stuff anywhere I run the script from as I need powershell, exchange management console, etc.

I also asked on some powershell forums and they mentioned it could be a double-hop authentication issue. Looks like a huge pain to work around that.

Thanks for the input.

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