{"id":51183,"date":"2010-02-25T00:01:00","date_gmt":"2010-02-25T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/02\/25\/hey-scripting-guy-can-i-use-transactions-to-write-wmi-information-to-the-registry\/"},"modified":"2010-02-25T00:01:00","modified_gmt":"2010-02-25T00:01:00","slug":"hey-scripting-guy-can-i-use-transactions-to-write-wmi-information-to-the-registry","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-use-transactions-to-write-wmi-information-to-the-registry\/","title":{"rendered":"Hey, Scripting Guy! Can I Use Transactions to Write WMI Information to the Registry?"},"content":{"rendered":"<p class=\"MsoNormal\"><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>&nbsp;<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I need to be able to create several registry keys, but only if the computer meets certain hardware requirements. I was thinking I could query a bunch of WMI classes and then use a transaction, but I am not sure which hardware WMI classes I should use. Do you have any ideas?<\/p>\n<p class=\"MsoNormal\">&#8212; NB<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello NB, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. I have spent quite a bit of time today tweeting with a member of the Sydney Windows PowerShell users group. The other day, Don Jones was going to be speaking to them while visiting Australia on vacation. The entire experience brought back some excellent memories. Also, Brett, the Microsoft Technical Account Manager (TAM) in Sydney who is my ANZAC biscuit connection, is coming to Seattle for the Microsoft TechReady session. I unfortunately will not be able to attend; I am still unable to fly because of recent ear surgery. Therefore, I spent my spare time finding a co-worker in Charlotte who can pick up my ANZAC biscuits. Therefore, it was a busy day, and mostly centered on Australia. That got me looking through some pictures I took during my last trip down under. Here is one I particularly like of the opera house.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Photo of Sydney Opera House\" alt=\"Photo of Sydney Opera House\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/february\/hey0225\/hsg-02-25-10-01.jpg\" width=\"600\" height=\"442\"><\/p>\n<p class=\"Fig-Graphic\">&nbsp;<\/p>\n<p class=\"MsoNormal\">When I was not trying to support my ANZAC biscuit addiction, I did get some time to check the <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> inbox. NB, if it were me and I did not need specific hardware performance specifications, I would use the WinSAT scores. One thing to keep in mind about WinSAT scores is they are generally created when Windows 7 or Windows Vista are installed (these are the only two platforms that have WinSat). New drivers, such as video drivers, can either enhance or degrade performance, and therefore you should run the WinSAT utility again. The <b>Win32_WinSAT<\/b> WMI class can be used to retrieve the most recent WinSAT scores for a particular platform. The <b>Win32_WinSAT<\/b> WMI class is <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa969204(VS.85).aspx\">documented on MSDN<\/a>. Unfortunately, the <b>Win32_WinSAT<\/b> WMI class does not have an update method&mdash;in fact it has no methods at all. There is a command line WinSAT utility, that can be used to update the WinSAT scores&mdash;it must be run with administrative permissions. Winsat.exe is documented in the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/cc770542(WS.10).aspx\">command-line reference<\/a> on TechNet. <\/p>\n<p class=\"MsoNormal\">The WriteWinSatToRegistryUseTransaction.ps1 script is similar to the script used in <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/02\/24\/hey-scripting-guy-february-24-2010.aspx\"><span>yesterday&rsquo;s Hey, Scripting Guy! Blog post<\/span><\/a>. The big difference is that in this script, we need to create a new registry key, but we are unable to do so with the <b>Set-Item<\/b> cmdlet. If the HKCU:SoftwareScriptingGuysHardware<b> <\/b>registry key exists, an error will be generated. It is best to use the <b>Test-Path<\/b> cmdlet to check to see if the registry key exists. If it does not exist, create it. The syntax is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">if(-not (test-path -Path HKCU:SoftwareScriptingGuysHardware))<br \/>{<br \/><span>&nbsp;<\/span>New-Item -Path HKCU:SoftwareScriptingGuysHardware<br \/>}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">After creating the registry key, if it needs to be created, the remainder of the script is similar to yesterdays. I use several Set-ItemProperty cmdlets to store the WinSat information in the registry. This is seen here. <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name CPUScore -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp;&nbsp; <\/span>-Value ($a.CPUScore).ToString() -useTransaction | out-null<br \/><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name D3dScore -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp;&nbsp; <\/span>-Value ($a.D3DScore).ToString() -useTransaction | out-null<span>&nbsp;&nbsp; <\/span><br \/><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name DiskScore -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp;&nbsp; <\/span>-Value ($a.DiskScore).ToString() -useTransaction | out-null<br \/><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name GraphicsScore -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp;&nbsp; <\/span>-Value ($a.GraphicsScore).ToString() -useTransaction | out-null<br \/><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name MemoryScore -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp;&nbsp; <\/span>-Value ($a.MemoryScore).ToString() -useTransaction | out-null<br \/><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name TimeTaken -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp;&nbsp; <\/span>-Value ($a.TimeTaken).ToString() -useTransaction | out-null<br \/><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name WinSPRLevel -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp;&nbsp; <\/span>-Value ($a.WinSPRLevel).ToString() -useTransaction | out-null<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When I was writing the script, I copied the first line and pasted it six times. I then went down the code, one line at a time, changing the registry key names and the WMI property names. I had a Windows PowerShell console open with the results of the following command on the screen so that I could easily make my changes.<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; gwmi win32_winsat<\/p>\n<p>__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<br \/>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Win32_WinSAT<br \/>__SUPERCLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br \/>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Win32_WinSAT<br \/>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Win32_WinSAT.TimeTaken=&#8221;MostRecentAssessment&#8221;<br \/>__PROPERTY_COUNT<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 8<br \/>__DERIVATION<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {}<br \/>__SERVER<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: MRED1<br \/>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: rootcimv2<br \/>__PATH<span>&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>: \\MRED1rootcimv2:Win32_WinSAT.TimeTaken=&#8221;MostRecentAssessment&#8221;<br \/>CPUScore<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 7.2<br \/>D3DScore<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 4<br \/>DiskScore<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 7.1<br \/>GraphicsScore<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 4<br \/>MemoryScore<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 7.2<br \/>TimeTaken<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: MostRecentAssessment<br \/>WinSATAssessmentState : 1<br \/>WinSPRLevel<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 4<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">We now need to complete the transaction, and display a status message:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;&nbsp; <\/span>Complete-Transaction<br \/><span>&nbsp;&nbsp; <\/span>&#8220;registry Key updated&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The complete WriteWinSatToRegistryUseTransaction.ps1 script is seen here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>WriteWinSatToRegistryUseTransaction.ps1<\/p>\n<p><\/strong><\/p>\n<p class=\"CodeBlockScreened\"><span><font><font face=\"Lucida Sans Typewriter\">#requires -version 2.0<br \/>Function New-HardwareKey ($computer)<br \/>{<br \/><span>&nbsp;<\/span>$t = {Start-Transaction}<br \/><span>&nbsp;<\/span>&amp;($t)<br \/><span>&nbsp;<\/span>Try<br \/><span>&nbsp; <\/span>{<br \/><span>&nbsp;&nbsp; <\/span>$a = Get-WmiObject -Class win32_WinSat -ComputerName $computer -ErrorAction silentlyContinue<br \/><span>&nbsp;&nbsp; <\/span>if(-not (test-path -Path HKCU:SoftwareScriptingGuysHardware))<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>{<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>New-Item -Path HKCU:SoftwareScriptingGuysHardware -UseTransaction `<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>-ErrorAction stop | out-null<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>}<br \/><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name CPUScore -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp;&nbsp; <\/span>-Value ($a.CPUScore).ToString() -useTransaction | out-null<br \/><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name D3dScore -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp;&nbsp; <\/span>-Value ($a.D3DScore).ToString() -useTransaction | out-null<span>&nbsp;&nbsp; <\/span><br \/><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name DiskScore -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp;&nbsp; <\/span>-Value ($a.DiskScore).ToString() -useTransaction | out-null<br \/><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name GraphicsScore -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp;&nbsp; <\/span>-Value ($a.GraphicsScore).ToString() -useTransaction | out-null<br \/><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name MemoryScore -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp;&nbsp; <\/span>-Value ($a.MemoryScore).ToString() -useTransaction | out-null<br \/><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name TimeTaken -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp; <\/span><span>&nbsp;<\/span>-Value ($a.TimeTaken).ToString() -useTransaction | out-null<br \/><span>&nbsp;&nbsp; <\/span>Set-ItemProperty -Name WinSPRLevel -Path HKCU:SoftwareScriptingGuysHardware `<br \/><span>&nbsp;&nbsp; <\/span>-Value ($a.WinSPRLevel).ToString() -useTransaction | out-null<br \/><span>&nbsp;&nbsp; <\/span>Complete-Transaction<br \/><span>&nbsp;&nbsp; <\/span>&#8220;registry Key updated&#8221;<br \/><span>&nbsp; <\/span>}<br \/><span>&nbsp;<\/span>Catch [System.Exception]<br \/><span>&nbsp; <\/span>{ <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Undo-Transaction <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>&#8220;roll back transaction&#8221;<br \/><span>&nbsp; <\/span>}<br \/><span>&nbsp;<\/span>Finally { &#8220;done&#8221; }<br \/>} #end function New-HardwareKey<\/p>\n<p>New-HardwareKey localHost<\/p>\n<p><\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When the WriteWinSatToRegistryUseTransaction.ps1 script is run, the registry is modified. This is seen in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of registry modified\" alt=\"Image of registry modified\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/february\/hey0225\/hsg-02-25-10-02.jpg\" width=\"600\" height=\"412\"><\/p>\n<p class=\"Fig-Graphic\">&nbsp;<\/p>\n<p class=\"MsoNormal\">NB, that is all there is to using transactions to write WMI information to the registry. This concludes our look at using transactions with the registry provider. Tomorrow it is once again time for Quick-Hits Friday. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.<br \/>&nbsp;\n<b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b>\n<b><span><\/span><\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I need to be able to create several registry keys, but only if the computer meets certain hardware requirements. I was thinking I could query a bunch of WMI classes and then use a transaction, but I am not sure which hardware WMI classes I should use. Do you have [&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":[51,3,4,45],"class_list":["post-51183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I need to be able to create several registry keys, but only if the computer meets certain hardware requirements. I was thinking I could query a bunch of WMI classes and then use a transaction, but I am not sure which hardware WMI classes I should use. Do you have [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51183","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=51183"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51183\/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=51183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}