powershell - 对话框不弹出
问题描述
我正在编写一个脚本,我得到了一个确认弹出框的帮助,问题是它在正确的条件下没有弹出(带有 /sources/install3.swm 的 USB 驱动器)。
以下是我的整个代码(因为是条件表示执行整个其余代码)。没有弹出的有问题的代码是工厂密钥检查:
# 1. Copy the wim
# 2. Mount the wim
# 3. Inject all the drivers from the driver path recursively
# 4. Commit and unmount the wim
Param(
# Legacy Path: $DriverPath = "\\desmo\release\ArubaImages\Daily\developer_teamos\master\2.4047.0\Image\Support"
# Factory Path:
[string]$DriverPath = "\\desmo\release\ArubaImages\Factory\customer\master\4.4074.0\Support\13.IMAGE"
)
# Check if a USB drive is inserted in the local computer.
do {
$USB = gwmi win32_diskdrive |
?{$_.interfacetype -eq "USB"} |
%{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.Replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} |
%{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} |
%{$_.deviceid}
if ($USB -eq $null) {
#Write-Host "There is no USB drive detected, please insert a USB drive"
#Popup to pause to give user time to enter a USB key
$Shell = New-Object -ComObject "WScript.Shell"
$Button = $Shell.Popup("Please insert proper USB key and click OK to continue.", 0, "There is no USB drive detected", 0)
}
} while ($USB -eq $null)
#Check if more than one USB drive is inserted in local computer.
do {
if ($USB.Count -gt 1) {
#Popup to pause to give time to correct USB key
$Shell = New-Object -ComObject "WScript.Shell"
$Button = $Shell.Popup("Please insure only the proper key is inserted.", 0, "There is more than one USB drive detected", 0)
}
$USB2 = gwmi win32_diskdrive |
?{$_.interfacetype -eq "USB"} |
%{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.Replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} |
%{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} |
%{$_.deviceid}
} while ($USB2.Count -gt 1)
#Check if this is the right key
do {
if (!(Test-Path $USB2\sources\install*.swm)) {
#Popup to pause to give time to correct USB key
$Shell = New-Object -ComObject "WScript.Shell"
$Button = $Shell.Popup("Ooops! You got the wrong key insterted. Please insert the proper key.", 0, "Wrong key insterted", 0)
}
$USB3 = gwmi win32_diskdrive |
?{$_.interfacetype -eq "USB"} |
%{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.Replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} |
%{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} |
%{$_.deviceid}
} while (!(Test-Path $USB3\sources\install*.swm))
#Quit script if install.wim is not in folder
if (!(Test-Path .\install.wim) ) {
Write-Warning "install.wim is not in source folder! Press any key to exit."
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Stop-Process -Id $PID #I use stop-process because some reason "exit" isn't working for me.
}
#Factory Key Check
$USB4 = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_.deviceid}
# You need this to use System.Windows.MessageBox
Add-Type -AssemblyName 'PresentationFramework'
# By default, you're not blasting the USB drive
$continue = 'No'
# If you see the file, prompt the user
if ( $(Test-Path -Path "$USB4\sources\install3.swm") ) {
$caption = "*** IT APPEARS THIS IS A FACTORY KEY ***"
$message = "Are you Sure You Want To Proceed:"
$continue = [System.Windows.MessageBox]::Show($message, $caption, 'YesNo');
}
if ($continue -eq 'Yes') {
# The user said "yes" to the "do you prompt"
#Continue on with regular script
$tempDir = ".\"
#Write-Host "Temp dir: ", $tempDir
$copiedWimPath = (Join-Path $tempDir "install.wim")
#Write-Host "USB key is no longer assumed to be D:, it can be any letter - but having more than one USB key in computer will confuse this script. The USB key(s) currently attached is/are ($USB)" -ForegroundColor red -BackgroundColor white
#Write-Host "You may correct any USB key problems (remove/change) until after splitting the image file:",$copiedWimPath -ForegroundColor red -BackgroundColor white
#Write-Host "Temp dir: ", $tempDir
$mountDir=(Join-Path "C:\MountOnwardWim" "mount")
#Create the mount directory
New-Item -ItemType directory -Path $mountDir -ErrorAction SilentlyContinue
#Mount wim
Write-Host "Mounting the image to ",$mountDir
Mount-WindowsImage -Path $mountDir -ImagePath $copiedWimPath -Index 1
#Inject all the drivers
Write-Host "Inject the drivers",$mountDir " - This may take awhile, please be patient"
Add-WindowsDriver -Path $mountDir -Driver $DriverPath -LogLevel WarningsInfo -Recurse
#Commit and unmount the wim
Write-Host "Dismounting the image from",$mountDir
Dismount-WindowsImage -Path $mountDir -Save
$USB5 = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_.deviceid}
$usbDir = "$USB5\sources"
$splitSwmFilePath = (Join-Path $usbDir "install.swm")
Write-Host "Removing old SWMs from USB key"
Remove-Item $USB5\sources\install*.swm
Write-Host "Splitting the image file: ",$copiedWimPath " - This may take awhile, please be patient"
Split-WindowsImage -ImagePath $copiedWimPath -SplitImagePath $splitSwmFilePath -FileSize 3072 -CheckIntegrity
#Write-Host "Moving split image files to USB key - This may take awhile, please be patient"
#Move-Item -Path C:\OnwardInjecting\install*.swm -Destination $USB4\sources
Write-Host "Removing install.wim from PC"
Remove-Item C:\OnwardInjecting\install.wim
Write-Host "Cleaning up DISM"
dism /cleanup-wim
Read-Host "All done! Press any key to exit"
Stop-Process -Id $PID
} else {
# The user said "no" to the flash prompt
Write-Output "Terminating process..."
Start-Sleep 1
}
解决方案
推荐阅读
- c# - 创建可供VB使用的C# WPF DLL
- swift - 使用 performSegue 视图时停止 webview 重新加载
- linux - 通过 SSH 和 FTP 安装包管理器
- c# - ASP.NET Core 2.1 中的远程验证值未传递给控制器
- ruby-on-rails - ActiveRecord 不再在数组中查找重复的 ID 并保存为拥有和属于多的关系,Rails 4.2.8
- sql-server - 获取列中未选择的单词
- blob - Web 应用程序的对象存储
- file-upload - GraphQL 文件上传请求应该是什么样的?
- javascript - 如何使用 AmCharts 创建自定义地图
- c# - 字典小写和大写c#