我下面的代码收集启动时间,服务器规格,磁盘等,并存储到多个变量中。然后,我将它们放入一个新对象中以很好地显示结果。显示磁盘的问题会导致结果出现问题,我不介意将所有磁盘显示在一列中,但不会显示所有磁盘。使用Format-table -autosize不能解决问题。 如果我无法将所有值都放在一列中,那么什么是在动态分配的驱动器号中动态显示驱动器号的正确方法。 IE;第1列代表磁碟1的值,第2列代表磁碟2的值... 我删除了下面的一些代码以减少行数。
$BootTime = GET-WmiObject win32_Operatingsystem -ComputerName $server -EA
STOP | select @{n='ServerName';e={$_.csname}},@{n='LastBootUpTime';e={$_.ConverttoDateTime($_.lastbootuptime)}} ,@{n='LocalTime';e={$_.ConverttoDateTime($_.LocalDateTime)}}
$Disks = Get-WmiObject win32_logicaldisk -ComputerName $server -Filter Drivetype=3 |`
Select-Object @{n = "Drive Letter";e = {$_.DeviceID}},`
@{n = "Total(GB)";e = {"{0:N1}" -f( $_.Size / 1gb)}},`
@{n = "FreeSpace(GB)";e = {"{0:N1}" -f( $_.Freespace / 1gb ) }},`
@{n = "FreeSpace(%)"; e = {"{0:P0}" -f ($_.freespace/$_.size)}}
New-Object -Type PSObject -Property @{
ServerName = $BootTime.Servername
LastBootTime = $BootTime.LastBootUptime
LocalTime = $BootTime.LocalTime
UpTime = $BootTime.UpTime
OS = $BootTime.OS
Domain = $Domain.Domain
Drive = $Disks."Drive Letter"
"Disks(GB)" = $Disks."Total(GB)"
"FreeSpace(GB)" = $Disks."FreeSpace(GB)"
"FreeSpace(%)" = $Disks."FreeSpace(%)"
}
}
CATCH
{
New-Object -Type PSObject -Property @{
ServerName = $Server
LastBootTime = "Try Pinging"
LocalTime = "WMI Not Responding"
UpTime = "Possibly WMI not functioning or server is hung"#$null
OS = $null
Drive = $null
"Disks(GB)" = $null
"FreeSpace(GB)" = $null
"FreeSpace(%)" = $null
"Memory(GB)" =$null
PhysicalCPU = $null
CPUName = $null
Cores = $null
Manufacturer = $null
Model = $null
}
}
}
$Output| Select ServerName,LastBootTime, LocalTime, UpTime,Domain,OS, PhysicalCPU, CPUName, Cores, "Memory(GB)", Drive, "Disks(GB)", "FreeSpace(GB)", "FreeSpace(%)" , Manufacturer, Model
Current Results:
Drive : {C:, E:, F:, G:...}
Disks(GB) : {149.7, 500.0, 1,945.0, 1,024.0...}
FreeSpace(GB) : {90.0, 358.6, 1,411.1, 909.9...}
FreeSpace(%) : {60%, 72%, 73%, 89%...}
Desired Results:
Drive : {C:, E:, F:, G:,H:}
Disks(GB) : {149.7, 500.0, 1,945.0, 1,024.0,100}
OR
Drive1 : C:
Drive2: D:
Disk(GB) 1 :149.7
Disk(GB) 2 : 500.0
答案 0 :(得分:0)
这是我的答案:
$server="localhost"
$BootTime = GET-WmiObject win32_Operatingsystem -ComputerName $server -EA STOP | select @{n='ServerName';e={$_.csname}},@{n='LastBootUpTime';e={$_.ConverttoDateTime($_.lastbootuptime)}},@{n='LocalTime';e={$_.ConverttoDateTime($_.LocalDateTime)}},Caption,OSArchitecture
$Disks = Get-WmiObject win32_logicaldisk -ComputerName $server -Filter Drivetype=3 | Select-Object @{n = "Drive Letter";e = {$_.DeviceID}},@{n = "Total(GB)";e = {"{0:N1}" -f( $_.Size / 1gb)}},@{n = "FreeSpace(GB)";e = {"{0:N1}" -f( $_.Freespace / 1gb ) }},@{n = "FreeSpace(%)"; e = {"{0:P0}" -f ($_.freespace/$_.size)}}
try{
foreach($disk in $disks){
New-Object -Type PSObject -Property @{
ServerName = $BootTime.Servername
LastBootTime = $BootTime.LastBootUptime
LocalTime = $BootTime.LocalTime
#UpTime = $BootTime.UpTime #not in code
OS = $BootTime.Caption
OSArchitecture = $BootTime.OSArchitecture
Domain = $Domain.Domain
"Letter"= $disk."Drive Letter"
"Disk(GB)" = $disk."Total(GB)"
"FreeSpace(GB)" = $disk."FreeSpace(GB)"
"FreeSpace(%)" = $disk."FreeSpace(%)"
}
}
}
CATCH{
Write-Error $_.Exception.Message
exit -1
}