PowerShell own objects

 

PowerShell cmdlets very often create an object or an object array (multiple objects in an array). In this post, I show how to create objects themselves and add properties to them.

what is an object?

An object has certain properties and methods. As an example for an object I take a server. The IP address and the hostname are properties of the server and therefore of the object. With methods something can be changed at the object. A method for a server could be for example a restart.

Creating an object

A new object can be created in PowerShell with the following command:

$server = New-Object -TypeName PSObject 

Alternatively, the object can be created with certain properties right away:

Creating an Object with Properties

$server = [PSCustomObject][Ordered]@{
    "IP" = "192.168.1.2"
    "FQDN" = "myServer.mydomain.mytld"
    }
$server
Result:

IP          FQDN
--          ----
192.168.1.2 myServer.mydomain.mytld

Adding Properties

Properties can be added to the object using the Add-Member command:

$server | Add-Member -Name "IP" -MemberType Noteproperty -Value $ip
$server | Add-Member -Name "FQDN" -MemberType Noteproperty -Value "$((Resolve-DnsName $ip).NameHost)"

Convert object into a hashtable

To convert an object into a hashtable, a simple Foreach loop can be used:

$HashTable = @{}
$server.psobject.properties | %{ $HashTable[$_.Name] = $_.Value }

A hashtable can in turn be converted to an object as follows:

Convert a Hashtable to an Object

New-Object -ArgumentList $server -TypeName psobject

Add an object to an array

For managing multiple servers, they can be combined into an array. I'll call the array $servers, a single server object: $server

The following command can be used to create an array for our server objects:

$servers = @()

So to add a server to the servers, the following command can be used:

$servers += $server

Multiple objects in a loop (loop)

Simple loop to call multiple IP addresses:

1..5 | foreach {
$ip="192.168.0.$_"
}

1..5 | foreach {
$ip="192.168.0.$_"
write-host $ip
}
Result:
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5

the variable $_ is filled with the values 1-5 i.e. 1,2,3,4,5:

Adding the objects:

$servers = @()
1..5 | foreach
{
$ip="192.168.0.$_"
$server = New-Object -TypeName PSObject
$server | Add-Member -Name "IP" -MemberType Noteproperty -Value $ip
$server | Add-Member -Name "FQDN" -MemberType Noteproperty -Value "$((Resolve-DnsName $ip).NameHost)"
$servers += $server
}

Show all properties of the objects

If there are several server objects in the array according to the example, a table of all servers and their properties can be displayed, for example:

$servers | Out-GridView

Exceptions

only certain objects of an object array:

$except=@("Einstellungen","Store")
Get-Process | Where-Object { 
            $except -notcontains $_.MainWindowTitle
        }
positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Questions / Comments


By continuing to browse the site, you agree to our use of cookies. More Details