Script to check remote machine info

As a follow-up to my last post I have now completed a script that enables you to search your Active Directory to get a list of applicable servers, then pull information from machines that you can run PowerShell scripts on remotely.

This script has expanded on the previous script and (I hope!) is a good reflection of my developing PowerShell skills. There are now IF statements to load modules (if required), a ping to test that the server is online, an AD lookup of the machine’s OS (better to check AD than attempt to query the server) as if you have non-PowerShell machines or cluster instances then the meaty part of the script would fail, a record of the Last Boot Time, processor family information and finally a list of installed server roles.

Two text files with a list of your servers is created, one that remains after the script finishes, and has a header with the number of servers queried, and another that is used by the script to iterate through the servers obtained from the AD search.

You can easily expand this script, so if you have multiple domains or an inconsistent machine naming policy it’s easy to include all required boxes.

If you want to automate this script you would need to run Get-credentials manually, so that a stored copy of your credentials be kept on the remoting server.

This script returns a text file for each machine queried. Next up is an Excel spreadsheet!

#################################################################### 
# Script  : ServerInfo.ps1 
# Author  : Tom Anderson  
# Date    : 17-10-2013    
# Purpose : Provide quick overview of servers in your domain. 
# History : 
#################################################################### 

#### Import required modules
IF (-not (Get-Module ServerManager)){            
  Import-Module ServerManager            
}  
IF (-not (Get-Module ActiveDirectory)){            
  Import-Module ActiveDirectory            
}  

#### Set parameters for output filename  
$date = Get-Date -format yyyyMMdd 
$machine = $env:computername
$folder = "\\$machine\E$\Folder\ServerInfo\$date"
$WServers = New-Item -type file "\\$machine\E$\Folder\ServerInfo\$date-ServerList.txt" -Force
$Servers = New-Item -type file "$folder\ServerList.txt" -Force

#### Set domain connectivity parameters
$LocalDomain = Get-ADComputer -SearchBase "DC=local,DC=yourdomain,DC=net" -Filter {(Name -like "UKServer*")} | Sort-Object Name | Select-Object -ExpandProperty Name
$ExtDomain = Get-ADComputer -SearchBase "OU=Users,DC=otherdomain,DC=net" -Filter {(Name -like "USServer*")} -Server "otherdomain.net" | Sort-Object Name | Select-Object -ExpandProperty Name

#### Pull credentials from stored file (saves you having to enter password multiple times)
#### Or prompts you to create a stored (and encrypted) file.
$Username = $env:username
$Folder = "\\server\folder"
$CredsFile = "$Folder\$Username-PowershellCreds.txt"
$FileExists = Test-Path $CredsFile
if  ($FileExists -eq $false) {
    Write-Host 'Credential file not found. Please enter your password:' -ForegroundColor Red
    Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File $CredsFile}
else
    {Write-Host 'You have a stored credential file which will be used to authenticate this session' -ForegroundColor Green}      
$Password = Get-Content $CredsFile | ConvertTo-SecureString
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password

#### Count the number of servers
$a = $LocalDomain | measure 
$b = $ExtDomain | measure
$Number = $a.count + $b.count
'There are ' + $Number + ' servers being queried.' | out-file $WServers -append
"`n" | out-file $WServers -append
Write-Host 'There are' $Number 'servers being queried.' -ForegroundColor Green

