Hi folks !
I am starting to get more into PowerShell scripting. I did a small script to grab system and computer information that I find useful. However, I'd like to have some outside thoughts on
- Function structure
- Is it ok ?
- Am I on the right path ?
I tested this code on Windows 10 and Windows Server 2012 Standard. It does give output what I want the function to do, therefore, I want to improve my skill and my code structure.
Thanks for reading !
Steven
I do know there is a synthax error, but if I leave it on one line, you guys wouldn't see it.# PowerShell v5.1 function Get-SystemInfo { # Gather information from computer $Computer = Get-WMIObject -Class Win32_ComputerSystem $DNSFlag = (Get-WMIObject -Class Win32_ComputerSystem).PartOfDomain $Name = $Computer.Name $Model = $Computer.Model # Gather information from system $PSVersion = $PSVersionTable.PSVersion.ToString() $OSInfo = Get-CimInstance Win32_OperatingSystem $OS = $OSInfo.Caption $OSVersion = $OSInfo.Version $InstallOn = $OSInfo.InstallDate $IPv4 = Get-NetIPAddress -AddressFamily 'IPv4' |
Where-Object {($_.InterfaceAlias -like 'Ethernet*') -OR ($_.InterfaceAlias -like 'Wi*')
-OR ($_.InterfaceAlias -like 'Local*')} `
| Where-Object {$_.IPAddress -notlike '169.254.*'} $IP = $IPv4.IPAddress $Interface = $IPv4.InterfaceAlias # Show gathered computer's information Write-Host '' # Empty Write-Host '' # Empty Write-Host " NAME : $Name" Write-Host " MODEL : $Model" IF($DNSFlag -match 'True'){ $Domain = $Computer.Domain Write-Host " DOMAIN : $Domain" } Write-Host '' # Empty # Show gathered system's information Write-Host " OPERATING SYSTEM : $OS" Write-Host " VERSION : $OSVersion" Write-Host " INSTALLED ON : $InstallOn" Write-Host " POWERSHELL VERSION : $PSVersion" IF ($IP.Count -lt 2){ Write-Host "" # Empty Write-Host " INTERFACE : $Interface" Write-Host " IP ADDRESS : $IP" Write-Host "" # Empty } ELSE{ For ($i=0;$i -lt $IP.Count;$i++){ Write-Host "" # Empty Write-Host " INTERFACE :"$Interface[$i] Write-Host " DESCRIPTION :"(Get-NetAdapter -Name $Interface[$i]).InterfaceDescription Write-Host " IP ADDRESS :"$IP[$i] } } Write-Host "" # Empty }