退出PowerShell菜单

时间:2017-03-31 09:40:41

标签: powershell

我已经创建了一个powershell菜单脚本,但偶尔我不想使用我创建的菜单功能我想出来并运行自定义命令,甚至更好地添加一个菜单功能,它允许我写一个自定义命令。

有人能帮我解决这个问题吗?

write-host  "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" 
write-host          "Exchange Online Management"                                 
write-host                                                            
write-host          "Press 1: To assign Full Access To A Mailbox"              
write-host          "Press 2: Get Mailbox Size"
write-host          "Press 3: Custom Exchange Command"
write-host          "Press Q: To Leave Exchange Management"                                     
write-host
write-host
write-host "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
$SM2 = Read-Host "Please Make A Selection"
Switch ($SM2) {
'1'{ 
DO something
}
'2' {
$Mailboxuser = read-host "Who's Mailbox are you trying to query?"
Get-MailboxStatistics $Mailboxuser | ft DisplayName, TotalItemSize, ItemCount
}
'3' {
return
}
'Q' {
Remove-PSSession $Session
return
}
}
}
until ($SM -eq 'Q')

1 个答案:

答案 0 :(得分:0)

PowerShell控制台窗口在完成脚本执行后即将关闭。

您可以使用脚本运行另一个PowerShell流程来解决这个问题:

'Q' {
Remove-PSSession $Session    
& powershell
}

另一个选项是创建脚本的快捷方式并添加-NoExit开关,以便在脚本完成时停止关闭窗口。

PowerShell -NoExit "C:\folder\script.ps1"