Inventory computers in the domain. Laziness-the progress engine

All welcome.
Recently the boss asked me to think about the issue of collecting information about the configuration of the computers we have in the domain. First, the request was only at the expense of processors, memory and hard drives. First thought — going by Department and request to vacate the computer for a minute. In the case of 1 computer is not difficult, but if they are 1500. Thoughts were directed towards PowerShell.

First it was necessary to extract the list of all computers in the domain. In this example my domain is Test.lan. Import this entire list into a file AllComputers.csv.
To do this, don't forget to add the module HELL for PS. I have in the workplace, he stated in the profile, and advise you to do the same:

import-module activedirectory
get-ADcomputer -Filter * |
Where-Object {$a=$_.name; $_.DistinguishedName -ne "CN=$a,OU=Disable,DC=Test,DC=lan"} |
Sort-Object name | Select-Object name | Export-csv C:\Invent\AllComputers.csv -NoTypeInformation


Here it is necessary to clarify that in my domain there is a folder Disable, where are the accounts for all disconnected computers. If they are disabled, what's the point for them to knock. In this folder we exclude from the search.

Everyone knows that not all computers in the domain that is included, work or even have a place to be. This before proceed to verify, we check the relationship with him. Of course you can not do if you have 100 computers. And if you have 2000 computers, the loss of time when the number of computers turned off about 800 will eat at you not enough time. Also it is necessary to recall the computers to which we have access. In the same sense to knock at their door no.

