Answered by:
PowerShell Scripting - From a 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
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 }
- Edited by Nevets24 Wednesday, March 27, 2019 8:27 PM More clear.
Wednesday, March 27, 2019 8:10 PM
Answers
-
Here is a very good place to hone your skills and maintain a standard approach to your scripting:
The "Script Analyzer" module can also give you excellent help with your scripts:
Install-Module PsScriptAnalyzer
\_(ツ)_/
- Marked as answer by Nevets24 Thursday, March 28, 2019 5:49 PM
Wednesday, March 27, 2019 8:47 PM
All replies
-
Here is a very good place to hone your skills and maintain a standard approach to your scripting:
The "Script Analyzer" module can also give you excellent help with your scripts:
Install-Module PsScriptAnalyzer
\_(ツ)_/
- Marked as answer by Nevets24 Thursday, March 28, 2019 5:49 PM
Wednesday, March 27, 2019 8:47 PM -
The following line is bad:
-OR ($_.InterfaceAlias -like 'Local*')} `
Do not use line extenders. Rad the docs at the link I posted to see all of the best ways to extend over multiple lines.
\_(ツ)_/
Wednesday, March 27, 2019 8:49 PM -
This would be the correct way to write that line on multiple lines:
$IPv4 = Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.IPAddress -notlike '169.254.*' -AND ( $_.InterfaceAlias -like 'Ethernet*' -OR $_.InterfaceAlias -like 'Wi*' -OR $_.InterfaceAlias -like 'Local*' ) }
Again - the style guide will discuss this and many more things. The script analyzer will point out structure, style and declaration errors.
\_(ツ)_/
Wednesday, March 27, 2019 8:53 PM -
This would be even better:
$IPv4 = Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.IPAddress -notlike '169.254.*' -AND $_.InterfaceAlias -match 'Ethernet|^Wi|^Local' }
\_(ツ)_/
- Edited by jrv Wednesday, March 27, 2019 8:56 PM
Wednesday, March 27, 2019 8:55 PM -
THis is another clean way to do this:
$IPv4 = Get-NetIPAddress -InterfaceAlias Ethernet,Wi*,Local* -AddressFamily IPv4 | Where-Object {$_.IPAddress -notlike '169.254.*'}
Always use the CmdLet methods first as they are usually faster and more specific. Filter only when the command doesn't support that requirement.
\_(ツ)_/
Wednesday, March 27, 2019 9:00 PM -
First off, thanks a lot. You provided many ways to improve a line that I knew wasn't that great.
Lots of lecture for the coming nights.
Thursday, March 28, 2019 5:27 PM