/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" Unix => MSH Translations: find and grep "," MSH Object Viewer "," Windows "Monad" Shell Beta 2 Documentation Pack "," sample of mine used at reskit.net "," Another Channel9 Interview With Jeffrey Snover. "," /\/\o\/\/ first entry "," ")

Thursday, October 20, 2005

 


MSH registry access



I needed to Change Default view in explorer,for Ca 1500 XP clients and reset the views.
(changing this when Grouppolicy is applied a.o. "Classic Shell" (that messed up the settings in the first place - reason for the request) etc. is another topic - and not funny ! - if you need too.. good luck ... search the unattended newsgroups for the registry-settings.. and check the GPO settings - the code below works at my home box but still not on my client in the customers domain - so we stopped by removing the "Classic shell"-GP setting, but ok, other story)

and I could not use MSH for this anyway (Yet),
but as I'm already getting used to prototyping in MSH. (I think the Guys who already played a bit with it know why ;-)),
hence I started there :

but the proto-typing in MSH (changing the reg was a nice adventure)

First getting there :

Change to the CurrentUser key :

MSH C:\> cd HKCU:

nice and easy ;-) so now to the softwarekey

cd so[tab]

MSH HKCU:\> cd Software

cd mi/[tab]\wi[tab]\cu[tab]\ex[tab]\Str[tab][tab]

MSH HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Streams>

Tab completion in the Regprovider so it realy browses like a dir (I realy like this)

So do a Dir
MSH HKCU:\...>LS
Hive: Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Streams
SKC VC Name Property
--- -- ---- --------
0 2Defaults {{20D04FE0-3AEA-1069-A2D8-08002B30309...
0 2 Desktop {TaskbarWinXP, Toolbars}

Hmm, should there not be a RegistryValue settings here ?
so I tryed :

MSH HKCU:\...> gi .
Hive: Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer
SKC VC Name Property--- -- ---- -------- 3 1 Streams {Settings}

I see the setting, but not realy as expected (thinking Commandline),try this 1 level higher in Explorer to realy see what I mean

so think Object .....

MSH HKCU:\> gp .
MshPath : Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\streamsMshParentPath : Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ExplorerMshChildName : streamsMshDrive : HKCU
MshProvider : System.Management.Automation.ProviderInfo
Settings : {8, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 119, 126, 19, 115, 53, 207, 17, 174, 105, 8, 0, 43, 46, 18, 98, 4, 0, 0, 0, 4, 0, 0, 0 , 67, 0, 0, 0}

GP = get-Property, we are getting the properties of the current Key.
looks a bit strange maybe, because we are (still) thinking filesystem when in the reg-provider when navigating but if you come to the place you want to be and going to the actions it comes clear then the fun starts :

I first make a location variable :

$location = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Streams'
and get the settings.
$Settings = (get-property $location).settings

to only view the property at the commandline I could do this

MSH HKCU:\..> (gp .).settings

, but since I copy the line to a script that I want to be able to run form anywhere I do it this way (and I can do "cd C:" to get a short prompt - you can change the prompt but thats also dangerous IMHO because you will not see wat you do)

MSH > $settings gm

TypeName: System.Byte

Name MemberType Definition
---- ---------- ----------
CompareTo Method System.Int32 CompareTo(Object value), System.Int32 Co...
Equals Method System.Boolean Equals(Object obj), System.Boolean Equ...
GetHashCode Method System.Int32 GetHashCode()
GetType Method System.Type GetType()
GetTypeCode Method System.TypeCode GetTypeCode()
ToString Method System.String ToString(), System.String ToString(Stri...

so we can see that MSH neatly provided us with a byte-array !!

so we can just do this :

$Settings[4] = 4

... set the 5th (bytearry starts with 0) and set it to 4 (details)
and write it back.

set-property $Location -property settings -value $settings

Cool .. thats al it took to set only 1 byte of an Byte-array in the registry.

almost done, just do the same trick on the following key :
$location = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Streams\Defaults'

Oops, it has {} around it (MSH script-block)

$Settings = get-property $Location -property "{20D04FE0-3AEA-1069-A2D8-08002B30309D}"

so far so good ..

MSH C:\> $settings[4] = 5

Unable to index into an object of type System.Management.Automation.MshObject.At line:1 char:11+ $settings[4 <<<< ] = 5

the problem is MSH will wrap this in a custom object
TypeName: System.Management.Automation.MshCustomObject

the trick is : you have to specify the name again :
$Settings."{20D04FE0-3AEA-1069-A2D8-08002B30309D}"[4] = $args[0]

watch out ALSO when writing it back :

so like this :

set-property $Location -property "{20D04FE0-3AEA-1069-A2D8-08002B30309D}" -value $settings."{20D04FE0-3AEA-1069-A2D8-08002B30309D}"

NOT LIKE THIS !!! (it will write a string to your registry !!)

set-property $Location -property "{20D04FE0-3AEA-1069-A2D8-08002B30309D}" -value $settings

so to be save first save it in another variable !

so the total function will be :

Function SetDefView{
# set byte array
$location = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Streams' $Settings = (get-property $location).settings
$Settings[4] = $args[0]
set-property $Location -property settings -value $settings

# set byte array in special key (that needs quotes like 123 or {6})
$location = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Streams\Defaults' $SettingsWrapper= get-property $Location -property "{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
$settings = $SettingsWrapper."{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
$settings[4] = $args[0] set-property $Location -property "{20D04FE0-3AEA-1069-A2D8-08002B30309D}" -value $settings
"Done ! (you have to logon / logoff to see the change)"
}

you see that the last part is a bit more complex since you have to deal with the wrapper, and easy to do wrong (use the 2 step variable declaration to avoid confusion). this can also happen with "simpler" keys like 123.

I hope this post will help you exploring the Reg with MSH

enjoy ..

gr /\/\o\/\/

PS. thx to Jeff Jones [MSFT] for helping me with this in mp.w.s.scripting

PS remember if testing that it will not always work in a GPO env.


Comments: Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?