磁盘检查 PowerShell 和报告
disk check PowerShell with report
我制作了一个 PowerShell 脚本来编写报告并突出显示 space 20% 以下的任何磁盘,但脚本一直失败,因为大小值不正确。你能帮帮我吗?
$Computers = Get-Content -Path C:\Users\gbekari\unbackup\Servers.txt
$results = foreach ($Computer in $Computers){
Get-WmiObject Win32_Volume -Filter "DriveType='3'" -ComputerName $Computer | ForEach {
New-Object PSObject -Property @{
Computername = $computer
date = (Get-Date -format "dd.MM.yy HH:mm")
size = ([Math]::Round($_.Size /1GB,2))
freeSpace = ([Math]::Round($_.FreeSpace /1GB,2))
Status = if ([Math]::Round(100 * $db.FreeSpace / $db.Size) -gt 19 ) {'NONE'} else {'Warning'}
empty = "Diskcheck"
}
}
}
$results | ConvertTo-Csv -NoTypeInformation -Delimiter "|" | % {$_-replace'"',''} | Set-Content -Path C:\Users\gbekari\unbackup\Sers.txt
Lance U. Matthews is on point with his comment, you're trying to reference a property (Size
) that does not exist in the Win32_Volume Class 并且您正在引用未定义的变量 ($db
)。
顺便说一句,如果你是运行 PowerShell 3.0或以上版本,你可以通过强制转换[pscustomobject]
instead of using New-Object
构造对象,这种方式更直接,高效。
Get-WmiObject
不再存在于较新版本的 PowerShell 中,如文档中所述:
Starting in PowerShell 3.0, this cmdlet has been superseded by Get-CimInstance
您还可以并行查询所有计算机,-ComputerName
接受计算机数组。
$Computers = Get-Content -Path C:\Users\gbekari\unbackup\Servers.txt
$results = Get-CimInstance -ClassName Win32_Volume -Filter "DriveType='3'" -ComputerName $Computers | ForEach-Object {
$status = if ([Math]::Round(100 * $_.FreeSpace / $_.Capacity) -gt 19 ) {
'NONE'
}
else {
'Warning'
}
[pscustomobject]@{
Date = Get-Date -format "dd.MM.yy HH:mm"
Size = [Math]::Round($_.Capacity / 1GB, 2)
FreeSpace = [Math]::Round($_.FreeSpace / 1GB, 2)
Status = $status
Empty = "Diskcheck"
}
}
($results | ConvertTo-Csv -NoTypeInformation -Delimiter "|") -replace '"', '' |
Set-Content -Path C:\Users\gbekari\unbackup\Sers.txt
如果您不介意将计算机名称放在名为 PSComputerName
的列中,请保留代码 as-is,如果您想要更改列名称,则可以使用 Select-Object
:
($results | Select-Object @{N='ComputerName'; E={ $_.PSComputerName }}, * |
ConvertTo-Csv -NoTypeInformation -Delimiter "|") -replace '"', '' |
Set-Content -Path C:\Users\gbekari\unbackup\Sers.txt
我制作了一个 PowerShell 脚本来编写报告并突出显示 space 20% 以下的任何磁盘,但脚本一直失败,因为大小值不正确。你能帮帮我吗?
$Computers = Get-Content -Path C:\Users\gbekari\unbackup\Servers.txt
$results = foreach ($Computer in $Computers){
Get-WmiObject Win32_Volume -Filter "DriveType='3'" -ComputerName $Computer | ForEach {
New-Object PSObject -Property @{
Computername = $computer
date = (Get-Date -format "dd.MM.yy HH:mm")
size = ([Math]::Round($_.Size /1GB,2))
freeSpace = ([Math]::Round($_.FreeSpace /1GB,2))
Status = if ([Math]::Round(100 * $db.FreeSpace / $db.Size) -gt 19 ) {'NONE'} else {'Warning'}
empty = "Diskcheck"
}
}
}
$results | ConvertTo-Csv -NoTypeInformation -Delimiter "|" | % {$_-replace'"',''} | Set-Content -Path C:\Users\gbekari\unbackup\Sers.txt
Lance U. Matthews is on point with his comment, you're trying to reference a property (Size
) that does not exist in the Win32_Volume Class 并且您正在引用未定义的变量 ($db
)。
顺便说一句,如果你是运行 PowerShell 3.0或以上版本,你可以通过强制转换[pscustomobject]
instead of using New-Object
构造对象,这种方式更直接,高效。
Get-WmiObject
不再存在于较新版本的 PowerShell 中,如文档中所述:
Starting in PowerShell 3.0, this cmdlet has been superseded by
Get-CimInstance
您还可以并行查询所有计算机,-ComputerName
接受计算机数组。
$Computers = Get-Content -Path C:\Users\gbekari\unbackup\Servers.txt
$results = Get-CimInstance -ClassName Win32_Volume -Filter "DriveType='3'" -ComputerName $Computers | ForEach-Object {
$status = if ([Math]::Round(100 * $_.FreeSpace / $_.Capacity) -gt 19 ) {
'NONE'
}
else {
'Warning'
}
[pscustomobject]@{
Date = Get-Date -format "dd.MM.yy HH:mm"
Size = [Math]::Round($_.Capacity / 1GB, 2)
FreeSpace = [Math]::Round($_.FreeSpace / 1GB, 2)
Status = $status
Empty = "Diskcheck"
}
}
($results | ConvertTo-Csv -NoTypeInformation -Delimiter "|") -replace '"', '' |
Set-Content -Path C:\Users\gbekari\unbackup\Sers.txt
如果您不介意将计算机名称放在名为 PSComputerName
的列中,请保留代码 as-is,如果您想要更改列名称,则可以使用 Select-Object
:
($results | Select-Object @{N='ComputerName'; E={ $_.PSComputerName }}, * |
ConvertTo-Csv -NoTypeInformation -Delimiter "|") -replace '"', '' |
Set-Content -Path C:\Users\gbekari\unbackup\Sers.txt