locked
Script to output foreach of computers via Get-ADComputer RRS feed

  • Question

  • Hi, 

    I'm not so familiar with PS Scripting but learning.

    I want to gather computers from AD and use them in a variable for a foreach loop to check if computer is protected and how many drives it have.

    When i export a normal compters.txt from the ad via CMD i get a useable list, but i want it to be in one go instead to maintain computers.txt list every time.

    What i have come to is below script:

    $Computers = "C:\test\computers.txt"
    Foreach ($Computer in $Computers)
    {
    if (Test-Connection -ComputerName $Computer -Count 2 -Quiet)
    {
    manage-bde -cn $computer -status | select-string "Computer Name","Conversion Status" 
    Get-WMIObject win32_diskdrive -computer $computer
    }  
    else
    {
    Write-host "$Computer is offline"
    Write-host ""
    }
    }

    By this it test each computer if it online and showing results.

    When i then want to gather computers using this code (works if i run it seperately) I get a list of computers wanted.

    Get-ADComputer -Filter * -SearchBase 'OU=Computers,DC=DOMAIN,DC=COM' | sort -unique | Format-Table Name -wrap -auto

    But if i creplace "C:\computers.txt" with this line, it reports " Microsoft.PowerShell.Commands.Internal.Format.FormatStartData is offline " Like the String is not correct.

    My decided script that should work, but only if line #1 replaced with $Computers = C:\computers.txt

    $Computers = Get-ADComputer -Filter * -SearchBase 'OU=Computers,DC=DOMAIN,DC=COM' | sort -unique | Format-Table Name -wrap -auto
    Foreach ($Computer in $Computers)
    {
    if (Test-Connection -ComputerName $Computer -Count 2 -Quiet)
    {
    manage-bde -cn $computer -status | select-string "Computer Name","Conversion Status" 
    Get-WMIObject win32_diskdrive -computer $computer
    }  
    else
    {
    Write-host "$Computer is offline"
    Write-host ""
    }
    }


    Tuesday, August 28, 2018 3:31 PM

Answers

  • Pipe $Computers to a Select clause to get what you want. Or use $Computer.Name.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by Anders Kofoed Wednesday, August 29, 2018 6:49 AM
    Tuesday, August 28, 2018 3:55 PM

All replies

  • Remove: " |sort -unique | Format-Table Name -wrap -auto"


    \_(ツ)_/

    Tuesday, August 28, 2018 3:39 PM
  • Don't sort or format the $Computers collection. The array will already have unique computers.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, August 28, 2018 3:40 PM
  • But this give me an string with unwanted text, i need output of only computer name? 

    CN=PC-BD019,OU=Copenhagen,OU=Computers,DC=DOMAIN,DC=DK

    when i go to my Foreach loop i ask it to ping my client, this fails ofc. as the computer name now are as above... i only need the PC-BDxxx outcome for all pc's

    Tuesday, August 28, 2018 3:52 PM
  • Pipe $Computers to a Select clause to get what you want. Or use $Computer.Name.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by Anders Kofoed Wednesday, August 29, 2018 6:49 AM
    Tuesday, August 28, 2018 3:55 PM
  • Foreach ($Computer in $Computers) ==> Foreach ($Computer in$Computers.Name)

    and don't use Format-* cmdlets, those are used only when you want to output plain text in a formatted way. Format-* cmdlets converts everything to text and the output is not directly usable any more for any other cmdlets or expression.


    Regards kvprasoon

    Tuesday, August 28, 2018 8:11 PM
  • Thanks Richard, 

    I so forgot about that option. 

    Wednesday, August 29, 2018 6:49 AM