#### Output server names to file
$LocalDomain | out-file $Servers –append
$LocalDomain | out-file $WServers –append
$ExtDomain | out-file $Servers –append 
$ExtDomain | out-file $WServers –append  
$ServerList = Get-Content $Servers 
ForEach ($Server in $ServerList) {

#### Test connection to server. if no ping reply recived a files is created to advise.
#### If a reply is received then script continues.
IF(!(Test-Connection -Cn $Server -BufferSize 16 -Count 1 -ea 0 -quiet))
      {$file = New-Item -type file "\\machine\E$\Folder\ServerInfo\$date\$Server-$date.txt" -Force
      'Unable to ping server' | out-file $file -append}
       ELSE {

#### Set search root for AD OS search, depending on server name
IF ($Server -like "*UKServer*")
    {$root = [ADSI]"LDAP://local.yourdomain.net"
    $Account = Get-ADComputer $server -Server "local.yourdomain.net"}
        ELSE {$root = [ADSI]"LDAP://otherdomain.net"
        $Account = Get-ADComputer $server -Server "otherdomain.net"}

#### Look up server in AD, and only continue if OS is 2008 or 2008 R2
$SAMName = $Account.SamAccountName
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.filter = "(&(objectClass=computer)(SAMAccountName= $SAMName))"
$computer = $searcher.findall()
$computerprops = $computer[0].Properties
$targetaddress = $computerprops.operatingsystem
IF($targetaddress -notlike "*Windows Server 2008*")
    {$file = New-Item -type file "\\machine\E$\Folder\ServerInfo\$date\$Server-$date.txt" -Force
      'Server is 2003 or lower' | out-file $file -append }
       ELSE {

Invoke-Command -ComputerName $Server -ScriptBlock {

#### Create file
$date = Get-Date -format yyyyMMdd 
$ComputerName = hostname
$file = New-Item -type file "\\machine\E$\Folder\ServerInfo\$date\$ComputerName-$date.txt" -Force

#### Add header information
'System report for ' + $ComputerName | out-file $file -append
$date = get-date -Format D
$time = get-date -Format T
"`n" | out-file $file -append
'Run at ' + $time + " on " + $date | out-file $file -append
"`n" | out-file $file -append
"`n" | out-file $file -append

#### Get Server Manager on board
Import-Module ServerManager

#### Get Operating System information
'-=Operating System Information=-' | out-file $file -append
"`n" | out-file $file -append
$OS = Get-WMIObject Win32_OperatingSystem
$OSName = $OS.Caption
$System = Get-WMIObject Win32_ComputerSystem
$installdate = ([WMI]'').ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).InstallDate) 
$installdateuk = $installdate.ToString("dd/MM/yyyy hh:mm:ss")
$boot = $OS.ConvertToDateTime($OS.LastBootUpTime)
$bootuk = $boot.ToString("dd/MM/yyyy hh:mm:ss")
'Operating system: ' + "`t"+ $OSName | out-file $file -append
'Service pack: ' + "`t" + "`t"+ $OS.CSDVersion | out-file $file -append
'Install date: ' + "`t" + "`t"+ $installdateuk | out-file $file -append
'Last Boot Time: ' + "`t" + $bootuk | out-file $file -append
"`n" | out-file $file -append
"`n" | out-file $file -append

#### Get Hardware Model information
'-=Hardware Model Information=-' | out-file $file -append
"`n" | out-file $file -append
'Architecture: ' + "`t" + "`t"+ $OS.OSArchitecture | out-file $file -append
'Manufacturer: ' + "`t" + "`t"+ $System.Manufacturer | out-file $file -append
'Model: ' + "`t" + "`t" + "`t"+ $System.Model | out-file $file -append
"`n" | out-file $file -append
"`n" | out-file $file -append

#### Get Disk information
'-=Disk Information=-' | out-file $file -append
"`n" | out-file $file -append
$Disks = Get-WmiObject Win32_LogicalDisk -Filter "DriveType='3'"
foreach ($Disk in $Disks) {
$DiskGB = [math]::round($Disk.Size/1073741824, 0)
'Drive: ' + $Disk.DeviceID + "`t" + "`t" + 'Size: ' + $DiskGB + 'Gb' | out-file $file -append
}
"`n" | out-file $file -append
"`n" | out-file $file -append

#### Get Processor information
'-=Processor Information=-' | out-file $file -append
"`n" | out-file $file -append
$Processors = Get-WmiObject win32_processor
[int]$cores = 0
[int]$sockets = 0
[string]$test = $null
foreach ($processor in $processors)
{
      if ($processor.numberofcores -eq $null)
      {
            If (-not $Test.contains($processor.SocketDesignation))
            {
                  $Test = $Test + $processor.SocketDesignation
                  $sockets++
            }
                  $cores++
      }
      else
      {
            $sockets++
            $cores = $cores + $processor.numberofcores
      }
}
'Family: ' + "`t" + "`t" + $processor.name | out-file $file –append 
'Processor cores: ' + "`t" + $cores | out-file $file –append 
'Logical processors: ' + "`t" + $sockets | out-file $file –append 
"`n" | out-file $file -append
"`n" | out-file $file -append

#### Get Memory information
'-=Memory Information=-' | out-file $file -append
"`n" | out-file $file -append
$RAMGB = [math]::round($System.TotalPhysicalMemory/1024/1024/1024, 0)
'Memory size: ' + "`t" + "`t" + $RAMGB + 'Gb'  | out-file $file –append 
"`n" | out-file $file -append
"`n" | out-file $file -append

#### Get Pagefile information
'-=Pagefile Information=-' | out-file $file -append
"`n" | out-file $file -append
$pagefiles = Get-WmiObject Win32_PageFileSetting
foreach ($pagefile in $pagefiles) {
$PagefileGB = [math]::round($pagefile.MaximumSize/1024, 0)
'Location: ' + "`t" + "`t" + $pagefile.Name | out-file $file –append 
'Size: ' + "`t" + "`t" + "`t" + $PagefileGB + 'Gb' | out-file $file –append 
}
"`n" | out-file $file -append
"`n" | out-file $file -append

#### Get Network information information
'-=NIC Information=-' | out-file $file -append
"`n" | out-file $file -append
$NICS = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -Property [a-z]* -ExcludeProperty IPX*,WINS* 
foreach ($NIC in $NICS) {
'Adapter name: ' + "`t" + "`t" + $NIC.Description  | out-file $file –append 
'IP Address: ' + "`t" + "`t" + $NIC.IPAddress | out-file $file –append 
'Search suffixes: ' + "`t" + $NIC.DNSDomainSuffixSearchOrder | out-file $file –append 
"DNS Suffix: " +  "`t" +  "`t" + $NIC.DNSDomain | out-file $file –append 
'DNS Servers: ' + "`t" + "`t" + $NIC.DNSServerSearchOrder | out-file $file –append 
"`n" | out-file $file –append 
"`n" | out-file $file -append
}

#### Lookup DNS entry
'-=NSLookup of host entry=-' | out-file $file -append
"`n" | out-file $file -append
nslookup $ComputerName | out-file $file –append 
"`n" | out-file $file –append 

#### Get Server Roles
IF ($OSName -like "Microsoft Windows Server 2008 R2*") {
'-=Installed Roles=-' | out-file $file -append
Get-WindowsFeature | Where {$_.Installed -and $_.FeatureType -eq "Role"} | Sort Name | Format-Table DisplayName -hidetableheaders | out-file $file -append}
ELSE {
'Unable to retrieve installed roles as server in 2008 standard' | out-file $file -append}
} -authentication credssp -credential $Cred #Close from invoke command
} #Close from attempted ping
} #Close from OS check
} #Close from FOR EACH
Remove-Item $Servers
Posted in Uncategorized

Leave a Reply

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

*