Tuesday, May 12, 2020

Creating PowerShell Custom Object to collect information within PS Script

Creating PowerShell Custom Object to collect information within PS Script

I wanted to post few examples of PS custom object to be used as an template to create custom PowerShell objects when needed, perhaps to have fun with. Using PS object method is pretty straight forward and simple. I will be listing two ways to create them. As long as you can change the variables and its collected information to fit into your needs, you could then pull available properties same way below template is using and export-CSV etc. to manipulate for your needs.

 

 

#(-)_.Collect information

$osInfo    = Get-CimInstance Win32_OperatingSystem

$compInfo  = Get-CimInstance Win32_ComputerSystem

$diskInfo  = Get-CimInstance Win32_LogicalDisk

 

 

Here is the template you can use to get started.

 

 

#(1)_.Collect Computer Information

$ComputerName = $env:COMPUTERNAME

$CompInfo = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName

$OSinfo   = Get-WmiObject -class win32_OperatingSystem -ComputerName $ComputerName

$BiosInfo = Get-WmiObject -class win32_BIOS -ComputerName $ComputerName

$diskInfo = Get-CimInstance Win32_LogicalDisk

$NicInfo  = Get-NetIPAddress -CimSession $computername -AddressFamily IPv4 | `

                            ?{ $_.InterfaceAlias -notmatch 'Loopback'}

 

 

#(2)_.Object type and a complete listing of its members.

$CompInfo |  gm | select name | Out-GridView

 

 

PS Object # Ordered Hashtable

 

#(4)_.Ordered Hashtable

$objectProperty = [ordered]@{

 

    ComputerName        = $compInfo.Name

    Domain              = $compInfo.Domain

    Workgroup           = $compInfo.Workgroup

    DomainJoined        = $compInfo.PartOfDomain

    OS                  = $osInfo.Caption

    OSVersion           = $osInfo.Version

    Disks               = $diskInfo

    IPaddresses         = $NicInfo.IPaddress

 

}

 

$PSObject1 = New-Object -TypeName psobject -Property $objectProperty

$PSObject1

 

 

 

PS Object # PSCustomObject

 

 

# PSCUSTOMOBJECT

$PSObject2 = [PSCustomObject]@{

 

    ComputerName        = $compInfo.Name

    Domain              = $compInfo.Domain

    Workgroup           = $compInfo.Workgroup

    DomainJoined        = $compInfo.PartOfDomain

    OS                  = $osInfo.Caption

    OSVersion           = $osInfo.Version

    Disks               = $diskInfo

    IPaddresses         = $NicInfo.IPaddress

 

}

 

$PSObject2

 

 

PS Object # PSCustomObject

 

$PSObject3 =@{

 

    ComputerName        = $compInfo.Name

    OS                  = $osInfo.Caption

    OSVersion           = $osInfo.Version

    Domain              = $compInfo.Domain

    Workgroup           = $compInfo.Workgroup

    DomainJoined        = $compInfo.PartOfDomain

 

}

 

 

 

Azure Solutions Architect

AWS Certified Cloud Practitioner

Azure Certified Security Engineer Associate

https://simplepowershell.blogspot.com

https://cloudsec365.blogspot.com

https://msazure365.blogspot.com

https://twitter.com/Message_Talk