This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
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-
block.
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)
http://winfx.msdn.microsoft.com/library/default.asp?url=/library/en-us/monad_gettingstarted/html/b364a5bb-453d-4a1b-9906-583bcbef2399.aspor download the Beta doc-set here :
http://www.microsoft.com/downloads/details.aspx?FamilyID=8a3c71d1-18e5-49d7-952a-c55d694ecee3&DisplayLang=enYou 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()
Value
-----
Client\mow
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.
Enjoy
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" ?>
<Types>
<Type>
<Name>System.IO.FileSystemInfo</Name>
<Members>
<ScriptMethod>
<Name>CopyGUI</Name>
<Script>
$name= $args[0]
#$target = (read-host "Enter Target: ")
$(new-object -com shell.application).NameSpace($name).CopyHere($this.fullname)
trap {"Error Copying"; continue }
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>Get_Group</Name>
<Script>
$this.GetAccessControl().getgroup([System.Security.Principal.ntaccount])
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>Get_GroupSID</Name>
<Script>
$this.GetAccessControl().getgroup([System.Security.Principal.securityidentifier])
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>Get_Owner</Name>
<Script>
$this.GetAccessControl().getOwner([System.Security.Principal.ntaccount])
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>Get_OwnerSID</Name>
<Script>
$this.GetAccessControl().getOwner([System.Security.Principal.securityidentifier])
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>Set_Owner</Name>
<Script>
$SP = $args[0]
$a = $this.GetAccessControl()
$a.SetOwner($SP)
$this.SetAccessControl($a)
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>GetWmi</Name>
<Script>get-WmiObject CIM_DATAFILE -filter "Name = `"$($this.fullname.replace(`"\`",`"\\`"))`""</Script>
</ScriptMethod>
</Members>
</Type>
</Types>