Tips on how to Invent a Windows Server Inventory File for Free with PowerShell

Tips on how to Invent a Windows Server Inventory File for Free with PowerShell

Powershell logo

PowerShell is oldschool by many server directors. Naturally, one among primarily the most oldschool projects is the flexibility to effect scripts and capabilities to inventory your servers and impress what your environment has.

Even though there are diverse ways to attain this, with various phases of complexity, we’re going to effect a fairly straightforward nonetheless enact Windows Server Inventory File within this article.

Must haves

This article will most likely be hands-on. Whenever you occur to imply to be conscious alongside, please manufacture definite you’ve got the following necessities in plan first:

  • Working on an Energetic Directory (AD) arena-joined Windows 10 PC
  • Enjoy the ActiveDirectory PowerShell module installed from the RSAT toolkit.
  • Enjoy permission to question AD computer accounts
  • Can scoot a long way off WMI/CIM queries against a long way off computers
  • Enjoy PowerShell Remoting accessible on a long way off computers

Retrieving Servers

Foundational to the script we’re building are the servers themselves. You would possibly perchance perchance perchance well also individually write them out in a text file that is read in, or in an array contained in the script itself nonetheless the expend of PowerShell we are in a position to diagram one better. To manufacture the script extra dynamic and never require us to alter it any time a brand unusual server is added, we are in a position to expend Energetic Directory (AD) to drag the list of computer objects in a given organizational unit (OU).

Under we’re utilizing the ActiveDirectory module, accessible in the RSAT toolkit, to question the Servers OU and retrieve all of the computer objects there by Salvage-ADComputer.

Import-Module ActiveDirectory

$OU = 'OU=Servers,DC=arena,DC=native'

$Params = @{
    "SearchBase" = $OU
    "Filter"     = '*'
}

$Servers = Salvage-ADComputer @Params

At this level, we are in a position to also personal filtered out simply the name property to populate the $servers variable, nonetheless most incessantly it is a long way amazingly precious to personal your entire returned object to manufacture primarily the most of later.

Figuring out the Records to Rep

Now that we now personal our servers, we now personal to determine what precisely would possibly perchance perchance perchance aloof we select up from each server. One motive that it would even be principal to withhold the entire AD object is to combine that knowledge with knowledge reveal from the server itself to invent an even bigger image of your environment.

In be conscious what does something admire this glance admire? Let’s list out one of the most properties that would be very precious to grab.

Server Values

  • Server Host Title
  • Free Disk Set
  • Memory
  • Network Connections

AD Values

  • Password Closing Location
  • Closing Logon
  • DNS Host Title

Retrieving Server Records

How diagram we recede about collecting this knowledge on our list of returned servers? Since we now personal a list of servers, we are in a position to personal to iterate over the $Servers object and query. Starting with a straightforward Foreach-Object loop beneath, we are in a position to invent a personalized object to withhold our values.

$Servers | Foreach-Object {
    [PSCustomObject]@{
        "ServerHostName"     = $_.Title
        "Description"        = $_.Description
        "FreeDiskSpace"      = $Null
        "TotalMemory"        = $Null
        "NetworkConnections" = $Null
        "PasswordLastSet"    = $_.pwdLastSet
        "LastLogon"          = $_.lastLogon
        "DNSHostName"        = $_.DNSHostName
        "CreationDate"       = $_.WhenCreated
    }
}

As you will most likely be in a position to be in a situation to repeat, by saving the entire object from Energetic Directory after we first retrieved the computers, lets us populate a giant sequence of knowledge. Sadly, here just just isn’t all of the working out that we need.

To glean the working out from each server, we are in a position to manufacture primarily the most of a familiar interface to many server directors, which is the Windows Management Instrumentation (WMI) interface. You would possibly perchance perchance perchance well also sight that the cmdlets oldschool beneath are from the Overall Records Mannequin (CIM) interface, of which WMI is Microsoft’s implementation of this well-liked.

Salvage the Free Disk Set

The expend of the accessible WMI class of Diagram shut32_LogicalDisk, we are in a position to glean all of the accessible disks and their free home. After we first scoot the bid, Salvage-CimInstance -ClassName Diagram shut32_LogicalDisk, you would possibly perchance perchance perchance well perchance also sight that it’s not precisely readable in its default output.

The second command here is that we now personal better than one power being returned. I’d select to grab about each of these drives and the procedure great free home is accessible in GBs. Let’s alter the code to diagram some transformations and manufacture it better.

$Disks = Salvage-CimInstance -ClassName Diagram shut32_LogicalDisk

$DisksResult = $Disks | Foreach-Object {
    [PSCustomObject]@{
        "Pressure"     = $_.DeviceID
        "FreeSpace" = [Math]::Round(($_.FreeSpace / 1GB),2)
    }
}

$DisksResult

After we scoot the commands, our output is a long way cleaner and can now be oldschool in our script.

