[ad_1]
PowerShell is utilized by many server directors. Naturally, one of the used duties is the flexibility to construct scripts and capabilities to stock your servers and perceive what your surroundings has.
Though there are lots of methods to accomplish this, with various ranges of complexity, we’re going to construct a pretty easy however impact Windows Server Inventory Report inside this text.
Prerequisites
This article might be hands-on. If you plan to comply with alongside, please ensure you could have the next stipulations in place first:
- Working on an Active Directory (AD) domain-joined Windows 10 PC
- Have the ActiveDirectory PowerShell module put in from the RSAT toolkit.
- Have permission to question AD pc accounts
- Can run distant WMI/CIM queries towards distant computer systems
- Have PowerShell Remoting obtainable on distant computer systems
Retrieving Servers
Foundational to the script we’re constructing are the servers themselves. You may individually write them out in a textual content file that’s learn in, or in an array throughout the script itself however utilizing PowerShell we will do one higher. To make the script extra dynamic and never require us to modify it any time a new server is added, we will use Active Directory (AD) to pull the record of pc objects in a given organizational unit (OU).
Below we’re using the ActiveDirectory
module, obtainable within the RSAT toolkit, to question the Servers
OU and retrieve the entire pc objects there by way of Get-ADComputer
.
Import-Module ActiveDirectory
$OU = 'OU=Servers,DC=area,DC=native'
$Params = @{
"SearchBase" = $OU
"Filter" = '*'
}
$Servers = Get-ADComputer @Params
At this level, we may have filtered out simply the title property to populate the $servers
variable, however typically it is rather helpful to have the whole returned object to make the most of later.
Determining the Data to Collect
Now that now we have our servers, we’d like to work out what precisely ought to we accumulate from every server. One cause that it may be essential to hold the complete AD object is to mix that information with information direct from the server itself to acquire a bigger image of your surroundings.
In follow what does one thing like this appear like? Let’s record out among the properties that will be very helpful to know.
Server Values
- Server Host Name
- Free Disk Space
- Memory
- Network Connections
AD Values
- Password Last Set
- Last Logon
- DNS Host Name
Retrieving Server Information
How will we go about accumulating this data on our record of returned servers? Since now we have a record of servers, we may have to iterate over the $Servers
object and question. Starting with a easy Foreach-Object
loop under, we will create a customized object to maintain our values.
$Servers | Foreach-Object {
[PSCustomObject]@{
"ServerHostName" = $_.Name
"Description" = $_.Description
"FreeDiskSpace" = $Null
"TotalMemory" = $Null
"NetworkConnections" = $Null
"PasswordLastSet" = $_.pwdLastSet
"LastLogon" = $_.finalLogon
"DNSHostName" = $_.DNSHostName
"CreationDate" = $_.WhenCreated
}
}
As you may inform, by saving the complete object from Active Directory after we first retrieved the computer systems, lets us populate a big selection of data. Unfortunately, this isn’t the entire data that we’d like.
To get the knowledge from every server, we are going to make the most of a acquainted interface to many server directors, which is the Windows Management Instrumentation (WMI) interface. You could discover that the cmdlets used under are from the Common Information Model (CIM) interface, of which WMI is Microsoft’s implementation of this commonplace.
Get the Free Disk Space
Using the obtainable WMI class of Win32_LogicalDisk
, we will get the entire obtainable disks and their free area. When we first run the command, Get-CimInstance -ClassName Win32_LogicalDisk
, you would possibly discover that it’s not precisely readable in its default output.
The second downside right here is that now we have a couple of drive being returned. I would really like to find out about every of these drives and the way a lot free area is on the market in GBs. Let’s modify the code to do some transformations and make it higher.
$Disks = Get-CimInstance -ClassName Win32_LogicalDisk
$DisksConsequence = $Disks | Foreach-Object {
[PSCustomObject]@{
"Drive" = $_.DeviceID
"FreeSpace" = [Math]::Round(($_.FreeSpace / 1GB),2)
}
}
$DisksConsequence
After we run the instructions, our output is far cleaner and may now be utilized in our script.
But what if we needed to alert on a low disk area situation? It can be good to develop this simply barely to set a flag on every drive that meets that situation. Comparing the free area to the overall obtainable area, we will see if it’s below 10% or 10 GB. The cause for the -or
situation, is that on very giant disks, 10% should be very beneficiant so setting an absolute restrict helps.
$Disks = Get-CimInstance -ClassName Win32_LogicalDisk
$DisksConsequence = $Disks | Foreach-Object {
$FreeSpace = [Math]::Round(($_.FreeSpace / 1GB),2)
$TotalSpace = [Math]::Round(($_.Size / 1GB),2)
If ( ($FreeSpace / $TotalSpace -LT 0.10) -Or $FreeSpace -LT 10 ) {
$LowDiskSpace = $True
} Else {
$LowDiskSpace = $False
}
[PSCustomObject]@{
"Drive" = $_.DeviceID
"FreeSpace" = $FreeSpace
"LowDiskSpace" = $LowDiskSpace
}
}
$DisksConsequence
As you may inform now, now we have a nice set of data to be saved with our servers.

