我想了解有关switch语句以及如何实现它的更多信息。我有以下原始代码来设置页面文件。我想在“Write-Host Windows 2008 edition detected”之后更改if和elseif条件来切换案例。
是否可以使用Switch语句执行此操作?我已经尝试了不同的公式,但仍然找不到完美的代码。
这是代码。
$OSbuild = [environment]::OSVersion.Version.Build ## Checking the OS version
$Drives = ".\Drives.txt"
$Cdrive = "C: Hard Drive"
$Ddrive = "D: Hard Drive"
$Pdrive = "P: Hard Drive"
$OthDrive1 = "[^DP]: Hard Drive"
$OthDrive2 = "[^CDP]: Hard Drive"
$Disk = Get-WmiObject win32_logicaldisk
Foreach ($Drive in $Disk)
{
Switch ($Drive.DriveType)
{
1{$Disks = $Drive.DeviceID + " Unknown"; Break}
2{$Disks = $Drive.DeviceID + " Floppy or Removable Drive"; Break}
3{$Disks = $Drive.DeviceID + " Hard Drive"; Break}
4{$Disks = $Drive.DeviceID + " Network Drive"; Break}
5{$Disks = $Drive.DeviceID + " CD"; Break}
6{$Disks = $Drive.DeviceID + " RAM Disk"; Break}
}
Write $Disks >> $Drives
}
if (Get-Content $Drives | ? {$_ -like $Pdrive})
{
Proces A
}
elseif ($OSbuild -eq 6001 -or 7601)
{
Write-Host "Windows 2008 edition detected."
$OSversion = "2008"
if (Get-Content $Drives | ? {$_ -like $Ddrive}) ## Multiple drives found (with D) in the server
{
Process B
}
elseif (Get-Content $Drives | ? {$_ -match $OthDrive2}) ## Multiple drives found (without D) in the server
{
Process C
}
elseif (Get-Content $Drives | ? {$_ -like $Cdrive -notmatch $OthDrive1}) ## Only C drive found in the server
{
Process D
}
else
{
Write "Couldn't find the paging file rule for this server!"
}
}