PowerShell for IT Professionals [#15] – Scripting part 2 – IF and psobject

Reading Time: < 1 minute

In this lesson we carry on writing the scripting by gathering requirements and putting them together as comments in code.

Then we will retrieve OS information with the help of WMI and display it on the screen with Write-Host.

Although using Write-Host is easy to use, it doesn’t really allow us to do very much e.g. we cannot export information to the CSV, therefore we change it and start using custom PSObject – that way our script will start returning information like a regular PowerShell command.

Finally, we will use an IF statement to decide whether the OS we are checking is a server.

Reference

IF statement

PS Custom Object

Hash table

Script

$OS = Get-CimInstance -ClassName Win32_OperatingSystem
# OS Name
Write-Host "OS Name: $($OS.Caption)"
# Version
Write-Host "OS Version: $($OS.Version)"
# Install Date
Write-Host "OS Install date: $($OS.InstallDate)"
# Client/Server
IF ($os.Caption -like "*Server*") { $server = $true } else { $server = $false}
# IP: Interface, ip, gateway, dns
# Network: Can I ping gateway, DNS Server, website
# Installed roles – server only
# Last updates
# It would be nice if could run this remotely 

$props = [pscustomobject]@{
    'OSName' = $OS.Caption
    'OSVersion' = $OS.Version
    'OSInstallDate' = $OS.InstallDate
    'isServer' = $Server
}
$props

0 comments on “PowerShell for IT Professionals [#15] – Scripting part 2 – IF and psobject

Leave a Reply

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