从多台计算机免费获得 space
Get free space from multiple computers
我写了一个简单的脚本,它应该从一个 OU 中获取计算机列表并报告磁盘大小和每台可访问计算机上的可用 space 数量。
$computers = Get-ADComputer -SearchBase "ou=Test,ou=test,ou=test,dc=test,dc=test,dc=net" -Filter * | select -ExpandProperty Name
$output = Foreach ($item in $computers) {
Write-host $item
$disk = Get-WmiObject Win32_LogicalDisk -Computer $item -Filter "DeviceID='C:'"
$disk_size = [math]::Round($disk.Size/1GB,2)
Write-host $disk_size
$disk_freespace = [math]::Round($disk.Freespace/1GB,2)
Write-Host $disk_freespace
}
$output | Export-Csv "networkpath\free_space_on_c_drive.csv" -notypeinformation -encoding "unicode"
输出在屏幕上看起来不错,但导出的 CSV 文件是空的。我想我可以将 foreach
更改为 ForEach-Object
,但我认为将每个脚本都放在一个衬里并不是一个好习惯,如果我需要添加更多操作,那将会很痛苦。
这是我根据评论更新的代码:
$computers = Get-ADComputer -SearchBase "ou=t,ou=test,ou=Production,ou=test,dc=test,dc=test,dc=net" -Filter * | select -ExpandProperty Name
$output = Foreach ($item in $computers) {
$disk = Get-WmiObject Win32_LogicalDisk -Computer $item -Filter "DeviceID='C:'"
$disk_size = [math]::Round($disk.Size/1GB,2)
$disk_freespace = [math]::Round($disk.Freespace/1GB,2)
$output = [PSCustomObject]@{
Name = $item
Size = $disk_size
FreeSpace = $disk_freespace
}
}
$output | Export-Csv "networkpath$((Get-Date).ToString("yyyyMMdd_HHmmss"))_free_space_on_c_drive.csv" -notypeinformation -encoding "unicode"
但现在我的 CSV 文件中只有一条记录。我究竟做错了什么?
这就是我试图传达的有关构建 PSCustomObject
并将其发送到 collection 的信息。 [咧嘴一笑]
它的作用...
- 在文本文件中伪造阅读
当准备好对您的数据执行此操作时,删除整个 #region/#endregion
块并使用 Get-Content
或使用 Get-ADComputer
。
- 定义报告文件名
- 定义没有响应时使用什么
- 遍历计算机名称 collection
- 测试目标是否响应
- 如果是 = 获取 WMI 磁盘数据
这使用 splat
来提高可读性,而不是一长串参数和值。
- if no = 将值设置为
$NoResponse
- 构建
PSCO
- 将其发送到
$Results
collection
- 在屏幕上显示
- 将 collection 发送到报告文件
代码...
#region >>> fake reading in a text file
# in real life, use Get-Content
$ComputerList = @'
BetterNotBeThere
LocalHost
10.0.0.1
127.0.0.1
'@ -split [System.Environment]::NewLine
#endregion >>> fake reading in a CSV file
$TimeStamp = (Get-Date).ToString('yyyy-MM-dd')
$ReportPath = $env:TEMP
$ReportFile = 'Dmitry Dorofeev_-_DiskSpaceReport_-_{0}.csv' -f $TimeStamp
$FullReportFile = Join-Path -Path $ReportPath -ChildPath $ReportFile
$NoResponse = '__n/a__'
$Results = foreach ($CL_Item in $ComputerList)
{
if (Test-Connection -ComputerName $CL_Item -Count 2 -Quiet)
{
$GWO_Params = @{
Class = 'Win32_LogicalDisk'
ComputerName = $CL_Item
Filter = "DeviceID = 'c:'"
}
$DiskInfo = Get-WmiObject @GWO_Params
$Size_GB = [math]::Round($DiskInfo.Size / 1gb, 2)
$FreeSpace_GB = [math]::Round($DiskInfo.FreeSpace / 1gb, 2)
}
else
{
$Size_GB = $FreeSpace_GB = $NoResponse
}
[PSCustomObject]@{
ComputerName = $CL_Item
Size_GB = $Size_GB
FreeSpace_GB = $FreeSpace_GB
}
}
# display on screen
$Results
# send to a csv file
$Results |
Export-Csv -LiteralPath $FullReportFile -NoTypeInformation
屏幕输出...
ComputerName Size_GB FreeSpace_GB
------------ ------- ------------
BetterNotBeThere __n/a__ __n/a__
LocalHost 931.41 730.79
10.0.0.1 __n/a__ __n/a__
127.0.0.1 931.41 730.79
csv 文件 [C:\Temp\Dmitry Dorofeev_-_DiskSpaceReport_-_2020-05-22.csv
] 内容...
"ComputerName","Size_GB","FreeSpace_GB"
"BetterNotBeThere","__n/a__","__n/a__"
"LocalHost","931.41","730.79"
"10.0.0.1","__n/a__","__n/a__"
"127.0.0.1","931.41","730.79"
我写了一个简单的脚本,它应该从一个 OU 中获取计算机列表并报告磁盘大小和每台可访问计算机上的可用 space 数量。
$computers = Get-ADComputer -SearchBase "ou=Test,ou=test,ou=test,dc=test,dc=test,dc=net" -Filter * | select -ExpandProperty Name
$output = Foreach ($item in $computers) {
Write-host $item
$disk = Get-WmiObject Win32_LogicalDisk -Computer $item -Filter "DeviceID='C:'"
$disk_size = [math]::Round($disk.Size/1GB,2)
Write-host $disk_size
$disk_freespace = [math]::Round($disk.Freespace/1GB,2)
Write-Host $disk_freespace
}
$output | Export-Csv "networkpath\free_space_on_c_drive.csv" -notypeinformation -encoding "unicode"
输出在屏幕上看起来不错,但导出的 CSV 文件是空的。我想我可以将 foreach
更改为 ForEach-Object
,但我认为将每个脚本都放在一个衬里并不是一个好习惯,如果我需要添加更多操作,那将会很痛苦。
这是我根据评论更新的代码:
$computers = Get-ADComputer -SearchBase "ou=t,ou=test,ou=Production,ou=test,dc=test,dc=test,dc=net" -Filter * | select -ExpandProperty Name
$output = Foreach ($item in $computers) {
$disk = Get-WmiObject Win32_LogicalDisk -Computer $item -Filter "DeviceID='C:'"
$disk_size = [math]::Round($disk.Size/1GB,2)
$disk_freespace = [math]::Round($disk.Freespace/1GB,2)
$output = [PSCustomObject]@{
Name = $item
Size = $disk_size
FreeSpace = $disk_freespace
}
}
$output | Export-Csv "networkpath$((Get-Date).ToString("yyyyMMdd_HHmmss"))_free_space_on_c_drive.csv" -notypeinformation -encoding "unicode"
但现在我的 CSV 文件中只有一条记录。我究竟做错了什么?
这就是我试图传达的有关构建 PSCustomObject
并将其发送到 collection 的信息。 [咧嘴一笑]
它的作用...
- 在文本文件中伪造阅读
当准备好对您的数据执行此操作时,删除整个#region/#endregion
块并使用Get-Content
或使用Get-ADComputer
。 - 定义报告文件名
- 定义没有响应时使用什么
- 遍历计算机名称 collection
- 测试目标是否响应
- 如果是 = 获取 WMI 磁盘数据
这使用splat
来提高可读性,而不是一长串参数和值。 - if no = 将值设置为
$NoResponse
- 构建
PSCO
- 将其发送到
$Results
collection - 在屏幕上显示
- 将 collection 发送到报告文件
代码...
#region >>> fake reading in a text file
# in real life, use Get-Content
$ComputerList = @'
BetterNotBeThere
LocalHost
10.0.0.1
127.0.0.1
'@ -split [System.Environment]::NewLine
#endregion >>> fake reading in a CSV file
$TimeStamp = (Get-Date).ToString('yyyy-MM-dd')
$ReportPath = $env:TEMP
$ReportFile = 'Dmitry Dorofeev_-_DiskSpaceReport_-_{0}.csv' -f $TimeStamp
$FullReportFile = Join-Path -Path $ReportPath -ChildPath $ReportFile
$NoResponse = '__n/a__'
$Results = foreach ($CL_Item in $ComputerList)
{
if (Test-Connection -ComputerName $CL_Item -Count 2 -Quiet)
{
$GWO_Params = @{
Class = 'Win32_LogicalDisk'
ComputerName = $CL_Item
Filter = "DeviceID = 'c:'"
}
$DiskInfo = Get-WmiObject @GWO_Params
$Size_GB = [math]::Round($DiskInfo.Size / 1gb, 2)
$FreeSpace_GB = [math]::Round($DiskInfo.FreeSpace / 1gb, 2)
}
else
{
$Size_GB = $FreeSpace_GB = $NoResponse
}
[PSCustomObject]@{
ComputerName = $CL_Item
Size_GB = $Size_GB
FreeSpace_GB = $FreeSpace_GB
}
}
# display on screen
$Results
# send to a csv file
$Results |
Export-Csv -LiteralPath $FullReportFile -NoTypeInformation
屏幕输出...
ComputerName Size_GB FreeSpace_GB
------------ ------- ------------
BetterNotBeThere __n/a__ __n/a__
LocalHost 931.41 730.79
10.0.0.1 __n/a__ __n/a__
127.0.0.1 931.41 730.79
csv 文件 [C:\Temp\Dmitry Dorofeev_-_DiskSpaceReport_-_2020-05-22.csv
] 内容...
"ComputerName","Size_GB","FreeSpace_GB"
"BetterNotBeThere","__n/a__","__n/a__"
"LocalHost","931.41","730.79"
"10.0.0.1","__n/a__","__n/a__"
"127.0.0.1","931.41","730.79"