/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" MSH directory watcher with popup-balloon "," StartRDP script "," convert images in MSH "," Style Problems "," ColorCoding MSH scripts on blog "," GUI folder copy tool "," MSHObjectViewer mentioned in More talking about Mo... "," MSH registry access "," Unix => MSH Translations: find and grep "," MSH Object Viewer "," ")

Saturday, October 22, 2005


Update-TypeData (Democracy to the types)

If you have a function that always acts op the same type,
you can add that function to the Type, you can do this by making a MSHXML-file, and loading that file.

If you have seen the channel 9 video "More Talking on Monad",
this is the "Democracy to the types" Jeffrey Snover was talking about.

Making the MSHXML file :

Copy the TapeData below in a file with the extension .MSHXML

to make a new scriptmethod just copy or make a scriptmethod-block give
it a name in the name-block and place your script in the script-

in a ScriptMethod you can access the object with the $this variable.

" for more info look for the "An Extensible Type System" paragraph on this page :
(You can also do the same for the formatting see An Extensible Mechanism for Formatting below that)


or download the Beta doc-set here :

You can update the created file by using update-typedata.

Update-TypeData Mow.Types.IO.MshXml

The TypeDatafile below is an extension to the System.IO.FileSystemInfo Class
the class we work with in MSH when we are working with files.

you can see that by doing the following

MSH> echo "test" > test.txt
MSH> (gi test.txt) gm

TypeName: System.IO.FileInfo
Name MemberType Definition
---- ---------- ----------
AppendText Method System.IO.StreamWriter AppendText()
CopyTo Method System.IO.FileInfo CopyTo(String de

If you load the typdatset below you get the following extra members

CopyGUI ScriptMethod System.Object CopyGUI();
Get_Group ScriptMethod System.Object Get_Group();
Get_GroupSID ScriptMethod System.Object Get_GroupSID();
Get_Owner ScriptMethod System.Object Get_Owner();
Get_OwnerSID ScriptMethod System.Object Get_OwnerSID();
GetWmi ScriptMethod System.Object GetWmi();
Set_Owner ScriptMethod System.Object Set_Owner();

Now you can do things like this :

Copy files / dirs with a GUI (like if copying form explorer.

MSH> (gi test.txt).copygui("c:\test")

will give you a message if you would want to replace the file ;-)

MSH C:\> (gi test.txt).get_Owner()

same for the get_OwnerSid get_Group get_groupSid.

they are reachable from the fileSystemObject, but a bit hidden and you need a .NET (2.0) class [System.Security.Principal.ntaccount]to get to them. (took me to the winFX SDK to find it, its new in .NET 2.0.

* tip as usual in MSH you get an Object back so try a get-member on the returned SID !

the Set_owner : will take a security principal and set the owner with it
(I will look where you can get them later, for now I just coppied the owner from 1 file to Another.

the last one Gets the WMI_info from the file. (this I got from a newsgroup post from Jeffrey Snover )
this gives you alot more info then the FileSystemInfo class alone.

MSH C:\> (gi test.txt).getWmi()
Compressed : False
Encrypted : False
Size :
Hidden : False
Name : c:\test.txt
Readable : True
System File :
Version :
Writeable : True

MSH C:\> (gi test.txt).getWmi() gm

MSH>dir ft name,{$_.getWmi().Writeable},{$_.GetWmi().Encrypted} -auto

etc. etc.


gr /\/\o\/\/

PS the CopyGUI could use some work in the error-handling,
but they are examples (you can always extend them), I'm just exploring MSH and publish my test scripts.

<?xml version="1.0" encoding="utf-8" ?>
$name= $args[0]
#$target = (read-host "Enter Target: ")
$(new-object -com shell.application).NameSpace($name).CopyHere($this.fullname)
trap {"Error Copying"; continue }
$SP = $args[0]
$a = $this.GetAccessControl()
<Script>get-WmiObject CIM_DATAFILE -filter "Name = `"$($this.fullname.replace(`"\`",`"\\`"))`""</Script>

Comments: Post a Comment

Links to this post:

Create a Link

<< Home


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.
Web mow001.blogspot.com

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