Powershell Classes – Export and Import Roundup

Hi together,

today i want so show you a little roundup for classes with functions – exporting and importing them.

A simple class in Powershell would look like this:

class MyObject
{
    [string] $ComputerName;
    [string] $Model;
    [string] $Manufacturer;
    [string] $BIOSSerial;
    [string] $OSArchitecture;
    [string] $OSVersion;
    [string] $ProcArchitecture;
}

You can export the classes to its own files and load it with the dot command to load the class types you need in a script:

#Folder of classes
$folder = $PSScriptRoot
. "$folder\ClassMyObject.ps1"

To work with them you could create and fill its properties with splatting – also you see in the code afterwards how to export and import them into a new variable:

class MyObject
{
    [string] $ComputerName;
    [string] $Model;
    [string] $Manufacturer;
    [string] $BIOSSerial;
    [string] $OSArchitecture;
    [string] $OSVersion;
    [string] $ProcArchitecture;

   # Instance method
   [String] GetComputername()
   {
       return $($this.ComputerName)
   }
}

$os = Get-WmiObject –Class Win32_OperatingSystem –comp localhost
$cs = Get-WmiObject –Class Win32_ComputerSystem –comp localhost
$bios = Get-WmiObject –Class Win32_BIOS –comp localhost
$proc = Get-WmiObject –Class Win32_Processor –comp localhost | Select-Object –First 1
$props = @{OSVersion=$os.version
    Model=$cs.model
    Manufacturer=$cs.manufacturer
    BIOSSerial=$bios.serialnumber
    ComputerName=$os.CSName
    OSArchitecture=$os.osarchitecture
ProcArchitecture=$proc.addresswidth}
$obj =  New-Object –TypeName MyObject –Property $props

#Exporting the instanciated class object
$obj | Export-Clixml -Path c:\Temp\MyObject.xml

#Importing the class object
[MyObject]$importedObject = Import-Clixml -Path c:\Temp\MyObject.xml

Write-Output $obj
#proving type
Write-Output $obj.GetType().Name
$obj | Format-Custom
$obj.PsObject.Typenames[0]

Herfore we made use of the functions Export-Clixml and Import-Clixml.


Now i want to show you 2 types of functions/methods.

Instance methods require the the class object to be instanciated previously. For example – the following “InfoObject” has to be created to return its spooler. Without being created it would not have the possibily to work with its properties.

Also i want to show you static functions. Static functions normally get parameters and return some kind of calculated result values. In this example i only return the first service of the actual system. The difference is that you don´t have to instanciate the class previously. You could build a “helper class” which gives you some functions which you need in a daily use. An example herefore – If you want to prove on a existing system which of 10 specific ports are open or not you could build easily a function in a helper class “NetworkingUtils” and use it every time you need it. By this you can work cleaner and your code gets lot smaller. The possibility to create bugs would also lower because you use the same functions in a every time and therefore you can fix them in a single code area.


# Define a class
class InfoObject
{
    $Processes

    $Services

   # Constructor
   InfoObject ()
   {
        # Gathering all Info as Powershell Objects and store it in My Powershell Object ($SysInfo)
        $this.Processes= Get-Process
        $this.Services= Get-Service
   }

   # Static method
   static [System.ServiceProcess.ServiceController] GetService()
   {
       return $(Get-Service)[0]
   }

   # Instance method
   [System.ServiceProcess.ServiceController] GetSpooler()
   {
        $returnvalue = $this.Services| Where-Object { $_.Name -eq 'Spooler'}
       return $returnvalue
   }
}
$props = @{Processes=Get-Process
Services = Get-Service}
$objekt = New-Object –TypeName InfoObject –Property $props
$objekt.GetSpooler()

#How you work with the static class - you load the type and afterwars you can use it like this
[InfoObject]::GetService()

I hope you make use of this nice features of Powershell v5.
Powershell gets more and more functions which devs love and enrich the daily use in a beautyfull manner. #NeverStopLearning

Best regards,

David

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s