简体   繁体   English

在Powershell中将字符串变量作为Argument传递

[英]Passing string variable as Argument in Powershell

I'm trying to write a PowerShell script to change drive letter of I: to something else. 我正在尝试编写PowerShell脚本来更改I的驱动器号:其他内容。 Here's the script. 这是脚本。

$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
        }
    }
}

Allow me to walk you through the script. 请允许我引导您完成脚本。 $driveI is used to retrieve all information regarding I:. $driveI用于检索有关I:的所有信息。 Now, if I: has no serial number, it indicates that I: doesn't exist. 现在,如果我:没有序列号,则表示我:不存在。 If, on the other hand, I: exists, I'm trying to find a drive letter which is free. 另一方面,如果我:存在,我正在寻找一个免费的驱动器号。 I'm implementing my search with the foreach loop. 我正在使用foreach循环实现我的搜索。 Now, when we call Get-WmiObject , we use drive letters with single quote. 现在,当我们调用Get-WmiObject ,我们使用单引号的驱动器号。 But when we use Set-WmiInstance , we use double quotes. 但是当我们使用Set-WmiInstance ,我们使用双引号。 However, even when I modify $s to be wrapped by double quote, it doesn't work. 但是,即使我修改$s以双引号包装,它也不起作用。 In short, if I use Write-Host $s , I get in output, say, "E:" . 简而言之,如果我使用Write-Host $s ,我会输入输出,比如"E:" When I use Set-WmiInstance -input $driveI -Arguments @{DriveLetter="E:"} , it works. 当我使用Set-WmiInstance -input $driveI -Arguments @{DriveLetter="E:"} ,它可以工作。 But when I use Set-WmiInstance -input $driveI -Arguments @{DriveLetter=$s} , it doesn't work. 但是当我使用Set-WmiInstance -input $driveI -Arguments @{DriveLetter=$s} ,它不起作用。 Could anyone tell me what I'm doing wrong? 谁能告诉我我做错了什么?

Don't put quotes in strings when you're going to remove them later anyway. 无论如何,当你要在以后删除它们时,不要将引号括在字符串中。 Instead add the quotes where you actually need them: 而是在实际需要它们的地方添加引号:

$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
    }
}

It's a bit clumsy, but this code works. 它有点笨拙,但这段代码有效。 I'll take your suggestions, and get rid of the Trim mess. 我会接受你的建议,摆脱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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM