在 Powershell 中将字符串变量作为参数传递
Passing string variable as Argument in Powershell
我正在尝试编写一个 PowerShell 脚本来将 I: 的驱动器盘符更改为其他内容。这是脚本。
$driveI = Get-WmiObject -Class win32_volume -Filter "DriveLetter='I:'"
if ($driveI.SerialNumber=""){
write-host "I: is free..."
} else {
write-host "I: is occupied"
foreach ( $s in @("'Z:'", "'Y:'", "'X:'", "'W:'", "'V:'", "'U:'", "'T:'", "'R:'", "'Q:'", "'P:'", "'O:'", "'N:'", "'M:'", "'L:'", "'K:'", "'J:'", "'H:'", "'G:'", "'F:'", "'E:'", "'D:'", "'B:'", "'A:'"))
{
$testdrv = Get-WmiObject -Class win32_volume -Filter "DriveLetter=$s"
if (!$testdrive.Exist)
{
$s = '"'+$s.Trim([char]0x0027)+'"'
Set-WmiInstance -input $driveI -Arguments @{DriveLetter=$s}
Write-Host I: has been moved to $s
break
}
}
}
请允许我向您介绍一下脚本。 $driveI
用于检索有关 I: 的所有信息。现在,如果 I: 没有序列号,则表明 I: 不存在。
另一方面,如果 I: 存在,我正试图找到一个可用的驱动器号。我正在使用 foreach
循环执行我的搜索。现在,当我们调用 Get-WmiObject
时,我们使用带单引号的驱动器号。但是当我们使用 Set-WmiInstance
时,我们使用双引号。但是,即使我将 $s
修改为用双引号括起来,它也不起作用。
简而言之,如果我使用 Write-Host $s
,我得到的输出是 "E:"
。当我使用 Set-WmiInstance -input $driveI -Arguments @{DriveLetter="E:"}
时,它起作用了。但是当我使用 Set-WmiInstance -input $driveI -Arguments @{DriveLetter=$s}
时,它不起作用。谁能告诉我我做错了什么?
无论如何,当您稍后要删除引号时,不要在字符串中加上引号。而是在您实际需要的地方添加引号:
$driveLetters = 'Z:', 'Y:', 'X:', ..., 'D:', 'B:', 'A:'
foreach ( $s in $driveLetters ) {
$testdrv = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter='$s'"
if (-not $testdrv) {
Set-WmiInstance -input $driveI -Arguments @{DriveLetter=$s}
Write-Host "I: has been moved to $s"
break
}
}
有点笨拙,但这段代码有效。我会采纳你的建议,摆脱 Trim
混乱局面。
$driveI = Get-WmiObject -Class win32_volume -Filter "DriveLetter='I:'"
if ($driveI -eq $null) {
write-host "I: is free..."
} else {
write-host "I: is occupied..."
foreach ( $s in @("'Z:'", "'Y:'", "'X:'", "'W:'", "'V:'", "'U:'", "'T:'", "'R:'", "'Q:'", "'P:'", "'O:'", "'N:'", "'M:'", "'L:'", "'K:'", "'J:'", "'H:'", "'G:'", "'F:'", "'E:'", "'D:'", "'B:'", "'A:'"))
{
$testdrv = Get-WmiObject -Class win32_volume -Filter "DriveLetter=$s"
if ($testdrv -eq $null)
{
$s = $s.Trim([char]0x0027)
Set-WmiInstance -input $driveI -Arguments @{DriveLetter=$s}
Write-Host I: has been moved to $s
break
}
}
}
我正在尝试编写一个 PowerShell 脚本来将 I: 的驱动器盘符更改为其他内容。这是脚本。
$driveI = Get-WmiObject -Class win32_volume -Filter "DriveLetter='I:'"
if ($driveI.SerialNumber=""){
write-host "I: is free..."
} else {
write-host "I: is occupied"
foreach ( $s in @("'Z:'", "'Y:'", "'X:'", "'W:'", "'V:'", "'U:'", "'T:'", "'R:'", "'Q:'", "'P:'", "'O:'", "'N:'", "'M:'", "'L:'", "'K:'", "'J:'", "'H:'", "'G:'", "'F:'", "'E:'", "'D:'", "'B:'", "'A:'"))
{
$testdrv = Get-WmiObject -Class win32_volume -Filter "DriveLetter=$s"
if (!$testdrive.Exist)
{
$s = '"'+$s.Trim([char]0x0027)+'"'
Set-WmiInstance -input $driveI -Arguments @{DriveLetter=$s}
Write-Host I: has been moved to $s
break
}
}
}
请允许我向您介绍一下脚本。 $driveI
用于检索有关 I: 的所有信息。现在,如果 I: 没有序列号,则表明 I: 不存在。
另一方面,如果 I: 存在,我正试图找到一个可用的驱动器号。我正在使用 foreach
循环执行我的搜索。现在,当我们调用 Get-WmiObject
时,我们使用带单引号的驱动器号。但是当我们使用 Set-WmiInstance
时,我们使用双引号。但是,即使我将 $s
修改为用双引号括起来,它也不起作用。
简而言之,如果我使用 Write-Host $s
,我得到的输出是 "E:"
。当我使用 Set-WmiInstance -input $driveI -Arguments @{DriveLetter="E:"}
时,它起作用了。但是当我使用 Set-WmiInstance -input $driveI -Arguments @{DriveLetter=$s}
时,它不起作用。谁能告诉我我做错了什么?
无论如何,当您稍后要删除引号时,不要在字符串中加上引号。而是在您实际需要的地方添加引号:
$driveLetters = 'Z:', 'Y:', 'X:', ..., 'D:', 'B:', 'A:'
foreach ( $s in $driveLetters ) {
$testdrv = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter='$s'"
if (-not $testdrv) {
Set-WmiInstance -input $driveI -Arguments @{DriveLetter=$s}
Write-Host "I: has been moved to $s"
break
}
}
有点笨拙,但这段代码有效。我会采纳你的建议,摆脱 Trim
混乱局面。
$driveI = Get-WmiObject -Class win32_volume -Filter "DriveLetter='I:'"
if ($driveI -eq $null) {
write-host "I: is free..."
} else {
write-host "I: is occupied..."
foreach ( $s in @("'Z:'", "'Y:'", "'X:'", "'W:'", "'V:'", "'U:'", "'T:'", "'R:'", "'Q:'", "'P:'", "'O:'", "'N:'", "'M:'", "'L:'", "'K:'", "'J:'", "'H:'", "'G:'", "'F:'", "'E:'", "'D:'", "'B:'", "'A:'"))
{
$testdrv = Get-WmiObject -Class win32_volume -Filter "DriveLetter=$s"
if ($testdrv -eq $null)
{
$s = $s.Trim([char]0x0027)
Set-WmiInstance -input $driveI -Arguments @{DriveLetter=$s}
Write-Host I: has been moved to $s
break
}
}
}