4

I have a program that selects which computers to connect to based on a text document. The list needs to only have the Computer names, 1 on each line. Nothing else.

I'm needing a command or script that i can run that produces this. I'd like to run this script automatically once a week to update the list with new computers. The server is running Windows Server 2008 R2. I have Quest AD Module installed, I haven't found much help with using it.

Any help would be appreciated! Thanks :)

2
  • 1
    Serverfault was not intended as a script writing service. I've voted for closure as there doesn't appear to have been an original attempt that failed.
    – Colyn1337
    Oct 30, 2015 at 14:29
  • This question would be on-topic on softwarerecs.stackexchange.com Nov 12, 2015 at 8:16

4 Answers 4

7

I think that's right, made some changes from a computer where i don't have a domain to test against.

# a PowerShell script, licensed under GPL ;)
#
# importing dependancy, assuming it's already installed.
# Install RSAT for Windows workstation, AD DS role for Windows Server if missing
Import-Module "ActiveDirectory"

# an array containing the OU paths we'll enumerate
$OUpaths = @("OU=Allocated,OU=Workstations,OU=WDS Org,DC=wds,DC=wdsgroup,DC=local","OU=Available,OU=Workstations,OU=WDS Org,DC=wds,DC=wdsgroup,DC=local")

# loop though the array of OUs, adding the computers to a list ('Object' really)
foreach ($iOUpath in $OUpaths)
    {
        ($objComputers += Get-ADComputer -SearchBase $iOUpath -Filter *)    #You might need to refine the query witha 'Filter' depending on your AD structure
    }

# dump the list to a file
$objComputers | Select name | Export-Csv -LiteralPath "C:\Temp\ComputerNames.txt" -NoTypeInformationA
4
  • I take it i run this as a powershell script?
    – Mooticus
    Oct 30, 2015 at 6:22
  • Yep, that's PowerShell. Don't need 'Quest AD' tool, just the native AD modules.
    – Conan1989
    Oct 30, 2015 at 7:03
  • @Conan1989 Maybe add a short description and/or some inline comments about what you're doing and why. Oct 30, 2015 at 7:13
  • 1
    @mathias-r-jessen Better? (Sorry; I'm a long time lurker, first time contributor)
    – Conan1989
    Oct 30, 2015 at 8:06
7

POWERSHELL GET AD COMPUTER OBJECT LIST OUTPUT TO FILE

Check out these PowerShell one-liner examples below, you can change and test the filter part of it to suit your needs for what you're querying from the AD Operating System, etc. attributes.

Be sure to change the output text file name and location where you need it to be output to in the Out-File C:\Test\Test.txt part file location wise.

Use for non-Servers List

Get-ADComputer -Filter {OperatingSystem -NotLike "*Server*"} | Select -Expand Name | Out-File C:\Test\Test.txt

Use for Servers List

Get-ADComputer -Filter {OperatingSystem -Like "*Server*"} | Select -Expand Name | Out-File C:\Test\TestServers.txt

Use for Custom Description List

(You can select all the AD computer objects from AD Users and Computers in the specific OU, then right-click and select Properties while ALL COMPUTER OBJECTS are selected, and then add a custom and unique string related to the OU for the Description value (see below screen shot). You could then refine the search filter below to look for the AD computers with this custom Description unique value to each OU)

Get-ADComputer -Filter {Description -Like "*CustomTestString*"} | Select -Expand Name | Out-File C:\Test\Custom.txt

enter image description here

3
  • Thanks! I can use the searchbase clause to refine it to OU's, however, do you know a way i can search multiple OU's with it? I need the computers of two OU's. I've found i can do this with QADComputer, but doesn't look like i can with ADComputer.
    – Mooticus
    Oct 30, 2015 at 6:17
  • @Mooticus for these kinds of things I've always preferred using an AD group. Add all the computers you are trying to get to a group and then pull it from there (Get-QADGroupMember [groupname]).
    – Patrick
    Oct 30, 2015 at 8:24
  • @Mooticus That'll work well too, just add the AD computer objects to a group and then query that group. Whatever floats your boat, many different ways to complete the task, so pick your way and move forward onto the next one. In any case, other domain admins need to not make changes without coordination if you're in a multi-admin environment whether it be change the values of some attribute, move out of the OU, or remove from the group; all similar downsides I suppose--we can scrutinize all day long if we wanted to but I'm sure you just want a feasible solution ultimately. Oct 30, 2015 at 18:04
1

I use this one liner to export the active computers in AD:

Get-ADComputer -filter {Enabled -eq $True} -Properties cn -SearchBase "OU=servers,OU=computers,DC=example,DC=example" | select cn | ConvertTo-Csv -NoTypeInformation | Select-Object -skip 1 | Out-File d:\output.csv

You can use .txt instead of .csv if you need it as a text file and just configure the script to run as a scheduled task.

Hope this helps you.

0

You can do like this for computers created in the last 7 days:

$d=[DateTime]::Today.AddDays(-7)  

$comps=Get-ADComputer -filter { (whencreated -le $d) } -searchbase "OU=,OU= ,DC= ,DC= " -properties whencreated, Description

$comps | sort-object -Descending whencreated |ft Name  -A
write-host 'PCs=' $comps.Count
1
  • Is there a reason you get your date object like that? is Get-Date not fractionally easier, and powershell 'native'?
    – Patrick
    Oct 30, 2015 at 8:22

Not the answer you're looking for? Browse other questions tagged .