最後に、外部 IDE を使用する必要はありませんでした。標準の PowerShell コードでうまくいきました。埋め込みコード!:) ScriptBlock が鍵でした。
$sb = {
$query = 'SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE TargetInstance ISA ''Win32_LogicalDisk'' AND TargetInstance.DriveType=2'
Register-WmiEvent -Query $query -SourceIdentifier RemovableDiskDetection -Action {
$class = $eventArgs.NewEvent.__CLASS
$device = $eventArgs.NewEvent.TargetInstance.DeviceID
$wshell = New-Object -ComObject Wscript.Shell
switch ($class)
{
__InstanceCreationEvent {
$path = $device + '\flag\'
Write-Host '*** Checking the existence of the file $path'
if (Test-Path -Path $path)
{
$wshell.Popup('Inserted, device id: $device WITH flag', 0, 'Done', 0x1)
}
else
{
$wshell.Popup('Inserted, device id: $device WITHOUT flag', 0, 'Done', 0x1)
}
}
__InstanceDeletionEvent {
$wshell.Popup('Removed, device id: $device ', 0, 'Done', 0x1)
}
}
}
}
start-process powershell.exe -argument "-noexit -nologo -noprofile -windowstyle hidden -command $sb"
この素晴らしい回避策の後、PS2EXEツールを使用してコンパイルしました。
.\ps2exe.ps1 -noConsole -inputFile .\magic.ps1 -outPutFile magic.exe