Function from Active Directory Management MOL not working

Welcome Forums General PowerShell Q&A Function from Active Directory Management MOL not working

Viewing 2 reply threads
  • Author
    Posts
    • #184209
      Participant
      Topics: 120
      Replies: 258
      Points: 229
      Rank: Participant

      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
    • #184245
      Participant
      Topics: 1
      Replies: 20
      Points: 77
      Helping Hand
      Rank: Member

      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.

      • #184257
        Participant
        Topics: 120
        Replies: 258
        Points: 229
        Rank: Participant

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

    • #184254
      Participant
      Topics: 1
      Replies: 20
      Points: 57
      Helping Hand
      Rank: Member

       

      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

      • #184260
        Participant
        Topics: 120
        Replies: 258
        Points: 229
        Rank: Participant

        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}}

Viewing 2 reply threads
  • You must be logged in to reply to this topic.