Here are a script, that you can include in yours script, to add Exchange management shell, to “normal” powershell scripts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$StopWatch = [System.Diagnostics.StopWatch]::StartNew() Function Test-Command ($Command) { Try { Get-command $command -ErrorAction Stop Return $True } Catch [System.SystemException] { Return $False } } IF (Test-Command "Get-Mailbox") {Write-Host "Exchange cmdlets already present"} Else { $CallEMS = ". '$env:ExchangeInstallPath\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto -ClientApplication:ManagementShell " Invoke-Expression $CallEMS $stopwatch.Stop() $msg = "`n`nThe script took $([math]::round($($StopWatch.Elapsed.TotalSeconds),2)) seconds to execute..." Write-Host $msg $msg = $null $StopWatch = $null } |