Extract Machine Info on Domain Using Get-WMIobject and Get-ADComputer

Hi Everyone,

I’m a super-noob when it comes to powershell. I’ve been able to extract the WMIobject win32_LogicalDisk info except for the ADComputer identity info. See my code and columns needed to populate. I keep getting a blank under user. Any thoughts?

$exportPath = “\Server01\users\ohyeah\Downloads\testfolder” # I change this to a central fileshare
$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($
@{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 DeviceID VolumeName Size_GB FreeSpace_GB %_FreeSpace_GB User Date

$_ is a placeholder for an object in the pipeline so when you run Get-ADComputer -identity $_ on line 8 you’re passing the current object in the pipeline to the Get-ADComputer cmdlet.

Line 4 is sending Win32_LogicalDisk objects down the pipeline and Get-ADComputer doesn’t know what to do with them.

You might get away with changing line 8 to $_.PSComputerName. Otherwise I’d probably refactor and use a foreach loop.

I see you cross posted. I provided you an answer in your other posting location, but so others may wan to know.
As for this…

@{Name="User"; Expression={$(Get-ADComputer -identity $_ -Properties Description | ft -a Description)}},

… is incorrect. The descript filed a note field in AD, for Format-Table is not an option, nor is it proper to use it in this calculated property.

That field will be blank if it is not filled in.
Also, you cannot use the $_ that way as that is not complete. $_.ServerName is what you should be using. Even with that, you’d still have issues.

So, you need to approach this like so…

@{Name="User"; Expression={(Get-ADComputer -identity $_.ServerName -Properties *).Description}},

… the above will return only that note property data. Other approaches will return an array of data or the DN.
For example (get not the Description field data but the computers full DN:

@{Name="User"; Expression={(Get-ADComputer -identity $_.ServerName -Properties Description}},

Hey John,

After I read your code a few times, I think I might be able to help point you in the right direction. However, you won’t be able to filter the text of type “string.” PS won’t compile that properly because -filter is meant for data objects that are filtered into tables or lists. You should try separating that line into it’s own script block.

You could try writing something like;

Get-ADComputer -cn $computers foreach ($computer in $ADcomputer)(Select-Object) -Property -Description | Out-String

Please let me know if you can get this to compile. It wouldnt surprise me if powershell was smart enough to print this out to screen. Powershell has amazing cmdlets, so its possible without a doubt.


[quote quote=129935]
However, you won’t be able to filter the text of type “string.”[/quote]

OP is using the Filter parameter correctly. The Filter parameter of Get-WMIObject accepts only an object of type String.

Thanks Matt, that is good to know. I was making an inference towards a possible real world problem he may have been trying to solve with the line of code not running. It looked like John was taking a stab at getting the user name from the ADcomputer field Description for each of the $computers. I have been in situations like John’s where your organization only tracks which computers are being assigned to which employees through a manual text “string” entry of “users name” into ADcomputer “Description” field. It is a bad practice of an IT department because it is poor asset management. That isn’t a common situation where you can go to tech.net and quickly retrieve well known code to perform that task for you.

If you possess admin rights, you could use PS remoting to find the user name. The easiest, least efficient way just enterpssession on each computer one by one and navigate to c:\Windows> DIR and look at the user folder it will give you their name. It’s better than having to walk around and log into each PC manually.