Function from Active Directory Management MOL not working

Hello,

I was testing a script I found in Manning publication “Learn Active Directory Management in a Month of Lunches” (Chapter 20. (20.7) Maintaining and monitoring Active Directory) re: getting C: drive size but the output re: PSComputername is wonky. The Calculated Properties don’t work. I’ve looked at both my book example and the Manning website (some slight differences in formatting the script) but not sure how to get good output for the given DCs in the domain.

$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()

foreach ($dc in $dom.DomainControllers) {
    Get-WmiObject -Class Win32_Volume -ComputerName $dc -Filter "Name='C:\\'"
    select Name,
    @{N='Size(GB)'; E={[Math]::Round(($_.Capacity / 1GB),2)}},
    @{N='Free(GB)'; E={[Math]::Round(($_.FreeSpace / 1GB),2)}},
    @{N='PercFree'; E={[Math]::Round((($_.FreeSpace / $_.Capacity) * 100),2)}}
    PSComputerName
    }

Error is:

PSComputerName : The term 'PSComputerName' is not recognized as the name of a cmdlet

I’m supposed to see:

Name           : C:\
Size(GB)       : 180.46
Free(GB)       : 18.03
PercFree       : 9.99
PSComputerName : SERVER02

I haven’t tested it but it looks like you have a couple of syntax errors.

Line 4 you are missing a pipe symbol at the end.

Line 9 looks like it is missing a comma at the end.

 

Line 4 misses a pipe symbol, Line 8 misses a comma and you could replace line 9 by

 @{N='PSComputerName'; E={$_.PSComputerName}}

or leave it as it is but then it will only put the name of the DC

thanks, fixed Line 4
(no comma needed for end of Line 9)

Thanks Kris, fixed missing comma and now working.
(the Calculated Property for PSComputerName didn’t add anything different. Not sure why but anyway, my output is good now.)

…or did you mean I can simply label it other than “PSComputerName”?

ie @{N=‘Domain Controller’; E={$_.PSComputerName}}