Gaining Insights into Your Workstation Fleet
In this blog post, we will explore a PowerShell script that allows you to gather information about your workstation fleet. The script uses Active Directory and CIM (Common Information Model) to retrieve details about computers that meet specific criteria.
Prerequisites
The Script
The script begins by retrieving a list of computer names from Active Directory that meet specific criteria.
$computers = Get-adcomputer -Filter * -SearchBase "ou=Computers,DC=contoso,dc=com" -Properties Name | where {$_.Operatingsystem -notlike '*server*'} | select -expandproperty name | sort-object Name
How It Works
Retrieving Computer Names from Active Directory
The script starts by using the `Get-adcomputer` cmdlet to retrieve a list of computer names from Active Directory. The `-Filter *` parameter specifies that we want to retrieve all computers, while the `-SearchBase` parameter specifies the organizational unit (OU) where our computers reside.
Filtering Out Server Computers
The script then uses the `where` cmdlet to filter out any computer names that contain the string "server" in their operating system name. This ensures we only get workstation computers.
Sorting Computer Names
Finally, the script sorts the list of computer names alphabetically using the `sort-object` cmdlet.
Key Code Snippets
foreach ($c in $computers) {
if (test-connection $c -BufferSize 1 -Count 1 -Quiet) {
Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $c | Select Name, Model
}
}
Usage Examples
To use this script, simply copy and paste it into your PowerShell console. The script will then output a list of workstation computer names that meet the specified criteria.
Conclusion
This script provides a useful way to gather information about your workstation fleet using PowerShell and Active Directory. By combining the `Get-adcomputer` cmdlet with CIM, you can quickly retrieve details about your computers and use this information to make informed decisions about your IT infrastructure.