Get the Memory Available
It’s useful to understand how a lot RAM is allotted to every server, particularly in a digital machine surroundings. If you discover that some are over-provisioned it can save you beneficial assets by right-sizing the servers. Thankfully, that is a lot easier to retrieve.
Using the Win32_PhysicalMemory
WMI class, we will sum the entire returned Capacity
properties to get the overall reminiscence.
(Get-CimInstance -ClassName Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum).Sum / 1GB
Get All of the Network Connections
Finally, we would like to retrieve the entire community connections collectively. This is helpful to know if a sure server has a number of interfaces to be apprehensive about. Using a barely completely different mechanism this time, we’re utilizing the Get-NetAdapter
cmdlet, however since this one doesn’t have a LaptopName
parameter, we are going to use PS Remoting to invoke this regionally on the goal server and return the outcomes to our script.
$NetworkConnections = Invoke-Command -LaptopName $_.DnsHostName -ScriptBlock Select-Object Name, Status, LinkSpeed
Our output will look comparable to that under and we will then save this into our script.
Keep in thoughts that for Invoke-Command
to work, PS Remoting will want to be arrange on the goal servers.
Putting it All Together
Now that now we have all of the items, let’s put this all collectively. The last script is under and combines all of the code to create a customized output object with simply what we would like to report on.
Import-Module ActiveDirectory
$OU = 'OU=Servers,DC=area,DC=native'
$Params = @{
"SearchBase" = $OU
"Filter" = '*'
}
$Servers = Get-ADComputer @Params
$Servers | Foreach-Object {
$Disks = Get-CimInstance -LaptopName $_.DnsHostName -ClassName Win32_LogicalDisk
$DisksConsequence = $Disks | Foreach-Object {
[PSCustomObject]@{
"Drive" = $_.DeviceID
"FreeSpace" = [Math]::Round(($_.FreeSpace / 1GB),2)
}
}
$NetworkConnections = Invoke-Command -LaptopName $_.DnsHostName -ScriptBlock Select-Object Name, Status, LinkSpeed
[PSCustomObject]@ Measure-Object -Property Capacity -Sum).Sum / 1GB)
"NetworkConnections" = $NetworkConnections
"PasswordLastSet" = $_.pwdLastSet
"LastLogon" = $_.finalLogon
"DNSHostName" = $_.DNSHostName
"CreationDate" = $_.WhenCreated
}
Conclusion
What now we have demonstrated right here is simply the tip of the iceberg by way of what will be constructed for a list report. There are many extra helpful properties which you could add on to this report. Taking this additional, you might construct this into an HTML web page, schedule a activity to run this weekly, and even wrap this in different instruments reminiscent of Ansible.
PowerShell makes it trivially straightforward to get all the knowledge you want to put collectively in a single place. Once you analyze your surroundings and decide what you want to know, construct the report in PowerShell to assist future-proof your skill to audit your surroundings.
[ad_2]
Source link