Auto Inventory from Profile

Pre-Loading the Get-WindowsServers function from your PWSH or ISE profile is real handy for inventorys

# Function to get Windows Servers
function Get-WindowsServers {
    param (
        [string]$Domain
    )
    if(!($Domain)){$Domain = "Burwelltech"}
    # Get all Windows Server computers in the specified domain
    $servers = Get-ADComputer -Filter {(OperatingSystem -like "*Windows Server*") -and (Enabled -eq "true")} -Server $Domain -Properties * |select Name,operatingsystem,DNSHostName,DistinguishedName,Enabled

    return $servers
}

Then you can just sort by the field you need like this:

Get-WindowsServers |sort -Descending -Property OperatingSystem |ft -AutoSize

As you can see, the $Domain has to be set (like to the default production domain netBIOS name) in order to function, if you don’t enter one at the prompt

Get-WindowsServers -Domain AnotherClientDomain |sort -Descending -Property OperatingSystem |ft -AutoSize

If you don’t want to preset the Domain variable you can then call this function rom other scripts with the double dot:

. .\get-windowsservers


Posted

in

by

Tags:

Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.