{"id":54783,"date":"2008-12-01T11:29:00","date_gmt":"2008-12-01T11:29:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/12\/01\/hey-scripting-guy-how-can-i-have-computer-configuration-information-written-to-an-html-file\/"},"modified":"2008-12-01T11:29:00","modified_gmt":"2008-12-01T11:29:00","slug":"hey-scripting-guy-how-can-i-have-computer-configuration-information-written-to-an-html-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-have-computer-configuration-information-written-to-an-html-file\/","title":{"rendered":"Hey, Scripting Guy! How Can I Have Computer Configuration Information Written to an HTML File?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! I need to obtain some basic configuration information from my computers on the network. It would be nice if the script would write the output in an HTML file that we could copy to our internal Web server. This would give us easy access to the information. Whadda ya say?<BR><BR>&#8211; JE<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hi JE,<\/P>\n<P>This morning, it is really cold, gray, and <A href=\"http:\/\/encarta.msn.com\/dictionary_1861733434\/yucky.html\" target=\"_blank\">yucky<\/A> outside. The weather makes me feel as if I am in Seattle, Washington, in the United States, instead of Charlotte, North Carolina. With enough gray outside, there is no reason to invite <A href=\"http:\/\/encarta.msn.com\/thesaurus_561583416\/pessimist.html\" target=\"_blank\">gloomy Gus<\/A> into our data center. It is with great joy, therefore, that I read your e-mail message because by writing output to an HTML file, we can bring a little color into our otherwise dreadful monotone existence. This is the kind of script that you can make as long and as complicated\u2014or as short and simple\u2014as you wish. As I was writing the script, what started as short and simple became a bit longer and only slightly less simple as the morning wore on.<\/P>\n<P>If interested, you can learn more about <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/pstips\/jan08\/pstip0104.mspx\" target=\"_blank\">saving data as HTML<\/A>. You can also use a VBScript approach to <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/tales\/sg0403.mspx\" target=\"_blank\">create Web pages<\/A>. And this article talks about <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_cpm_dzwk.mspx?mfr=true\" target=\"_blank\">inventorying computer hardware<\/A>.<\/P>\n<P>Here is the <B>SystemConfigToHtml.ps1<\/B>&nbsp;script:<\/P><PRE class=\"codeSample\">$body = $null\n$computer = &#8220;localhost&#8221;\n$System = Get-WmiObject -Class win32_computersystem -ComputerName $computer\n$processor = Get-WmiObject -Class win32_processor -ComputerName $computer\n$video = Get-WmiObject -Class win32_videocontroller -ComputerName $computer\n$disk = [wmi]&#8221;\\\\$computer\\root\\cimv2:win32_logicaldisk.deviceID=&#8217;c:'&#8221;\n$body += &#8220;Processor speed &#8221; + $processor.CurrentClockSpeed + &#8220;&lt;br&gt;&#8221;\n$body += &#8220;Video ram (meg):  &#8221; + $video[0].AdapterRAM\/1mb + &#8220;&lt;br&gt;&#8221;\n$body += &#8220;Free disk space(gig): &#8221; + $disk.FreeSpace\/1gb + &#8220;&lt;br&gt;&#8221;\n$body += &#8220;Make: &#8221; + $system.Manufacturer + &#8220;&lt;br&gt;&#8221;\n$body += &#8220;Model: &#8221; + $system.Model + &#8220;&lt;br&gt;&#8221;\n$body += &#8220;Ram(meg): &#8221; + [int]$system.TotalPhysicalMemory\/1mb\n$head =  &#8220;&lt;style&gt;&#8221;\n$head += &#8220;BODY{background-color:peachpuff}&#8221;\n$head += &#8220;&lt;\/style&gt;&#8221;\n$head += &#8220;&lt;h1&gt;System report for $computer &lt;\/h1&gt;&lt;br&gt;&#8221;\nConvertTo-Html  -body $body -Head $head|\nOut-File -FilePath &#8220;C:\\fso\\$computer.html&#8221;\n<\/PRE>\n<P>The script begins by initializing a couple of variables. The first variable, <B>$body<\/B>, is used to hold the main text we want to display on the Web page. The <B>$computer<\/B> variable is used to hold the name of the computer to query. This is seen&nbsp;here:<\/P><PRE class=\"codeSample\">$body = $null\n$computer = &#8220;localhost&#8221;\n<\/PRE>\n<P>Next, what we want to do is obtain some basic computer system information. This is the same <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_cpm_ciwz.mspx?mfr=true\" target=\"_blank\">WMI class<\/A> you have probably used many times before in VBScript. To query WMI with Windows PowerShell, you use the <B>Get-WmiObject<\/B> cmdlet. In a script, I recommend using all the parameter names, such as <B>\u2013class<\/B> and <B>\u2013computername<\/B>, but they are not really required. This returns all the information from the WMI class and stores it in the <B>$system<\/B> variable. <\/P><PRE class=\"codeSample\">$System = Get-WmiObject -Class win32_computersystem -ComputerName $computer<\/PRE>\n<P>We then want to obtain some information about the processor. We use the <B>win32_processor<\/B> WMI class. We use the same style of command as the previous line and store the results in the <B>$processor<\/B> variable (<A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/hardware\/basic\/hwbavb03.mspx?mfr=true\" target=\"_blank\">this VBScript<\/A> retrieves the same information):<\/P><PRE class=\"codeSample\">$processor = Get-WmiObject -Class win32_processor -ComputerName $computer<\/PRE>\n<P>We also want to gather some information about the video card. To do this, we use the <B>Win32_videoController<\/B> WMI class. We store the video controller information in the <B>$video<\/B> variable (here is <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/rexx\/hardware\/display\/hwdirx04.mspx?mfr=true\" target=\"_blank\">a VBScript<\/A> that retrieves the same information):<\/P><PRE class=\"codeSample\">$video = Get-WmiObject -Class win32_videocontroller -ComputerName $computer<\/PRE>\n<P>We decide to connect specifically to the <B>C:<\/B> drive on the computer. To do this, we use the <B>[WMI]<\/B> type accelerator. This allows us to provide the path to the specific drive we are interested in. This is similar to using the <B>Get<\/B> method from the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_wmi_srzu.mspx?mfr=true\" target=\"_blank\">SwbemServices<\/A> object you may have used in VBScript: <\/P><PRE class=\"codeSample\">$disk = [wmi]&#8221;\\\\$computer\\root\\cimv2:win32_logicaldisk.deviceID=&#8217;c:&#8217;<\/PRE>\n<P>Now we want to build up the information that will be printed out. Here we switch gears and combine Windows PowerShell with HTML. To add information to the <B>$body<\/B> variable, we use the <B>+=<\/B> operator. This is the same thing as saying <B>$body = $body + &#8220;whatever&#8221;<\/B>. The <B>+=<\/B> operator is much easier to type. To include a new line in HTML, we use the <B>&lt;br&gt;<\/B> tab; we place it at the end of each line. For the processor, we were only interested in the <B>CurrentClockSpeed<\/B> property, so we choose that from the <B>$processor<\/B> variable. All the processor information is included in the variable, and we could use any of the information contained in the <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394373.aspx\" target=\"_blank\">Win32_Processor<\/A> class.<\/P>\n<P>The last thing we need to see is the use of the administrative constants: <B>1mb<\/B> and <B>1gb<\/B>. We use these to convert the number from a whole lot of bytes to megabytes and gigabytes as appropriate. Here is the body&nbsp;section:<\/P><PRE class=\"codeSample\">$body += &#8220;Processor speed &#8221; + $processor.CurrentClockSpeed + &#8220;&lt;br&gt;&#8221;\n$body += &#8220;Video ram (meg):  &#8221; + $video[0].AdapterRAM\/1mb + &#8220;&lt;br&gt;&#8221;\n$body += &#8220;Free disk space(gig): &#8221; + $disk.FreeSpace\/1gb + &#8220;&lt;br&gt;&#8221;\n$body += &#8220;Make: &#8221; + $system.Manufacturer + &#8220;&lt;br&gt;&#8221;\n$body += &#8220;Model: &#8221; + $system.Model + &#8220;&lt;br&gt;&#8221;\n$body += &#8220;Ram(meg): &#8221; + $system.TotalPhysicalMemory\/1mb\n<\/PRE>\n<P>This is where we add the style information for the body color, and set the H1 heading for the page. This is basic HTML type of code and is not special for Windows PowerShell at all. Here it&nbsp;is:<\/P><PRE class=\"codeSample\">$head =  &#8220;&lt;style&gt;&#8221;\n$head += &#8220;BODY{background-color:peachpuff}&#8221;\n$head += &#8220;&lt;\/style&gt;&#8221;\n$head += &#8220;&lt;h1&gt;System report for $computer &lt;\/h1&gt;&lt;br&gt;&#8221;\n<\/PRE>\n<P>To create the actual Web page, we use the <B>ConvertTo-Html<\/B> cmdlet. We give it the body information from the <B>$body<\/B> variable and the head information from the <B>$head<\/B> variable, and we then pipeline the text to the <B>out-File<\/B> cmdlet where we specify the path and file. We use the <B>$computer<\/B> variable to create the file name. This is seen&nbsp;here:<\/P><PRE class=\"codeSample\">ConvertTo-Html  -body $body -Head $head |\nOut-File -FilePath &#8220;C:\\fso\\$computer.html&#8221;\n<\/PRE>\n<P>When we run the script, we generate the following HTML; you will note, no doubt, that it looks like HTML code with all the little tags and the like:<\/P><IMG height=\"201\" alt=\"Image of the .html file\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1201\/HSG_PcAudit01.jpg\" width=\"500\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>If we open the file in Internet Explorer (by double-clicking it if Internet Explorer is your default browser), you are greeted with this colorful, cheerful Web page. There is certainly room for improvement, but it does add a bit of color to an otherwise gloomy day:<\/P><IMG height=\"354\" alt=\"Image of the .html file rendered in Internet Explorer\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1201\/HSG_PcAudit02.jpg\" width=\"500\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>JE, we hope you have a colorful day. See you tomorrow.<\/P>\n<P><FONT class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><SPAN class=\"Apple-style-span\"><B><B>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/B><\/B><\/SPAN><\/FONT><\/P><FONT class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><B><\/B><\/FONT><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I need to obtain some basic configuration information from my computers on the network. It would be nice if the script would write the output in an HTML file that we could copy to our internal Web server. This would give us easy access to the information. Whadda ya say?&#8211; JE Hi [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[237,16,3,4,30,45,6],"class_list":["post-54783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-basic-computer-information","tag-desktop-management","tag-scripting-guy","tag-scripting-techniques","tag-web-pages-and-htas","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I need to obtain some basic configuration information from my computers on the network. It would be nice if the script would write the output in an HTML file that we could copy to our internal Web server. This would give us easy access to the information. Whadda ya say?&#8211; JE Hi [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54783","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=54783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54783\/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=54783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=54783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=54783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}