Chapters/adding-custom-formatting/Solution/TMMachineInfo/TMMachineInfo.psm1
#requires -version 5.0 class TMMachineInfo { # Properties [string]$ComputerName [string]$OSVersion [string]$OSBuild [string]$Manufacturer [string]$Model [string]$Processors [string]$Cores [string]$RAM [string]$SystemFreeSpace [string]$Architecture hidden[datetime]$Date #Methods [void]Refresh() { Try { $params = @{ ComputerName = $this.Computername ErrorAction = 'Stop' } $session = New-CimSession @params #define a hashtable of parameters to splat #to Get-CimInstance $cimparams = @{ ClassName = 'Win32_OperatingSystem' CimSession = $session ErrorAction = 'stop' } $os = Get-CimInstance @cimparams $cimparams.Classname = 'Win32_ComputerSystem' $cs = Get-CimInstance @cimparams $cimparams.ClassName = 'Win32_Processor' $proc = Get-CimInstance @cimparams | Select-Object -first 1 $sysdrive = $os.SystemDrive $cimparams.Classname = 'Win32_LogicalDisk' $cimparams.Filter = "DeviceId='$sysdrive'" $drive = Get-CimInstance @cimparams $session | Remove-CimSession #use the computername from the CIM instance $this.ComputerName = $os.CSName $this.OSVersion = $os.version $this.OSBuild = $os.buildnumber $this.Manufacturer = $cs.manufacturer $this.Model = $cs.model $this.Processors = $cs.numberofprocessors $this.Cores = $cs.numberoflogicalprocessors $this.RAM = ($cs.totalphysicalmemory / 1GB) $this.Architecture = $proc.addresswidth $this.SystemFreeSpace = $drive.freespace $this.date = Get-Date } Catch { throw "Failed to connect to $this.computername. $($_.Exception.message)" } #try/catch } # Constructors TMMachineInfo([string]$ComputerName) { $this.ComputerName = $ComputerName $this.Refresh() } } #class Function Get-MachineInfo { [cmdletbinding()] [alias("gmi")] Param( [Parameter(Position = 0, ValueFromPipeline)] [Alias("cn")] [ValidateNotNullorEmpty()] [string[]]$Computername = $env:COMPUTERNAME ) Begin { Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)" } #begin Process { foreach ($computer in $computername) { Write-Verbose "[PROCESS] Getting machine information from $($computer.toUpper())" New-Object -TypeName TMMachineInfo -ArgumentList $computer } } #process End { Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)" } #end } Function Update-MachineInfo { [cmdletbinding()] [alias("umi")] Param( [Parameter(Position = 0, ValueFromPipeline)] [ValidateNotNullorEmpty()] [TMMachineInfo]$Info, [switch]$Passthru ) Begin { Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)" } #begin Process { Write-Verbose "[PROCESS] Refreshing: $(($Info.ComputerName).ToUpper())" $info.Refresh() if ($Passthru) { #write the updated object back to the pipeline $info } } #process End { Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)" } #end } |