在foreach循环中创建新变量?

时间:2019-09-14 21:36:13

标签: powershell loops variables foreach

我试图遍历驱动器号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)"
    }
}

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

根据需要,您需要使用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开关将覆盖已经创建的任何同名只读或常量变量。