PowerShell Script to get Servers info from the inventory list

olegarr 131 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!

Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Server | User experience | Other
Microsoft System Center | Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 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 123.4K Reputation points MVP Volunteer Moderator
    2021-09-16T19:07:25.46+00:00

    Hi @olegarr ,

    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 6,591 Reputation points Moderator
    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.