0

I'm currently trying to write a skript that'll automatically connect to one of our exchange servers to open a exchange management shell remotely. This is the script I have written so far:

$servers = "ex1.domain.local", "ex2.domain.local"
$server = $servers[(Get-Random -Maximum $servers.count)]
$credential = Get-Credential domain\administrator  -Message "Enter your login credentials"
write-host "Connecting remote PowerShell to $($server)..."
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://$server/PowerShell/ -Credential $credential
New-PSSession -Session $session -DisableNameChecking -AllowClobber

The script seems to run just fine but unfortunately the powershell console window immediately closes after New-PSSession -Session $session -DisableNameChecking -AllowClobber. Whenever I paste these command into a powershell script seperately it works just fine.

How can I alter this script to not automatically close the console window after importing a powershelkl session?

farosch
  • 401

3 Answers3

0

You need to call the script with the -noexit switch. I personally use an .bat file to call a powershell.exe process with the switch.

Example:

powershell -noexit "C:\Scripts\PSSession_Exchange\ExchangePowerShell.ps1"

Run the .bat file, and you're good to go.

doenoe
  • 1,183
0

About the code issue, I suggest you post it on the Microsoft TechNet Exchange Sever Development forum.

Kelvin_D
  • 226
  • 1
  • 3
0

I use Read-Host "Press Enter" which will wait for the user input of the Enter key to finish running.

nvlddmkm
  • 13
  • 1
  • 4