How to Build a Windows Server Inventory Report for Free with PowerShell – CloudSavvy IT


PowerShell is utilized by many server directors. Naturally, one of the used duties is the flexibility to construct scripts and features 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 be certain you will have the next conditions 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 laptop 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 laptop 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 all the laptop objects there through 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 extremely helpful to have the complete returned object to make the most of later.

Determining the Data to Collect

Now that we’ve got our servers, we want to work out what precisely ought to we accumulate from every server. One purpose that it may be essential to preserve the total AD object is to mix that knowledge with knowledge direct from the server itself to acquire a bigger image of your surroundings.

In apply what does one thing like this seem like? Let’s record out a number of the properties that might 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 we’ve got a record of servers, we can 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 possibly can inform, by saving the total object from Active Directory after we first retrieved the computer systems, lets us populate a big selection of knowledge. Unfortunately, this isn’t all the data that we want.

To get the knowledge from every server, we’ll 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 normal.

Get the Free Disk Space

Using the obtainable WMI class of Win32_LogicalDisk, we will get all the obtainable disks and their free area. When we first run the command, Get-CimInstance -ClassName Win32_LogicalDisk, you may discover that it’s not precisely readable in its default output.

The second drawback right here is that we’ve got multiple drive being returned. I would love to find out about every of these drives and the way a lot free area is obtainable in GBs. Let’s modify the code to do some transformations and make it higher.

$Disks = Get-CimInstance -ClassName Win32_LogicalDisk

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

$DisksEnd result

After we run the instructions, our output is far cleaner and may now be utilized in our script.

But what if we wished 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 underneath 10% or 10 GB. The purpose for the -or situation, is that on very massive disks, 10% should still be very beneficiant so setting an absolute restrict helps.

$Disks = Get-CimInstance -ClassName Win32_LogicalDisk

$DisksEnd result = $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
    }
}

$DisksEnd result

As you possibly can inform now, we’ve got a nice set of knowledge to be saved with our servers.

How%20topercent20Build%20apercent20Windows%20Server%20Inventory%20Report%20for/Untitled%202.png?trim=1,1&bg-color=000&pad=1,1

Get the Memory Available

It’s helpful 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 worthwhile assets by right-sizing the servers. Thankfully, that is a lot less complicated to retrieve.

Using the Win32_PhysicalMemory WMI class, we will sum all the 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 all the community connections collectively. This is helpful to know if a sure server has a number of interfaces to be anxious 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’ll use PS Remoting to invoke this domestically 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 we’ve got 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

  $DisksEnd result = $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 we’ve got demonstrated right here is simply the tip of the iceberg when it comes to what might be constructed for a listing report. There are many extra helpful properties that you may add on to this report. Taking this additional, you can construct this into an HTML web page, schedule a job to run this weekly, and even wrap this in different instruments resembling Ansible.

PowerShell makes it trivially simple 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.



Source link

This Web site is affiliated with Amazon associates, Clickbank, JVZoo, Sovrn //Commerce, Warrior Plus etc.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Adblock Detected!

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by whitelisting our website.