我想创建一个脚本,检测并延迟Windows 10 PC上的关闭,并在关机时插入USB-Stick时显示消息。
我的问题是我的脚本没有停止关机。我对Powershell很新。为我的英语而烦恼。
这就是我得到的:
[System.Reflection.Assembly]::Load("System.Management, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$Man = New-Object System.Management.ManagementClass("Win32_USBHub")
[System.Management.ManagementObjectCollection]$Devs = $Man.GetInstances()
$global:shuttingDown = $false
$sysevent = [microsoft.win32.systemevents]
Register-ObjectEvent -InputObject $sysevent -EventName "SessionEnding" -Action {$global:shuttingDown = $true; Write-Host " SessionEnding"}
Register-ObjectEvent -InputObject $sysevent -EventName "SessionEnded" -Action {$global:shuttingDown = $true; Write-Host " SessionEnded"}
do {
Start-Sleep -Seconds 1
if ($global:shuttingDown) {
& "$env:SystemRoot\System32\Shutdown.exe" -a ;
$Devs | ForEach-Object {
if($_.GetPropertyValue("Name").ToString() -eq "USB-Massenspeichergerät"){
Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::Ok
$MessageboxTitle = “USB-Stick gefunden”
$Messageboxbody = “Es wurde ein USB-Stick gefunden. Nicht vergessen mitzunehmen ;)”
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
[System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$messageicon)
Write-Output ""
Write-Output $_.GetPropertyValue("Name").ToString()
Write-Output ""
}
$Devs.Dispose()
$Man.Dispose()
}
}
Write-Host " Waiting for Shutdown"
} while ($true)
thx求助
答案 0 :(得分:0)
所以基本上,如果$ global:shuttingDown保持值,那么使用 Break
do {
Start-Sleep -Seconds 1
if ($global:shuttingDown) {break}
Write-Host " Waiting for Shutdown"
} while ($true)