Powershell不会在我的脚本中使用exit关闭

时间:2015-07-02 21:06:58

标签: powershell

这是我的代码。前两个选项有效,但不是第三个。我不确定我做错了什么。我是powershell的新手。感谢。

 
$opt = Read-Host "Type 1 to login | Type 2 to disconnect | Type 3 to Exit"
write-host $opt

If ($opt -eq '1') {
$user = "myusername"

$UserCredential = Get-Credential -credential $user

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

Import-PSSession $Session
}

ElseIf ($opt -eq '2') {
Get-PSSession | Remove-PSSession
} 

ElseIf ($opt -eq '3') {
Exit
}

1 个答案:

答案 0 :(得分:1)

如果您确实想要关闭shell而不是使用exit,请改用它:

$Host.SetShouldExit(0)

感谢@PetSerAl指出此选项可以正常关闭PowerShell进程,而不是仅使用Stop-Process $pid将其终止。

顺便说一下,任何碰到你的剧本的人都会发现你关闭他们的shell很粗鲁。也许3应该只是exit script而4是exit and close shell

相关问题