{"id":2579,"date":"2013-11-12T00:01:00","date_gmt":"2013-11-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/11\/12\/combine-automation-tasks-by-using-powershell-objects\/"},"modified":"2013-11-12T00:01:00","modified_gmt":"2013-11-12T00:01:00","slug":"combine-automation-tasks-by-using-powershell-objects","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/combine-automation-tasks-by-using-powershell-objects\/","title":{"rendered":"Combine Automation Tasks by Using PowerShell Objects"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell objects to combine output from multiple WMI classes.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I registered for <a href=\"http:\/\/powershellsaturday.com\/007\/\" target=\"_blank\">Windows PowerShell Saturday #007<\/a>, which will be held in Charlotte, NC. I am going to be presenting one or two talks (now I am scheduled for two talks, but we may end up with a special guest, and if we do, I will give him one of my slots). The lineup of speakers is absolutely stellar. I know that the Scripting Wife is busy working on a blog post that will describe the speakers and their sessions. This event is going to be the most awesome event outside of the <a href=\"http:\/\/powershell.org\/wp\/community-events\/summit\/\" target=\"_blank\">Windows PowerShell Summit<\/a>.<\/p>\n<p><strong>Note&nbsp;&nbsp;<\/strong>Last week I wrote a three-part series that talked about Windows PowerShell objects:<\/p>\n<ul>\n<li>In&nbsp;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/11\/05\/a-powershell-object-lesson-part-one.aspx\" target=\"_blank\">A PowerShell Object Lesson: Part 1<\/a>, I talked about the advantage of using Windows PowerShell objects, and how they are helpful to the scripter.<\/li>\n<li>In&nbsp;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/11\/06\/a-powershell-object-lesson-part-2.aspx\" target=\"_blank\">A PowerShell Object Lesson: Part 2<\/a>, I talked about pipelining objects.<\/li>\n<li>In <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/a-powershell-object-lesson-part-3\/\" target=\"_blank\">A PowerShell Object Lesson: Part 3<\/a>, I talked about various ways to create objects.<\/li>\n<\/ul>\n<h2>Use objects to facilitate access to WMI data<\/h2>\n<p>There are thousands of WMI classes available in modern versions of Windows. The classes seem to vary in terms of usefulness, but they do represent a broad spectrum of classes. For some good articles about working with Windows PowerShell and WMI, see <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/wmi\/windows+powershell\/getting+started\/\" target=\"_blank\">this collection of Hey, Scipting Guy! Blog<\/a>&nbsp;posts.<\/p>\n<p>One of the problems with WMI is that the classes are narrowly defined. I mean, one class covers the computer hardware, and another class covers the computer operating system. I generally want to know about both. For example, if I have a version of Windows that is running in 32-bit mode, I generally want to know if the hardware will support 64-bit, because I may want to reinstall the operating system to take advantage of greater amounts of memory.<\/p>\n<p>An easy way to do this is to return a single object that contains the information I need.<\/p>\n<p><strong>Note&nbsp;<\/strong> In this script, I am using the <strong>Get-CimInstance<\/strong> cmdlet, which is available in Windows PowerShell 4.0 and Windows PowerShell 3.0. If you are still using Windows PowerShell&nbsp;2.0, substitute <strong>Get-WmiObject<\/strong> in the two places where I use <strong>Get-CimInstance<\/strong>.<\/p>\n<p>The complete CombineWMIClasses.ps1 script is shown here:<\/p>\n<p style=\"padding-left: 30px\"># CombineWMIClasses.ps1<\/p>\n<p style=\"padding-left: 30px\"># ed wilson, msft<\/p>\n<p style=\"padding-left: 30px\"># HSG-11-12-13<\/p>\n<p style=\"padding-left: 30px\"># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">$comp = Get-CimInstance -ClassName Win32_ComputerSystem<\/p>\n<p style=\"padding-left: 30px\">$os = Get-CimInstance -ClassName win32_OperatingSystem<\/p>\n<p style=\"padding-left: 30px\">$object = [pscustomobject]@{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Make = $comp.Manufacturer<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Model = $comp.Model<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; DNSName = &#8216;{0}.{1}&#8217; -f $comp.DNSHostName, $comp.Domain<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; SystemType = $comp.SystemType<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Caption = $os.Caption<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; InstallDate = $os.InstallDate<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; LastBootUpTime = $os.LastBootUpTime<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; OsArchitecture = $os.OSArchitecture<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; BuildNumber = $os.BuildNumber<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; ServicePack = &#8216;{0}.{1}&#8217; -f<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $os.ServicePackMajorVersion, $os.ServicePackMinorVersion}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$object<\/p>\n<h2>Gather the data<\/h2>\n<p>The first step is to gather the data. I decided to query two WMI classes and combine the output into a custom object that I need. The first command queries the <strong>Win32_ComputerSystem<\/strong> WMI class. I opened the Windows PowerShell console, and piped the results to the <strong>Format-List<\/strong> cmdlet to examine the information that is available from the class. The command is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-CimInstance Win32_ComputerSystem | Format-List *<\/p>\n<p style=\"padding-left: 30px\"><strong>Note&nbsp;<\/strong> One of the advantages of using the <strong>Get-CimInstance<\/strong> cmdlet is that tab expansion works for the WMI classes, so I do not have to type the entire class name. On my laptop, I type <strong>Win32_Computer&lt;tab&gt;<\/strong> and it picks up the class on the second Tab press.<\/p>\n<p>The output from the previous command is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2816.HSG-11-12-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2816.HSG-11-12-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>I have the Windows PowerShell console pushed off of my main screen to my secondary monitor, so it is easy to refer to the output while I decide which properties I need to access.<\/p>\n<p>I then do the same thing with the <strong>Win32_OperatingSystem<\/strong> WMI class as shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-CimInstance Win32_OperatingSystem | Format-List *<\/p>\n<p>The command and output appear in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1307.HSG-11-12-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1307.HSG-11-12-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Now, I decide to create the custom object. To do this, I use the <strong>[pscustomobject]<\/strong> type accelerator, and I pass a hash table to it with the properties I need. Here are the first three lines, where I gather the WMI data and I create the custom object:<\/p>\n<p style=\"padding-left: 30px\">$comp = Get-CimInstance -ClassName Win32_ComputerSystem<\/p>\n<p style=\"padding-left: 30px\">$os = Get-CimInstance -ClassName win32_OperatingSystem<\/p>\n<p style=\"padding-left: 30px\">$object = [pscustomobject]@{<\/p>\n<p>From the <strong>Win32_ComputerSystem<\/strong> WMI class, I need the make, model, system type, and DNS name of the computer. I decide to combine the host name and the domain name to create the fully qualified domain name of my system. Here is that portion of the script:<\/p>\n<p style=\"padding-left: 60px\">Make = $comp.Manufacturer<\/p>\n<p style=\"padding-left: 60px\">Model = $comp.Model<\/p>\n<p style=\"padding-left: 60px\">DNSName = &#8216;{0}.{1}&#8217; -f $comp.DNSHostName, $comp.Domain<\/p>\n<p style=\"padding-left: 60px\">SystemType = $comp.SystemType<\/p>\n<p>From the <strong>Win32_OperatingSystem<\/strong> WMI class, I want the caption of the Windows system, the install date, the last boot up, operating system architecture, build number, and service pack version. I decide to combine the major and minor service pack version information. Here is that portion of the script:<\/p>\n<p style=\"padding-left: 60px\">Caption = $os.Caption<\/p>\n<p style=\"padding-left: 60px\">InstallDate = $os.InstallDate<\/p>\n<p style=\"padding-left: 60px\">LastBootUpTime = $os.LastBootUpTime<\/p>\n<p style=\"padding-left: 60px\">OsArchitecture = $os.OSArchitecture<\/p>\n<p style=\"padding-left: 60px\">BuildNumber = $os.BuildNumber<\/p>\n<p style=\"padding-left: 60px\">ServicePack = &#8216;{0}.{1}&#8217; -f<\/p>\n<p style=\"padding-left: 60px\">&nbsp;$os.ServicePackMajorVersion, $os.ServicePackMinorVersion}<\/p>\n<p>When I run the script, it returns a nice object. I could then do anything I want to do with this new object.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0435.HSG-11-12-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0435.HSG-11-12-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to using Windows PowerShell to create objects from multiple WMI classes. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell objects to combine output from multiple WMI classes. Microsoft Scripting Guy, Ed Wilson, is here. I registered for Windows PowerShell Saturday #007, which will be held in Charlotte, NC. I am going to be presenting one or two talks (now I am scheduled for [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,409,3,4,45,6],"class_list":["post-2579","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-objects","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell objects to combine output from multiple WMI classes. Microsoft Scripting Guy, Ed Wilson, is here. I registered for Windows PowerShell Saturday #007, which will be held in Charlotte, NC. I am going to be presenting one or two talks (now I am scheduled for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2579"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2579\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}