【问题标题】:Creating new variables in foreach loop?在 foreach 循环中创建新变量?
【发布时间】:2020-01-16 06:18:46
【问题描述】:

我正在尝试遍历驱动器号 A-Z,并为每个驱动器号输出一个新变量,结果将由我们使用的监控程序获取。目前代码重置了变量,所以我只得到一个驱动器号的结果(Z,因为它是循环中的最后一个)。我已经查看了动态变量(我不确定这是否是我需要的),但我有点迷茫……

理想情况下,我希望它输出以下内容:

$DriveLetterResultA = $DriveLetterResult for $DriveLetter 'A'
$DriveLetterResultB = $DriveLetterResult for $DriveLetter 'B'
$DriveLetterResultC = $DriveLetterResult for $DriveLetter 'C'
$DriveLetterResultD = $DriveLetterResult for $DriveLetter 'D'
etc...

这是无效的代码:

$DriveLetters = [char[]](0..255) -clike '[A-Z]'
foreach ($DriveLetter in $DriveLetters) {
    $drive = New-Object system.io.driveinfo("$($DriveLetter)`:")
    $drive.DriveType
    $drive.DriveFormat
    if (($drive.DriveType -eq "Fixed") -and ($drive.DriveFormat -eq "NTFS" -or ($drive.DriveFormat -eq "FAT") -or ($drive.DriveFormat -eq "FAT32") -or ($drive.DriveFormat -eq "exFAT"))) {
        $DriveLetterResult = Repair-Volume -DriveLetter $DriveLetter -Scan -ErrorAction 0 -ErrorVariable RepairVolumeError
        if ($DriveLetterResult-eq "ScanRunning") {
            $DriveLetterResult = "ScanRunning (NoErrorsFound)"
        }
        if ($RepairVolumeError -ne "") {
            $DriveLetterResult= $RepairVolumeError | Out-String
        }
    } else {
        $DriveLetterResult = "NA (NoErrorsFound)"
    }
}

【问题讨论】:

    标签: powershell loops variables foreach


    【解决方案1】:

    这是如何开始的。这是 powershell 脚本输出一组属性的典型方式。

    $DriveLetters = [char[]](0..255) -clike '[A-Z]'
    foreach ($DriveLetter in $DriveLetters) {
        $drive = New-Object system.io.driveinfo("$($DriveLetter)`:")
        #$drive.DriveType
        #$drive.DriveFormat
        If (($drive.DriveType -eq "Fixed") -and
          ($drive.DriveFormat -eq "NTFS" -or ($drive.DriveFormat -eq "FAT") -or
          ($drive.DriveFormat -eq "FAT32") -or ($drive.DriveFormat -eq "exFAT"))) {
        $DriveLetterResult = Repair-Volume -DriveLetter $DriveLetter -Scan -ErrorAction 0 -ErrorVariable RepairVolumeError
            If ($DriveLetterResult-eq "ScanRunning") {
            $DriveLetterResult = "ScanRunning (NoErrorsFound)"
            }
            If ($RepairVolumeError -ne "") {
            $DriveLetterResult= $RepairVolumeError | Out-String
            }    }
        Else {
        $DriveLetterResult = "NA (NoErrorsFound)"
        }
    
        [pscustomobject]@{
          DriveLetter = $DriveLetter
          DriveType = $drive.DriveType
          DriveFormat = $drive.DriveFormat
          DriveLetterResult = $DriveLetterResult
        }
    }
    

    输出:

    DriveLetter       DriveType DriveFormat DriveLetterResult
    -----------       --------- ----------- -----------------
    A           NoRootDirectory             NA (NoErrorsFound)
    B           NoRootDirectory             NA (NoErrorsFound)
    C                     Fixed NTFS        Repair-Volume : Access denied...
    D           NoRootDirectory             NA (NoErrorsFound)
    E           NoRootDirectory             NA (NoErrorsFound)
    F           NoRootDirectory             NA (NoErrorsFound)
    G           NoRootDirectory             NA (NoErrorsFound)
    H           NoRootDirectory             NA (NoErrorsFound)
    I           NoRootDirectory             NA (NoErrorsFound)
    J           NoRootDirectory             NA (NoErrorsFound)
    K           NoRootDirectory             NA (NoErrorsFound)
    L           NoRootDirectory             NA (NoErrorsFound)
    M           NoRootDirectory             NA (NoErrorsFound)
    N           NoRootDirectory             NA (NoErrorsFound)
    O           NoRootDirectory             NA (NoErrorsFound)
    P           NoRootDirectory             NA (NoErrorsFound)
    Q           NoRootDirectory             NA (NoErrorsFound)
    R           NoRootDirectory             NA (NoErrorsFound)
    S           NoRootDirectory             NA (NoErrorsFound)
    T           NoRootDirectory             NA (NoErrorsFound)
    U           NoRootDirectory             NA (NoErrorsFound)
    V           NoRootDirectory             NA (NoErrorsFound)
    W           NoRootDirectory             NA (NoErrorsFound)
    X           NoRootDirectory             NA (NoErrorsFound)
    Y           NoRootDirectory             NA (NoErrorsFound)
    Z           NoRootDirectory             NA (NoErrorsFound)
    

    【讨论】:

    • 感谢您的帮助,该表很棒,但我需要将每个驱动器号结果输出为字符串中的单独变量,否则程序无法处理此类数据。
    【解决方案2】:

    您需要使用Set-VariableNew-Variable

    $DriveLetters = [char[]](65..90)
    foreach ($DriveLetter in $DriveLetters) {
        $drive = New-Object system.io.driveinfo("$($DriveLetter)`:")
        $drive.DriveType
        $drive.DriveFormat
        if (($drive.DriveType -eq "Fixed") -and ($drive.DriveFormat -eq "NTFS" -or ($drive.DriveFormat -eq "FAT") -or ($drive.DriveFormat -eq "FAT32") -or ($drive.DriveFormat -eq "exFAT"))) {
            $DriveLetterResult = Repair-Volume -DriveLetter $DriveLetter -Scan -ErrorAction 0 -ErrorVariable RepairVolumeError
            if ($DriveLetterResult-eq "ScanRunning") {
                $DriveLetterResult = "ScanRunning (NoErrorsFound)"
            }
            if ($RepairVolumeError -ne "") {
                $DriveLetterResult= $RepairVolumeError | Out-String
            }
        } else {
            $DriveLetterResult = "NA (NoErrorsFound)"
        }
        Set-Variable -Name "DriveLetterResult$DriveLetter" -Value $DriveLetterResult -Force
    }
    

    -Force 开关将覆盖已创建的任何同名只读变量或常量变量。

    【讨论】:

    • 非常感谢,这正是我所追求的。赞赏!
    猜你喜欢
    • 2018-06-17
    • 2021-12-02
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多