{"id":8059,"date":"2021-01-19T11:35:15","date_gmt":"2021-01-19T16:35:15","guid":{"rendered":"https:\/\/jdhitsolutions.com\/blog\/?p=8059"},"modified":"2021-01-19T11:35:21","modified_gmt":"2021-01-19T16:35:21","slug":"solving-the-powershell-conversion-challenge","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/","title":{"rendered":"Solving the PowerShell Conversion Challenge"},"content":{"rendered":"\n<p>Today let's look at how I approached the first Iron Scripter <a href=\"https:\/\/ironscripter.us\/a-powershell-conversion-challenge\/\" target=\"_blank\" rel=\"noreferrer noopener\">PowerShell challenge of the year.<\/a> The goal of the challenge was to convert or translate an object into a PowerShell class definition. If you are new to these challenges, the journey to how you achieve the goal is more valuable than the end result. The challenges are designed to be learning exercises.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Start with a Single Object<\/h2>\n\n\n\n<p>To define a new class of object, I need a single object to serve as my template.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$obj&nbsp;=&nbsp;Get-CimInstance&nbsp;win32_operatingsystem&nbsp;-ComputerName&nbsp;localhost<\/code><\/pre>\n\n\n\n<p>In this particular example, I could also use Get-CimClass to list property names. But since I want to be able to define a class from any type of object, I'll ignore this alternative. <\/p>\n\n\n\n<p>Instead I'll rely on a hidden PowerShell feature. One of the reasons PowerShell is easy to use is that it abstracts or hides a lot of the .NET developer stuff. This is great because it means you don't have to be a developer to use PowerShell. However, as you gain experience, you can peel back some of these abstractions. That's what I am going to do. I'm going to get a generic <em>psobject <\/em>property.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"321\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-1024x321.png\" alt=\"psobject\" class=\"wp-image-8060\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-1024x321.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-300x94.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-768x240.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-1536x481.png 1536w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-2048x641.png 2048w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-850x266.png 850w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>This is a more generic representation of the object. Let's dive deeper.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-properites.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"332\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-properites-1024x332.png\" alt=\"\" class=\"wp-image-8061\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-properites-1024x332.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-properites-300x97.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-properites-768x249.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-properites-850x275.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-properites.png 1522w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>I can see the property name and its type which will be helpful in defining my class. The PowerShell class definition will look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">class myClass {\n[typename]$PropertyName\n...\n}<\/code><\/pre>\n\n\n\n<p>I can define the property line using the -f operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$prop&nbsp;=&nbsp;$obj.PSobject.properties&nbsp;|&nbsp;Select-Object&nbsp;-First&nbsp;1\n\"[{0}]`${1}\"&nbsp;-f&nbsp;$prop.TypeNameOfValue,$prop.name<\/code><\/pre>\n\n\n\n<p>The {0} and {1} entries are placeholders and they are filled in using the values on the right side of the -f operator. I'm also escaping the $ so it is treated literally. The created string is <code>[System.Boolean]$PSShowComputerName<\/code><\/p>\n\n\n\n<p>With this in mind, I can generate a complete class version of the object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$obj.psobject.properties&nbsp;|&nbsp;ForEach-Object&nbsp;-Begin&nbsp;{\n$class&nbsp;=&nbsp;@\"\nclass&nbsp;myOS&nbsp;{\n#properties\n\n\n\"@\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;-Process&nbsp;{&nbsp;$class&nbsp;+=&nbsp;\"[{0}]`${1}`n\"&nbsp;-f&nbsp;$_.TypeNameOfValue,&nbsp;$_.name&nbsp;}&nbsp;-End&nbsp;{&nbsp;$class&nbsp;+=&nbsp;\"}\"&nbsp;}<\/code><\/pre>\n\n\n\n<p>This is what I end up with.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">class myOS {\n properties\n [System.Boolean]$PSShowComputerName\n [string]$Caption\n [string]$Description\n [CimInstance#DateTime]$InstallDate\n [string]$Name\n [string]$Status\n [string]$CreationClassName\n [string]$CSCreationClassName\n [string]$CSName\n [int16]$CurrentTimeZone\n [bool]$Distributed\n [uint64]$FreePhysicalMemory\n [uint64]$FreeSpaceInPagingFiles\n [uint64]$FreeVirtualMemory\n [CimInstance#DateTime]$LastBootUpTime\n [CimInstance#DateTime]$LocalDateTime\n [uint32]$MaxNumberOfProcesses\n [uint64]$MaxProcessMemorySize\n [uint32]$NumberOfLicensedUsers\n [uint32]$NumberOfProcesses\n [uint32]$NumberOfUsers\n [uint16]$OSType\n [string]$OtherTypeDescription\n [uint64]$SizeStoredInPagingFiles\n [uint64]$TotalSwapSpaceSize\n [uint64]$TotalVirtualMemorySize\n [uint64]$TotalVisibleMemorySize\n [string]$Version\n [string]$BootDevice\n [string]$BuildNumber\n [string]$BuildType\n [string]$CodeSet\n [string]$CountryCode\n [string]$CSDVersion\n [bool]$DataExecutionPrevention_32BitApplications\n [bool]$DataExecutionPrevention_Available\n [bool]$DataExecutionPrevention_Drivers\n [byte]$DataExecutionPrevention_SupportPolicy\n [bool]$Debug\n [uint32]$EncryptionLevel\n [byte]$ForegroundApplicationBoost\n [uint32]$LargeSystemCache\n [string]$Locale\n [string]$Manufacturer\n [string[]]$MUILanguages\n [uint32]$OperatingSystemSKU\n [string]$Organization\n [string]$OSArchitecture\n [uint32]$OSLanguage\n [uint32]$OSProductSuite\n [bool]$PAEEnabled\n [string]$PlusProductID\n [string]$PlusVersionNumber\n [bool]$PortableOperatingSystem\n [bool]$Primary\n [uint32]$ProductType\n [string]$RegisteredUser\n [string]$SerialNumber\n [uint16]$ServicePackMajorVersion\n [uint16]$ServicePackMinorVersion\n [uint32]$SuiteMask\n [string]$SystemDevice\n [string]$SystemDirectory\n [string]$SystemDrive\n [string]$WindowsDirectory\n [string]$PSComputerName\n [Microsoft.Management.Infrastructure.CimClass]$CimClass\n [Microsoft.Management.Infrastructure.Generic.CimKeyedCollection`1[[Microsoft.Management.Infrastructure.CimProperty, Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]]$CimInstanceProperties\n [Microsoft.Management.Infrastructure.CimSystemProperties]$CimSystemProperties\n }<\/code><\/pre>\n\n\n\n<p>I probably don't need all those properties and I'll handle that in a bit. But first let me try this with a different type of object and refine my technique.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$properties\u00a0=\u00a0\"Name\",\"Displayname\",\"MachineName\",\"StartType\"\n(Get-Service\u00a0bits).psobject.properties\u00a0|\nWhere-Object\u00a0{$properties\u00a0-contains\u00a0$_.name}\u00a0|\nForEach-Object\u00a0-Begin\u00a0{\n$class\u00a0=\u00a0@\"\nclass\u00a0mySvc\u00a0{\n#properties\n\n\n\"@\n}\u00a0-Process\u00a0{\u00a0$class\u00a0+=\u00a0\"[{0}]`${1}`n\"\u00a0-f\u00a0$_.TypeNameOfValue,\u00a0$_.name\u00a0}\u00a0-End\u00a0{\u00a0$class\u00a0+=\u00a0\"}\"\u00a0}<\/code><\/pre>\n\n\n\n<p>And my resulting class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">class mySvc {\n properties\n [System.String]$Name\n [System.String]$DisplayName\n [System.String]$MachineName\n [System.ServiceProcess.ServiceStartMode]$StartType\n }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Convert-ObjectToClass<\/h2>\n\n\n\n<p>Now that I have working code from a prompt, i can wrap up in a function and add features.<\/p>\n\n\n\n<pre title=\"Convert-ObjecttoClass\" class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Function\u00a0Convert-ObjectToClass\u00a0{\n\u00a0\u00a0\u00a0\u00a0[cmdletbinding()]\n\u00a0\u00a0\u00a0\u00a0[outputType([String])]\n\u00a0\u00a0\u00a0\u00a0Param(\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[Parameter(<em>Position<\/em>\u00a0=\u00a00,\u00a0<em>Mandatory<\/em>,\u00a0<em>ValueFromPipeline<\/em>)]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[object]$InputObject,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[Parameter(<em>Mandatory<\/em>,\u00a0<em>HelpMessage<\/em>\u00a0=\u00a0\"Enter\u00a0the\u00a0name\u00a0of\u00a0your\u00a0new\u00a0class\")]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[string]$Name,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[string[]]$Properties,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[string[]]$Exclude\n\u00a0\u00a0\u00a0\u00a0)\n\u00a0\u00a0\u00a0\u00a0Begin\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Starting\u00a0$($myinvocation.MyCommand)\"\n\u00a0\u00a0\u00a0\u00a0}\u00a0<em>#begin<\/em>\n\n\u00a0\u00a0\u00a0\u00a0Process\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Converting\u00a0existing\u00a0type\u00a0$($InputObject.getType().Fullname)\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#create\u00a0a\u00a0list\u00a0to\u00a0hold\u00a0properties<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$prop\u00a0=\u00a0[system.collections.generic.list[object]]::new()\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#define\u00a0the\u00a0class\u00a0here-string<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$myclass\u00a0=\u00a0@\"\n\n#\u00a0this\u00a0class\u00a0is\u00a0derived\u00a0from\u00a0$($InputObject.getType().Fullname)\nclass\u00a0$Name\u00a0{\n#properties\n\n\"@\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#get\u00a0the\u00a0required\u00a0properties<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($Properties)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$InputObject.psobject.properties\u00a0|\u00a0Where-Object\u00a0{\u00a0$Properties\u00a0-contains\u00a0$_.name\u00a0}\u00a0|\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Select-Object\u00a0-Property\u00a0Name,\u00a0TypeNameOfValue\u00a0|\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ForEach-Object\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Adding\u00a0$($_.name)\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$prop.Add($_)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0<em>#foreaach<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0<em>#if\u00a0Properties<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Adding\u00a0all\u00a0properties\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$InputObject.psobject.properties\u00a0|\u00a0Select-Object\u00a0-Property\u00a0Name,\u00a0TypeNameOfValue\u00a0|\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ForEach-Object\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Adding\u00a0$($_.name)\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$prop.Add($_)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0<em>#foreach<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0<em>#else\u00a0all<\/em>\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($Exclude)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0foreach\u00a0($item\u00a0in\u00a0$Exclude)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Excluding\u00a0$item\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#remove\u00a0properties\u00a0that\u00a0are\u00a0tagged\u00a0as\u00a0excluded\u00a0from\u00a0the\u00a0list<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[void]$prop.remove($($prop.where({\u00a0$_.name\u00a0-like\u00a0$item\u00a0})))\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Processing\u00a0$($prop.count)\u00a0properties\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0foreach\u00a0($item\u00a0in\u00a0$prop)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#add\u00a0the\u00a0property\u00a0definition\u00a0name\u00a0to\u00a0the\u00a0class<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#e.g.\u00a0[string]$Name<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$myclass\u00a0+=\u00a0\"[{0}]`${1}`n\"\u00a0-f\u00a0$item.TypeNameOfValue,\u00a0$item.name\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#add\u00a0placeholder\u00a0content\u00a0to\u00a0the\u00a0class\u00a0definition<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$myclass\u00a0+=\u00a0@\"\n\n#Methods\u00a0can\u00a0be\u00a0inserted\u00a0here\n\u00a0\u00a0\u00a0\u00a0&lt;#\n\u00a0\u00a0\u00a0\u00a0[returntype]\u00a0MethodName(&lt;parameters>)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0code\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0value\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0#>\n\n#constructor\u00a0placeholder\n$Name()\u00a0{\n\u00a0\u00a0\u00a0\u00a0#insert\u00a0code\u00a0here\n}\n\n}\u00a0#close\u00a0class\u00a0definition\n\"@\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#if\u00a0running\u00a0VS\u00a0Code\u00a0or\u00a0the\u00a0PowerShell\u00a0ISE,\u00a0copy\u00a0the\u00a0class\u00a0to\u00a0the\u00a0clipboard<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#this\u00a0code\u00a0could\u00a0be\u00a0modified\u00a0to\u00a0insert\u00a0the\u00a0class\u00a0into\u00a0the\u00a0current\u00a0document<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($host.name\u00a0-match\u00a0\"ISE|Visual\u00a0Studio\u00a0Code\"\u00a0)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$myClass\u00a0|\u00a0Set-Clipboard\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$myClass\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Host\u00a0\"The\u00a0class\u00a0definition\u00a0has\u00a0been\u00a0copied\u00a0to\u00a0the\u00a0Windows\u00a0clipboard.\"\u00a0-ForegroundColor\u00a0green\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$myClass\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0}\u00a0<em>#process<\/em>\n\u00a0\u00a0\u00a0\u00a0End\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Ending\u00a0$($myinvocation.MyCommand)\"\n\u00a0\u00a0\u00a0\u00a0}\u00a0<em>#end<\/em>\n}<\/code><\/pre>\n\n\n\n<p>The function name is up for debate. The name I'm  using is really a literal representation of what the function will accomplish. But \"ObjectToClass\" is a weird noun. A better name might be <strong>ConvertTo-PSClass<\/strong>. An argument could also be made to use a different verb such as <strong>Out <\/strong>or <strong>Export<\/strong>. These are design considerations not to be overlooked.<\/p>\n\n\n\n<p>My function includes PowerShell code to detect if it is running in the PowerShell ISE or VS Code and if so, to copy the class to the clipboard.  I am a little hesitant to include this in the function because this is a separate task from defining the class and PowerShell functions should do only one thing. But, this code doesn't add any pipelined output, and from my perspective, I'm running the function to create a class definition that I can use in a scripting project.<\/p>\n\n\n\n<p>I can use it like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Get-Ciminstance\u00a0win32_Operatingsystem\u00a0|\u00a0Convert-ObjectToClass\u00a0-Properties\u00a0Caption,CSName,Version,InstallDate\u00a0-Name\u00a0myOS<\/code><\/pre>\n\n\n\n<p>The class definition is far from perfect and I'm expecting to have to refine it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">this class is derived from Microsoft.Management.Infrastructure.CimInstance\n class myOS {\n properties\n [string]$Caption\n [CimInstance#DateTime]$InstallDate\n [string]$CSName\n [string]$Version\n Methods can be inserted here\n <code>&lt;# [returntype] MethodName(&lt;parameters>) {     code     return value } #><\/code>\n constructor placeholder\n myOS() {\n     #insert code here\n }\n } #close class definition<\/code><\/pre>\n\n\n\n<p>For example, the InstallDate needs to be a [DateTime] object. I also know I'll need to rename some properties such as $CSName to $Computername. But my function got me started. <\/p>\n\n\n\n<p>Since I've started, I might as well give you a taste of the entire process. Here's my revised class definition.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">class\u00a0myOS\u00a0{\n\u00a0\u00a0\u00a0\u00a0<em>#properties<\/em>\n\u00a0\u00a0\u00a0\u00a0[string]$Name\n\u00a0\u00a0\u00a0\u00a0[DateTime]$InstallDate\n\u00a0\u00a0\u00a0\u00a0[string]$Computername\u00a0=\u00a0$env:COMPUTERNAME\n\u00a0\u00a0\u00a0\u00a0[string]$Version\n\u00a0\u00a0\u00a0\u00a0[datetime]$AuditDate\u00a0=\u00a0(Get-Date)\n\n\u00a0\u00a0\u00a0\u00a0<em>#Methods\u00a0can\u00a0be\u00a0inserted\u00a0here<\/em>\n\n\u00a0\u00a0\u00a0\u00a0hidden\u00a0[timespan]\u00a0GetInstallAge()\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$time\u00a0=\u00a0New-TimeSpan\u00a0-Start\u00a0$this.InstallDate\u00a0-End\u00a0(Get-Date)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0$time\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0<em>#constructor\u00a0placeholder<\/em>\n\u00a0\u00a0\u00a0\u00a0myOS([string]$Computername)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$os\u00a0=\u00a0Get-CimInstance\u00a0-ClassName\u00a0Win32_OperatingSystem\u00a0-ComputerName\u00a0$Computername\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this.name\u00a0=\u00a0$os.caption\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this.version\u00a0=\u00a0$os.Version\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this.installDate\u00a0=\u00a0$os.InstallDate\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this.Computername\u00a0=\u00a0$Computername\n\u00a0\u00a0\u00a0\u00a0}\n\n}\u00a0<em>#close\u00a0class\u00a0definition<\/em><\/code><\/pre>\n\n\n\n<p>Because I'm defining a custom object, I can also define type and format extensions, although I'm skipping the latter for now.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Update-TypeData\u00a0-TypeName\u00a0myOS\u00a0-MemberType\u00a0ScriptProperty\u00a0-MemberName\u00a0InstallAge\u00a0-Value\u00a0{\u00a0$this.GetInstallAge()\u00a0}\u00a0-Force<\/code><\/pre>\n\n\n\n<p>The last thing I need is an easy way for a user to use the class. I don't want to force the user to have to use the raw class definition. Instead, create your own helper functions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Function\u00a0Get-OS\u00a0{\n\u00a0\u00a0\u00a0\u00a0[cmdletbinding()]\n\u00a0\u00a0\u00a0\u00a0[Outputtype(\"myOS\")]\n\u00a0\u00a0\u00a0\u00a0Param([string[]]$Computername\u00a0=\u00a0$env:COMPUTERNAME)\n\n\u00a0\u00a0\u00a0\u00a0foreach\u00a0($computer\u00a0in\u00a0$computername)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0New-Object\u00a0-TypeName\u00a0myOS\u00a0-ArgumentList\u00a0$computer\n\u00a0\u00a0\u00a0\u00a0}\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-os.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"676\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-os-1024x676.png\" alt=\"\" class=\"wp-image-8064\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-os-1024x676.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-os-300x198.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-os-768x507.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-os-350x230.png 350w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-os-850x561.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/get-os.png 1212w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>I have to admit, I might actually use this function. This is definitely not beginner-level PowerShell so if you have questions, please don't be shy and ask in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today let&#8217;s look at how I approached the first Iron Scripter PowerShell challenge of the year. The goal of the challenge was to convert or translate an object into a PowerShell class definition. If you are new to these challenges, the journey to how you achieve the goal is more valuable than the end result&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Just posted: Solving the #PowerShell Conversion Challenge","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8],"tags":[516,224,618,534],"class_list":["post-8059","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-classes","tag-function","tag-iron-scripter","tag-powershell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Solving the PowerShell Conversion Challenge &#8226; The Lonely Administrator<\/title>\n<meta name=\"description\" content=\"Here&#039;s my take on a recent Iron Scripter challenge to convert a PowerShell object into a PowerShell class definition.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Solving the PowerShell Conversion Challenge &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"Here&#039;s my take on a recent Iron Scripter challenge to convert a PowerShell object into a PowerShell class definition.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-19T16:35:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-19T16:35:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-1024x321.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8059\\\/solving-the-powershell-conversion-challenge\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8059\\\/solving-the-powershell-conversion-challenge\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Solving the PowerShell Conversion Challenge\",\"datePublished\":\"2021-01-19T16:35:15+00:00\",\"dateModified\":\"2021-01-19T16:35:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8059\\\/solving-the-powershell-conversion-challenge\\\/\"},\"wordCount\":677,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8059\\\/solving-the-powershell-conversion-challenge\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/psobject-1024x321.png\",\"keywords\":[\"Classes\",\"Function\",\"Iron Scripter\",\"PowerShell\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8059\\\/solving-the-powershell-conversion-challenge\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8059\\\/solving-the-powershell-conversion-challenge\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8059\\\/solving-the-powershell-conversion-challenge\\\/\",\"name\":\"Solving the PowerShell Conversion Challenge &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8059\\\/solving-the-powershell-conversion-challenge\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8059\\\/solving-the-powershell-conversion-challenge\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/psobject-1024x321.png\",\"datePublished\":\"2021-01-19T16:35:15+00:00\",\"dateModified\":\"2021-01-19T16:35:21+00:00\",\"description\":\"Here's my take on a recent Iron Scripter challenge to convert a PowerShell object into a PowerShell class definition.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8059\\\/solving-the-powershell-conversion-challenge\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8059\\\/solving-the-powershell-conversion-challenge\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8059\\\/solving-the-powershell-conversion-challenge\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/psobject.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/psobject.png\",\"width\":2460,\"height\":770},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8059\\\/solving-the-powershell-conversion-challenge\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Solving the PowerShell Conversion Challenge\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Solving the PowerShell Conversion Challenge &#8226; The Lonely Administrator","description":"Here's my take on a recent Iron Scripter challenge to convert a PowerShell object into a PowerShell class definition.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/","og_locale":"en_US","og_type":"article","og_title":"Solving the PowerShell Conversion Challenge &#8226; The Lonely Administrator","og_description":"Here's my take on a recent Iron Scripter challenge to convert a PowerShell object into a PowerShell class definition.","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/","og_site_name":"The Lonely Administrator","article_published_time":"2021-01-19T16:35:15+00:00","article_modified_time":"2021-01-19T16:35:21+00:00","og_image":[{"url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-1024x321.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Solving the PowerShell Conversion Challenge","datePublished":"2021-01-19T16:35:15+00:00","dateModified":"2021-01-19T16:35:21+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/"},"wordCount":677,"commentCount":1,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-1024x321.png","keywords":["Classes","Function","Iron Scripter","PowerShell"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/","name":"Solving the PowerShell Conversion Challenge &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject-1024x321.png","datePublished":"2021-01-19T16:35:15+00:00","dateModified":"2021-01-19T16:35:21+00:00","description":"Here's my take on a recent Iron Scripter challenge to convert a PowerShell object into a PowerShell class definition.","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/01\/psobject.png","width":2460,"height":770},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8059\/solving-the-powershell-conversion-challenge\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Solving the PowerShell Conversion Challenge"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":8107,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8107\/scripting-challenge-meetup\/","url_meta":{"origin":8059,"position":0},"title":"Scripting Challenge Meetup","author":"Jeffery Hicks","date":"February 1, 2021","format":false,"excerpt":"As you probably know, I am the PowerShell problem master behind the challenges from the Iron Scripter site. Solving a PowerShell scripting challenge is a great way to test your skills and expand your knowledge. The final result is merely a means to an end. How you get there and\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/rubik.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":7489,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7489\/powershell-word-play\/","url_meta":{"origin":8059,"position":1},"title":"PowerShell Word Play","author":"Jeffery Hicks","date":"May 19, 2020","format":false,"excerpt":"A few weeks ago an Iron Scripter PowerShell challenge was issued that involved playing with words and characters. Remember, the Iron Scripter challenges aren't intended to create meaningful, production worthy code. They are designed to help you learn PowerShell fundamentals and scripting techniques. This particular challenge was aimed at beginner\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":9018,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/9018\/an-iron-scripter-warm-up-solution\/","url_meta":{"origin":8059,"position":2},"title":"An Iron Scripter Warm-Up Solution","author":"Jeffery Hicks","date":"May 6, 2022","format":false,"excerpt":"We just wrapped up the 2022 edition of the PowerShell+DevOps Global Summit. It was terrific to be with passionate PowerShell professionals again. The culmination of the event is the Iron Scripter Challenge. You can learn more about this year's event and winner here. But there is more to the Iron\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7680,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7680\/friday-fun-back-to-school-with-powershell\/","url_meta":{"origin":8059,"position":3},"title":"Friday Fun: Back to School with PowerShell","author":"Jeffery Hicks","date":"September 11, 2020","format":false,"excerpt":"For today's fun with PowerShell, I thought I'd share my solutions for a recent Iron Scripter challenge. If you aren't familiar with these challenges, and you should be, they are designed to test your PowerShell skills and hopefully help you learn something new. There are challenges for all skill levels\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-cylindervolume-format.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-cylindervolume-format.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-cylindervolume-format.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-cylindervolume-format.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":7559,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7559\/an-expanded-powershell-scripting-inventory-tool\/","url_meta":{"origin":8059,"position":4},"title":"An Expanded PowerShell Scripting Inventory Tool","author":"Jeffery Hicks","date":"June 19, 2020","format":false,"excerpt":"The other day I shared my code that I worked up to solve an Iron Scripter PowerShell challenge. One of the shortcomings was that I didn't address a challenge to include a property that would indicate what file was using a given command. I also felt I could do better\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":8236,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8236\/solving-another-powershell-math-challenge\/","url_meta":{"origin":8059,"position":5},"title":"Solving Another PowerShell Math Challenge","author":"Jeffery Hicks","date":"March 22, 2021","format":false,"excerpt":"Last month, the Iron Scripter Chairman posted a \"fun\" PowerShell scripting challenge. Actually, a few math-related challenges . As with all these challenges, the techniques and concepts you use to solve the challenge are more important than the result itself. Here's how I approached the problems. Problem #1 The first\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/get-possiblesum.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/get-possiblesum.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/get-possiblesum.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8059","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8059"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8059\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}