Answered by:
PowerShell Function - Beginner

Question
-
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
# 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 }
Psssst !
I edited the code in order to show it all. I know there is an error at the $IPv4 if I leave it that way.
I know Write-Host '' # EMPTY are useless since I can use other methods.
- Edited by Nevets24 Wednesday, March 27, 2019 3:40 PM Wouldn't see all the code.
Wednesday, March 27, 2019 3:37 PM
Answers
-
Hello Steven,
This is the forum for Training and Certification, but there is a dedicated forum for scripting! I suggest you ask for advice in one of those forums :-)
Here's the forum for either Scripting or PowerShell:
Scripting > The Official Scripting Guys Forum!
or
Scripting > Windows PowerShell
Hope that helps!
Best regards,
LeonBlog:
https://thesystemcenterblog.com LinkedIn:
- Marked as answer by Nevets24 Friday, March 29, 2019 2:46 PM
Wednesday, March 27, 2019 7:27 PM
All replies
-
Hello Steven,
This is the forum for Training and Certification, but there is a dedicated forum for scripting! I suggest you ask for advice in one of those forums :-)
Here's the forum for either Scripting or PowerShell:
Scripting > The Official Scripting Guys Forum!
or
Scripting > Windows PowerShell
Hope that helps!
Best regards,
LeonBlog:
https://thesystemcenterblog.com LinkedIn:
- Marked as answer by Nevets24 Friday, March 29, 2019 2:46 PM
Wednesday, March 27, 2019 7:27 PM -
Oh thanks for the heads up !Wednesday, March 27, 2019 8:09 PM