{"id":1392,"date":"2009-05-07T05:30:38","date_gmt":"2009-05-07T13:30:38","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2009\/05\/07\/sometimes-an-apple-is-an-orange\/"},"modified":"2009-05-07T05:30:38","modified_gmt":"2009-05-07T13:30:38","slug":"sometimes-an-apple-is-an-orange","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2009\/05\/07\/sometimes-an-apple-is-an-orange\/","title":{"rendered":"Sometimes an Apple is an Orange"},"content":{"rendered":"<p>I was helping a member out in the PowerShell forum at <a href=\"http:\/\/www.scriptinganswers.com\" target=\"_blank\">ScriptingAnswers.com<\/a>. He was trying to create a multistring registry entry on a remote computer using the .NET registry classes. Creating registry values is really not that complicated. Here&#8217;s a short example:<\/p>\n<div style=\"border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px\">\n<div style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">$computer=<span style=\"color: #006080\">\"CHAOS\"<\/span><\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">$regpath=<span style=\"color: #006080\">\"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\"<\/span><\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(<span style=\"color: #006080\">'LocalMachine'<\/span>, $Computer)<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">$regKey= $reg.OpenSubKey($regpath,$True)<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">&nbsp;<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"><span style=\"color: #008000\">#create a new string value<\/span><\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">$regKey.SetValue(<span style=\"color: #006080\">\"MyTest\"<\/span>,<span style=\"color: #006080\">\"Sample Value\"<\/span>,<span style=\"color: #006080\">\"STRING\"<\/span>)<\/pre>\n<\/div>\n<\/div>\n<p>You have to open the registry key for writing using $TRUE as the second parameter. The <strong>SetValue()<\/strong> method has several overloads. I&#8217;m using one where I specify the new registry value, its data and the value kind; in this case a string. I can also create DWORD,MultiString or Expandable string values as well.<\/p>\n<p>Multistring implies an array, so first I thought all that was necessary would be to specify an array. But no.<\/p>\n<p><font face=\"Consolas\"><font color=\"#0000ff\">PS C:\\test&gt; $regKey.SetValue(&#8220;MyMulti&#8221;,(&#8220;a&#8221;,&#8221;b&#8221;,&#8221;c&#8221;),&#8221;MULTISTRING&#8221;)<br \/><\/font><font color=\"#ff0000\">Exception calling &#8220;SetValue&#8221; with &#8220;3&#8221; argument(s): &#8220;The type of the value object did not match the specified RegistryValueKind or the object could not be properly converted.&#8221;<br \/>At line:1 char:17<br \/>+ $regKey.SetValue &lt;&lt;&lt;&lt; (&#8220;MyMulti&#8221;,(&#8220;a&#8221;,&#8221;b&#8221;,&#8221;c&#8221;),&#8221;MULTISTRING&#8221;)<br \/>&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotSpecified: (:) [], MethodInvocationException<br \/>&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : DotNetMethodException<\/font><\/font> <\/p>\n<p>I tried creating an array first.<\/p>\n<p><font color=\"#0000ff\" face=\"Consolas\">PS C:\\test&gt; $arr=@(&#8220;a&#8221;,&#8221;b&#8221;,&#8221;c&#8221;)<br \/>PS C:\\test&gt; $regKey.SetValue(&#8220;MyMulti&#8221;,$a,&#8221;MULTISTRING&#8221;)<\/font> <\/p>\n<p>The error was the same. Then I had a thought to compare value from an existing multistring setting so I created one using REGEDIT and retrieved it in PowerShell.<\/p>\n<p><font color=\"#0000ff\" face=\"Consolas\">PS C:\\test&gt; $sa=$regKey.GetValue(&#8220;sa&#8221;)<br \/>PS C:\\test&gt; $sa<br \/>lucky<br \/>daisy<br \/>ginger<\/font> <\/p>\n<p>It looked like an array.&nbsp; Tested as an array. But so did mine.<\/p>\n<p><font color=\"#0000ff\" face=\"Consolas\">PS C:\\test&gt; $sa -is [array]<br \/>True<br \/>PS C:\\test&gt; $arr -is [array]<br \/>True<\/font> <\/p>\n<p>I tried creating a new multistring registry value with $sa. To my surprise it worked!<\/p>\n<p><font color=\"#0000ff\" face=\"Consolas\">PS C:\\test&gt; $regKey.SetValue(&#8220;MyMulti&#8221;,$sa,&#8221;MULTISTRING&#8221;)<br \/>PS C:\\test&gt; $regkey.GetValue(&#8220;MyMulti&#8221;)<br \/>lucky<br \/>daisy<br \/>ginger<\/font> <\/p>\n<p>Now I was really puzzled. I had two variables that both appeared to be arrays of strings, yet one failed . Then I tried this:<\/p>\n<p><font color=\"#0000ff\" face=\"Consolas\">PS C:\\test&gt; $sa.getType() <\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Consolas\">IsPublic IsSerial Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BaseType<br \/>&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8211;<br \/>True&nbsp;&nbsp;&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp;&nbsp; String[]&nbsp;&nbsp; System.Array <\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Consolas\">PS C:\\test&gt; $arr.GetType() <\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Consolas\">IsPublic IsSerial Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BaseType<br \/>&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8211;<br \/>True&nbsp;&nbsp;&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp;&nbsp; Object[]&nbsp;&nbsp; System.Array<\/font><\/p>\n<p>Did you catch the difference? The underlying system types were different. Now I knew what to do. <\/p>\n<p><font color=\"#0000ff\" face=\"Consolas\">PS C:\\test&gt; [string[]]$arr=@(&#8220;a&#8221;,&#8221;b&#8221;,&#8221;c&#8221;)<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Consolas\">PS C:\\test&gt; $regKey.SetValue(&#8220;MyMulti&#8221;,$arr,&#8221;MULTISTRING&#8221;)<br \/>PS C:\\test&gt; $regKey.GetValue(&#8220;MyMulti&#8221;)<br \/>a<br \/>b<br \/>c<\/font> <\/p>\n<p>I specifically cast $arr to be essentially a string of nested string. The registry was expecting simple strings, not string objects. <\/p>\n<p>I was distracted by PowerShell&#8217;s display of my two variables. I thought I was comparing apples to apples, but one of them was actually an orange. If I was more of a developer, I might have figured this out sooner but it was a valuable lesson to not take things at face value.<\/p>\n<p>If you have PowerShell questions or challenges, I hope you&#8217;ll join me in the forums at ScriptingAnswers.com.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was helping a member out in the PowerShell forum at ScriptingAnswers.com. He was trying to create a multistring registry entry on a remote computer using the .NET registry classes. Creating registry values is really not that complicated. Here&#8217;s a short example.<\/p>\n","protected":false},"author":3,"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":[25],"tags":[82,28,31,57],"class_list":["post-1392","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-objects","tag-powershell","tag-registry","tag-scripting"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=1392"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1392\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=1392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=1392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=1392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}