无法从WMIObject

时间:2018-12-07 19:19:18

标签: powershell windows-server-2016

关于powershell,我是个超级菜鸟。除ADComputer身份信息外,我已经能够提取WMIobject win32_LogicalDisk信息。请参阅我的代码和填充所需的列。我一直在用户下一片空白。有什么想法吗?

$exportPath = "\\Server01\users\ohyeah\Downloads\testfolder"

$computers = Get-Content "\\Server01\users\ohyeah\Downloads\testfolder\computers.txt"

$driveinfo = Get-WMIobject win32_LogicalDisk -ComputerName $computers -filter "DriveType=3" | Select-Object SystemName, DeviceID, VolumeName,
@{Name="Size_GB"; Expression={"{0:N1}" -f($_.size/1gb)}},
@{Name="FreeSpace_GB"; Expression={"{0:N1}" -f($_.freespace/1gb)}},
@{Name="%_FreeSpace_GB"; Expression={"{0:N2}%" -f(($_.freespace/$_.size)*100)}},
@{Name="User"; Expression={$(Get-ADComputer -identity $_ -Properties Description | ft -a Description)}},
@{Name="Date"; Expression={$(Get-Date -format 'g')}} 

$driveinfo | Out-GridView 
$driveinfo | Format-Table -AutoSize
$driveinfo | Export-Csv "$exportPath\test.csv" -NoTypeInformation -NoClobber -Append

SystemName设备ID VolumeName大小_GB FreeSpace_GB%_FreeSpace_GB用户日期

1 个答案:

答案 0 :(得分:0)

您已经通过SystemName属性获得了计算机名称。只需直接将其传递。

您也不能以这种方式在该计算出的属性中使用该Format-Table,特别是如果您要的只是一个字段属性,而该字段只是注释字段。

最后,除非填充了description字段,否则它将为空,并且使用Description属性不是默认列表的一部分,您必须要求它先询问所有属性,然后询问特定属性。

在加入了本地域的主机上运行它...

# This will give you the data in the Description property
Clear-Host

$computers = $env:COMPUTERNAME

$driveinfo = Get-WMIobject win32_LogicalDisk -ComputerName $computers -filter "DriveType=3" | 
Select-Object SystemName, DeviceID, VolumeName,
@{Name="Size_GB"; Expression={"{0:N1}" -f($_.size/1gb)}},
@{Name="FreeSpace_GB"; Expression={"{0:N1}" -f($_.freespace/1gb)}},
@{Name="%_FreeSpace_GB"; Expression={"{0:N2}%" -f(($_.freespace/$_.size)*100)}},
@{Name="User"; Expression={$(Get-ADComputer -identity $_.SystemName -Properties *).Description}},
@{Name="Date"; Expression={$(Get-Date -format 'g')}} 

$driveinfo | Out-GridView


# This will give you the full DN of the computer object
Clear-Host

$computers = $env:COMPUTERNAME

$driveinfo = Get-WMIobject win32_LogicalDisk -ComputerName $computers -filter "DriveType=3" | 
Select-Object SystemName, DeviceID, VolumeName,
@{Name="Size_GB"; Expression={"{0:N1}" -f($_.size/1gb)}},
@{Name="FreeSpace_GB"; Expression={"{0:N1}" -f($_.freespace/1gb)}},
@{Name="%_FreeSpace_GB"; Expression={"{0:N2}%" -f(($_.freespace/$_.size)*100)}},
@{Name="User"; Expression={$(Get-ADComputer -identity $_.SystemName -Properties Description)}},
@{Name="Date"; Expression={$(Get-Date -format 'g')}} 

$driveinfo | Out-GridView