{"id":862,"date":"2012-01-14T11:15:59","date_gmt":"2012-01-14T16:15:59","guid":{"rendered":"http:\/\/enterpriseadmins.org\/blog\/?p=862"},"modified":"2012-01-14T11:16:08","modified_gmt":"2012-01-14T16:16:08","slug":"simple-examples-of-powershell-pscustomobject","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/simple-examples-of-powershell-pscustomobject\/","title":{"rendered":"Simple examples of Powershell PSCustomObject"},"content":{"rendered":"<p>In most of my Powershell scripts I make use of the PSCustomObject to store results.  The example I first learned (and use all the time) looks just like this:<\/p>\n<pre><code class=\"language-1\">\r\n$item = \"\" | Select Name, RAM, CPU\r\n$item.Name = \"blah\"\r\n$item.Ram = 4096\r\n$item.Cpu = 1\r\n$item\r\n<\/code><\/pre>\n<p>In the above example, you create a blank PSCustomObject on line 1 which has Name, RAM and CPU placehoders.  Then on lines 2 through 4 you assign values to each attribute.  Finally on line 5 you do something with the variable $item.  In the above example, the contents of the $item variable are displayed onscreen.  Normally you&#8217;d see something like $myreport += $item which stores the new item in a collection called $myreport.  <\/p>\n<p>The problem is adding an additional attribute to your object requires two steps &#8212; first, you must declare the attribute on line 1 and then assign something to the attribute after it is declared.  For example, if I wanted to add PowerState to my report, I would have to add &#8220;, PowerState&#8221; on the first line.  If you forget, which happens to me all the time, you get this error:<\/p>\n<pre>\r\n$item.PowerState = \"PoweredOff\"\r\nProperty 'PowerState' cannot be found on this object; make sure it exists and is settable.\r\nAt line:1 char:7\r\n+ $item. <<<< PowerState = \"PoweredOff\"\r\n    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException\r\n    + FullyQualifiedErrorId : PropertyAssignmentException\r\n<\/pre>\n<p>I recently found a new example that I like a lot better.  Here is an example:<\/p>\n<pre><code class=\"language-1\">\r\n$item2 = new-object -type PSObject -Property @{\r\n\tName = \"blah\"\r\n\tRAM = 4096\r\n\tCPU = 1\r\n}\r\n$item2\r\n<\/code><\/pre>\n<p>In the above example, attributes are created as needed, removing the need to pre-define them on the first line of the script.  This method removes the need to specify \"$item.\" for each attribute, making the code easier to read\/maintain\/support.  <\/p>\n<p>This is a very simple example, but I hope others find it useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In most of my Powershell scripts I make use of the PSCustomObject to store results. The example I first learned (and use all the time) looks just like this: $item = &#8220;&#8221; | Select Name, RAM, CPU $item.Name = &#8220;blah&#8221; &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/simple-examples-of-powershell-pscustomobject\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"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,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-862","post","type-post","status-publish","format-standard","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=862"}],"version-history":[{"count":4,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/862\/revisions"}],"predecessor-version":[{"id":866,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/862\/revisions\/866"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}