Powershell 在foreach循环中创建新变量?
我试图循环遍历驱动器号A-Z,并为每个驱动器号输出一个新变量,其结果将由我们使用的监控程序获取。目前代码重置了变量,所以我只得到单个驱动器号的结果(Z,因为它是循环中的最后一个驱动器号)。我已经研究过动态变量(我不确定这是否是我所需要的),但我有点迷茫 理想情况下,我希望它输出以下内容:Powershell 在foreach循环中创建新变量?,powershell,loops,variables,foreach,Powershell,Loops,Variables,Foreach,我试图循环遍历驱动器号A-Z,并为每个驱动器号输出一个新变量,其结果将由我们使用的监控程序获取。目前代码重置了变量,所以我只得到单个驱动器号的结果(Z,因为它是循环中的最后一个驱动器号)。我已经研究过动态变量(我不确定这是否是我所需要的),但我有点迷茫 理想情况下,我希望它输出以下内容: $DriveLetterResultA = $DriveLetterResult for $DriveLetter 'A' $DriveLetterResultB = $DriveLetterResult fo
$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脚本输出一组属性的典型方式
$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)
您需要利用
设置变量
或新变量
$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
开关将覆盖已创建的同名只读或常量变量。感谢您的帮助,这个表很好,但我需要将每个驱动器号结果作为一个单独的字符串变量输出,否则程序无法处理这种类型的数据。非常感谢,这正是我想要的。谢谢!