PowerShell Script to get Servers info from the inventory list

Oleg Aronov 106 Reputation points
2021-09-16T16:22:13.277+00:00

Hello:

I wonder if someone can help me with PowerShell script to gather Servers info from my inventory list.

I need to collect some hardware/software information for remote servers on my list that I have already. How can I approach it?

Ideally I need as mush data as possible - OS, Built Date, UpTime, Space Used/Left, Last Reboot, Last Logon User and etc.

I should be able to provide credentials for login.
Of course, would be nice to check first if the server is reachable or not.

Is it doable? Any ideas where/how to start?

Thank you!

Microsoft System Center
Microsoft System Center
A suite of Microsoft systems management products that offer solutions for managing datacenter resources, private clouds, and client devices.
883 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,410 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,431 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 45,416 Reputation points
    2021-09-16T18:40:47.14+00:00

    Is it doable? Sure. At least most of it. Start with this:

    Get-Content InventoryListOfServerNames.txt |
        Foreach-Object{
            $x = Get-CimInstance -ComputerName $_ -ClassName win32_operatingsystem
            $y = Get-CimInstance -ComputerName $_-ClassName win32_logicaldisk -Filter "DriveType = 3"
            # fill in name/data pairs as needed
            [PSCustomObject]@{
                Name = Value
            }
        }
    

    The "Last Logon User" is unclear. Do you mean the last user to logon the machine from its console? Or interactively? Using RDP? There are many different types of logons. Each logon should be recorded in the machine's security log.


  2. Andreas Baumgarten 99,466 Reputation points MVP
    2021-09-16T19:07:25.46+00:00

    Hi @Oleg Aronov ,

    in addition to Rich:
    You can test the connection to the server like this (add the line between line 2 and 3 of Rich's script and add a } at the end):

    if (Test-Connection -TargetName $_ -Quiet) { # line 3 - 8 of Rich's script }  
    

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-connection?view=powershell-7.1

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  3. AlexZhu-MSFT 5,551 Reputation points Microsoft Vendor
    2021-09-20T02:59:29.863+00:00

    Hi,

    For Linux computers, we may use ssh connection to get the information we needed.

    Here's a sample for retrieveing OS info and cpu usage from linux computers. Just to illustrate how it works and we need to change it to cater for the actual environment.

    Get-Content InventoryListOfServerNames.txt |  
    	Foreach-Object{  
    		if (Test-Connection -computername $_ -Quiet)  
    		{  
    			$computer = $_  
    			$os_info = ssh root@$computer "uname -a"  
    			$os_info  
    			$cpu_usage_linux = ssh root@$computer "top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 | gawk '{print `$2+`$4+`$6}'"  
    			$cpu_usage_linux  
    		}  
    	}  
    

    133319-powershell-with-ssh-02.png

    Note: we need to setup passwordless ssh connect before running the script.
    https://mcilis.medium.com/how-to-setup-passwordless-ssh-connect-from-windows-to-linux-b84881454b6a
    Note: This is not from Microsoft, just for your reference

    Alex
    If the response is helpful, please click "Accept Answer" and upvote it.