import-csv c:\Invent\AllComputers.csv | foreach {
$a=$_.name
if ((Test-connection $a -count 2 -quiet) -eq "True")
{
if ((Get-WmiObject -computername $a Win32_OperatingSystem) -eq $null)
{


Many may argue:
"Why is it so difficult? Why should first do the list and then import it. Not easier at once?"
I agree, easier. But to have a list of computers that agree, nice. In addition, the list is numbered. And You always know how much you have computers in HELL.

For a connection test is selected, the cmdlet Test-connection with the-quiet option, that we were not given lines with different info and was just given the answer: True or False. What we reduce the number of queries from 2 to 4.
If we knock on to the computer with WMI query, and the rights to such action have not, will get a lot of lines red with error. So just filter out a computers WMI probe request

Any information (probably almost any) can be found, if you get into WMI objects, and PS allows you to do just to cheer. So I immediately delved into the search for the right WMI objects.
Here you can look all classes with their attributes.
Making sure that the computer on the network that the access we have to him is, look it inside:

the Write-Host "Checking computer" -ForeGroundColor Green $a
"Computer" | out-file c:\Invent\Comp\$a.txt
Get-WmiObject -computername $a Win32_OperatingSystem |
select-object csname, caption, Serialnumber, csdVersion |
ft @{Label="Network name"; Expression={$_.CSname}},
@{label="Name"; Expression={$_.caption}}
@{label="Version"; Expression={$_.csdVersion}},
@{label="Serial number"; Expression={$_.SerialNumber}} -auto -wrap |
out-file c:\Invent\Comp\$a.txt -append
Get-WmiObject -computername $a Win32_ComputerSystemProduct | select-object UUID |
UUID ft-autosize | out-file c:\Invent\Comp\$a.txt -append
"CPU" | out-file c:\Invent\Comp\$a.txt -append
Get-WmiObject -computername $a Win32_Processor | select-object name, SocketDesignation, Description |
ft @{label="Name"; Expression={$_.name}}
@{label="Connector"; Expression={$_.SocketDesignation}}
@{label="Description"; Expression={$_.Description}} -auto -wrap | out-file c:\Invent\Comp\$a.txt -append
"Motherboard" | out-file c:\Invent\Comp\$a.txt -append
Get-WmiObject -computername $a Win32_BaseBoard | select-object Manufacturer, Product, SerialNumber |
ft @{label="Manufacturer"; Expression={$_.manufacturer}}
@{label="Model"; Expression={$_.Product}}
@{label="Serial number"; Expression={$_.SerialNumber}} -auto -wrap |
out-file c:\Invent\Comp\$a.txt -append
"Hard drives" | out-file c:\Invent\Comp\$a.txt -append
Get-WmiObject -computername $a Win32_DiskDrive | select-object Model, Partitions, Size, interfacetype |
ft @{Label="Model"; Expression={$_.Model}}
@{Label="Number of sections"; Expression={$_.Partitions}}
@{Label="Interface"; Expression={$_.interfaceType}} -auto -wrap |
out-file c:\Invent\Comp\$a.txt -append
"Logical disks" | out-file c:\Invent\Comp\$a.txt -append
Get-WmiObject -computername $a Win32_LogicalDisk -Filter "drivetype is applied=3" | select-object DeviceID, FileSystem, Size, FreeSpace |
ft @{Label="Name"; Expression={$_.DeviceID}},
@{Label="Filesystem"; Expression={$_.FileSystem}},
@{Label="Size (GB)"; Expression={($_.Size/1GB).tostring("F00")}}
@{Label="Free space (GB)"; Expression={($_.FreeSpace/1GB).tostring("F00")}} -auto -wrap |
out-file c:\Invent\Comp\$a.txt -append
"RAM" | out-file c:\Invent\Comp\$a.txt -append
Get-WmiObject -computername $a Win32_Physicalmemory | Select-Object capacity, DeviceLocator |
ft @{Label="Size (MB)"; Expression={($_.capacity/1MB).tostring("F00")}}
@{Label="Location"; Expression={$_.DeviceLocator}} -auto -wrap |
out-file c:\Invent\Comp\$a.txt -append
"Video card" | out-file c:\Invent\Comp\$a.txt -append
Get-WmiObject -computername $a Win32_videoController |
Select-Object name, AdapterRAM, VideoProcessor |
ft @{Label="Name"; Expression={$_.name}}
@{Label="memory (MB)"; Expression={($_.AdapterRAM/1MB).tostring("F00")}}
@{Label="Video Processor"; Expression={$_.VideoProcessor}} -auto -wrap |
out-file c:\Invent\Comp\$a.txt -append
"Network card" | out-file c:\Invent\Comp\$a.txt -append
$OS=Get-WmiObject -computername $a Win32_OperatingSystem | foreach {$_.caption}
if ($OS -eq "Microsoft Windows 2000 Professional")
{
Get-WmiObject -computername $a Win32_NetworkAdapterConfiguration -Filter "DHCPEnabled=True" |
Select-Object caption,MACaddress |
ft @{Label="Name"; Expression={$_.caption}}
@{Label="MAC address"; Expression={$_.MACAddress}} -auto -wrap |
out-file c:\Invent\Comp\$a.txt -append
}
else
{
Get-WmiObject -computername $a Win32_NetworkAdapter -Filter "NetConnectionStatus>0" |
Select-Object name, AdapterType, MACAddress |
ft @{Label="Name"; Expression={$_.name}}
@{Label="MAC address"; Expression={$_.MACAddress}}
@{Label="Type"; Expression={$_.AdapterType}} -auto -wrap |
out-file c:\Invent\Comp\$a.txt -append
}
}


All information about the computer crashes to a text file.
Here it is necessary to stop attention on 2 points.
First, the line Write-Host "Checking computer" -ForeGroundColor Green $a I did solely for myself, because it's nice to know what is currently PowerShell.
Secondly, why are we before you check the network card check the OS.
Unfortunately, Windows 2000 does not respond to the query Win32_NetworkAdapter, for this we used the query Win32_NetworkAdapterConfiguration. Why not leave only the last? You can leave, but Win32_NetworkAdapter issues such attribute as Name, when its analogue only Caption. A trifle, but nice.
You can also see that when you check the network card, we check its performance, otherwise we will get a list of several cards that currently You are not interested. Do you need it?

That's basically all.
The script can be modified to your needs.
So now the authorities are unlikely to scare you with the computer inventory.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Looking for books as you want

Automatically create Liquibase migrations for PostgreSQL

Vkontakte sync with address book for iPhone. How it was done