But what if we desired to alert on a low disk home situation? It’d be good to manufacture bigger this simply fairly to plan a flag on each power that meets that situation. Comparing the free home to the full accessible home, we are in a position to gaze if it’s below 10% or 10 GB. The motive in the support of the -or situation, is that on very immense disks, 10% would possibly perchance perchance perchance aloof aloof be very generous so environment an absolute restrict helps.

$Disks = Salvage-CimInstance -ClassName Diagram shut32_LogicalDisk

$DisksResult = $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 = $Correct
  } Else {
    $LowDiskSpace = $Inaccurate
  }

    [PSCustomObject]@{
        "Pressure"        = $_.DeviceID
    "FreeSpace"    = $FreeSpace
    "LowDiskSpace" = $LowDiskSpace
    }
}

$DisksResult

As you will most likely be in a position to be in a situation to repeat now, we now personal a immense plan of knowledge to be saved with our servers.

How%20to%20Invent%20a%20Windows%20Server%20Inventory%20File%20for/Untitled%202.png?fine=1,1&bg-coloration=000&pad=1,1

Salvage the Memory Readily accessible

It’s helpful to grab how great RAM is allocated to each server, particularly in a virtual machine environment. Whenever you occur to catch that some are over-provisioned you will most likely be in a position to be in a situation to set precious resources by apt-sizing the servers. Happily, here is a long way extra efficient to retrieve.

The expend of the Diagram shut32_PhysicalMemory WMI class, we are in a position to sum all of the returned Capacity properties to glean the full memory.

(Salvage-CimInstance -ClassName Diagram shut32_PhysicalMemory | Measure-Object -Property Capacity -Sum).Sum / 1GB

Salvage The entire Network Connections

Lastly, we’re enthusiastic to retrieve all of the community connections together. Right here’s precious to grab if a decided server has extra than one interfaces to be unnerved about. The expend of a fairly assorted mechanism this time, we’re the expend of the Salvage-NetAdapter cmdlet, nonetheless since this one doesn’t personal a ComputerName parameter, we are in a position to expend PS Remoting to invoke this in the neighborhood on the diagram server and return the outcomes to our script.

$NetworkConnections = Invoke-Stutter -ComputerName $_.DnsHostName -ScriptBlock {
    Salvage-NetAdapter -Bodily | Recall out-Object Title, Dwelling, LinkSpeed
}

Our output will glance the same to that beneath and we are in a position to then set this into our script.

Make a choice into legend that for Invoke-Stutter to work, PS Remoting can personal to be plan up on the diagram servers.

Striking it All Collectively

Now that we now personal the full pieces, let’s set this all together. The closing script is beneath and combines the full code to invent a personalized output object with simply what we’re enthusiastic to document on.

Import-Module ActiveDirectory

$OU = 'OU=Servers,DC=arena,DC=native'

$Params = @{
    "SearchBase" = $OU
    "Filter"     = '*'
}

$Servers = Salvage-ADComputer @Params

$Servers | Foreach-Object {
  $Disks = Salvage-CimInstance -ComputerName $_.DnsHostName -ClassName Diagram shut32_LogicalDisk

  $DisksResult = $Disks | Foreach-Object {
    [PSCustomObject]@{
      "Pressure"     = $_.DeviceID
      "FreeSpace" = [Math]::Round(($_.FreeSpace / 1GB),2)
    }
  }
  
  $NetworkConnections = Invoke-Stutter -ComputerName $_.DnsHostName -ScriptBlock {
    Salvage-NetAdapter -Bodily | Recall out-Object Title, Dwelling, LinkSpeed
  }

    [PSCustomObject]@{
        "ServerHostName"     = $_.Title
        "Description"        = $_.Description
        "FreeDiskSpace"      = $DisksResult
        "TotalMemory"        = ((Salvage-CimInstance -ComputerName $_.DnsHostName -ClassName Diagram shut32_PhysicalMemory | Measure-Object -Property Capacity -Sum).Sum / 1GB)
        "NetworkConnections" = $NetworkConnections
        "PasswordLastSet"    = $_.pwdLastSet
        "LastLogon"          = $_.lastLogon
        "DNSHostName"        = $_.DNSHostName
        "CreationDate"       = $_.WhenCreated
    }
}

Conclusion

What we now personal demonstrated here is simply the tip of the iceberg in the case of what would possibly perchance perchance also be constructed for a list document. There are many extra precious properties that you will most likely be in a position to be in a situation to add on to this document. Taking this extra, you would possibly perchance perchance perchance well perchance also effect this into an HTML page, time table a project to scoot this weekly, or even wrap this in other tools equivalent to Ansible.

PowerShell makes it trivially easy to glean the full knowledge it be principal to set together in one plan. Whenever you analyze your environment and resolve what it be principal to grab, effect the document in PowerShell to support future-proof your capability to audit your environment.

Learn More

Share your love