/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" Moving to ThePowerShellGuy.com "," PowerShell Code formatting test for my new Blog "," PowerShell Code formatting test for my new Blog "," PowerShell Community Extensions V1.0 "," PowerShell : Access remote eventlogs "," Windows PowerShell Scripting Sweepstakes! "," PowerShell : making Custum Enums "," TechNet Webcast: An Overview of Windows PowerShell "," "Windows PowerShell: TFM" goes "Gold" "," PowerShell : Advanced renaming of files "," ")

Sunday, December 24, 2006

 


Moving to ThePowerShellGuy.com



as you might have seen in last testpost

I started a new blog here : The PowerShell Guy  ( http://ThePowerShellGuy.com )

the Feeds you can find here :

  • RSS 2.0
  • Atom 1.0
  • Starting with this first entry : PowerShell : Generating RTF Help from XML help files I will no longer post on this blog.

    the content on this blog will not be removed

     

    Enjoy,

    Greetings,/\/\o\/\/
    Tags : PowerShell




     1 comments

    Saturday, December 23, 2006

     


    PowerShell Code formatting test for my new Blog



    I'm testing my new blog The PowerShell guy,

    I post the same testpost here to compare

    Testing codesamples for viewing and copying (do not link here, I will post this item again later after testing , in its final version with a permanent link)

    I'm testing with IE7 and Bloglines, if you have problems pasting in the code on the old or new blog please leave a comment,

    at the moment it looks like the The PowerShell guy

     blog does not have the formatting problems I have here

    convert number to Roman notation

    Function as PowerShell code (Pre tag CodeHTMLer)

    # Convert Int to Roman notation
    # /\/\o\/\/ 2006

    function format-Roman ($num ) {

    $M = [math]::truncate($num / 1000)
    $num -= $M * 1000
    $D = [math]::truncate($num / 500)
    $num -= $D * 500
    $C = [math]::truncate($num / 100)
    $num -= $C * 100
    $L = [math]::truncate($num / 50)
    $num -= $L * 50
    $X = [math]::truncate($num / 10)
    $num -= $x * 10
    $V = [math]::truncate($num / 5)
    $num -= $V * 5

    $Roman = "M" * $M
    $Roman += "D" * $D
    $Roman += "C" * $C
    $Roman += "L" * $L
    $Roman += "X" * $X
    $Roman += "V" * $V
    $Roman += "I" * $num

    $roman = $roman.replace('DCCCC','CM') # 900
    $roman = $roman.replace('CCCC','CD') # 400
    $roman = $roman.replace('LXXXX','XC') # 90
    $roman = $roman.replace('XXXX','XL') # 40
    $roman = $roman.replace('VIIII','IX') # 9
    $roman = $roman.replace('IIII','IV') # 4

    Return $Roman

    }



    Commandline example (Block Quote)


     



    PoSH>format-Roman 1987
    MCMLXXXVII
    PoSH>format-Roman 1999
    MCMXCIX

    PoSH>Update-TypeData C:\PowerShell\TypeData\TypedataInt32.ps1xml
    PoSH>(32).toroman()
    XXXII


     


    format an Integer in Roman notation


     


    Type extension as XML (convert whitespace, codehtmler)


     


    <?xml version="1.0encoding="utf-8?> 
    <Types> 
        <Type> 
            <Name>System.Int32</Name> 
            <Members> 
                <ScriptMethod> 
                    <Name>ToRoman</Name> 
                     <Script> 
                        $num = $this

                        $M = [math]::truncate($num / 1000) 
                        $num -= $M * 1000 
                        $D = [math]::truncate($num / 500) 
                        $num -=  $D * 500 
                        $C = [math]::truncate($num / 100) 
                        $num -=  $C * 100 
                        $L = [math]::truncate($num / 50) 
                        $num -=  $L * 50 
                        $X = [math]::truncate($num / 10) 
                        $num -=  $x * 10 
                        $V = [math]::truncate($num / 5) 
                        $num -=  $V * 5
                        
                        $Roman = "M" * $M
                        $Roman += "D" * $D
                        $Roman += "C" * $C
                        $Roman += "L" * $L
                        $Roman += "X" * $X
                        $Roman += "V" * $V
                        $Roman += "I" * $num
                        
                        $roman = $roman.replace('DCCCC','CM') # 900
                        $roman = $roman.replace('CCCC','CD') # 400
                        $roman = $roman.replace('LXXXX','XC') # 90
                        $roman = $roman.replace('XXXX','XL') # 40
                        $roman = $roman.replace('VIIII','IX') # 9
                        $roman = $roman.replace('IIII','IV') # 4
                        
                        Return $roman
                    </Script> 
                </ScriptMethod> 

            </Members> 
        </Type> 
    </Types>

     


    * Edit * Added script again formatted by PowerShell analyzer


     

    # Function Format-Roman
    #
    /\/\o\/\/ 006
    #
    http://ThePowerShellGuy.com

    # HTML Formatting Generated with PowerShell Analyzer

    function format-Roman ($num ) {

    $M = [math]::truncate($num / 1000)
    $num -= $M * 1000
    $D = [math]::truncate($num / 500)
    $num -= $D * 500
    $C = [math]::truncate($num / 100)
    $num -= $C * 100
    $L = [math]::truncate($num / 50)
    $num -= $L * 50
    $X = [math]::truncate($num / 10)
    $num -= $x * 10
    $V = [math]::truncate($num / 5)
    $num -= $V * 5

    $Roman = "M" * $M
    $Roman += "D" * $D
    $Roman += "C" * $C
    $Roman += "L" * $L
    $Roman += "X" * $X
    $Roman += "V" * $V
    $Roman += "I" * $num

    $roman = $roman.replace('DCCCC','CM') # 900
    $roman = $roman.replace('CCCC','CD') # 400
    $roman = $roman.replace('LXXXX','XC') # 90
    $roman = $roman.replace('XXXX','XL') # 40
    $roman = $roman.replace('VIIII','IX') # 9
    $roman = $roman.replace('IIII','IV') # 4

    Return
    $roman

    }



     


    Enjoy, Greetings /\/\o\/\/




    posted by /\/\o\/\/
     2 comments
     


    PowerShell Code formatting test for my new Blog



    I'm testing my new blog The PowerShell guy,


    I post the same testpost here to compare


    Testing codesamples for viewing and copying (do not link here, I will post this item again later after testing , in its final version with a permanent link)


    I'm testing with IE7 and Bloglines, if you have problems pasting in the code on the old or new blog please leave a comment,


    at the moment it looks like the The PowerShell guy


     blog does not have the formatting problems I have here


    convert number to Roman notation


    Function as PowerShell code (Pre tag CodeHTMLer)

    # Convert Int to Roman notation
    # /\/\o\/\/ 2006

    function format-Roman ($num ) {

    $M = [math]::truncate($num / 1000)
    $num -= $M * 1000
    $D = [math]::truncate($num / 500)
    $num -= $D * 500
    $C = [math]::truncate($num / 100)
    $num -= $C * 100
    $L = [math]::truncate($num / 50)
    $num -= $L * 50
    $X = [math]::truncate($num / 10)
    $num -= $x * 10
    $V = [math]::truncate($num / 5)
    $num -= $V * 5

    $Roman = "M" * $M
    $Roman += "D" * $D
    $Roman += "C" * $C
    $Roman += "L" * $L
    $Roman += "X" * $X
    $Roman += "V" * $V
    $Roman += "I" * $num

    $roman = $roman.replace('DCCCC','CM') # 900
    $roman = $roman.replace('CCCC','CD') # 400
    $roman = $roman.replace('LXXXX','XC') # 90
    $roman = $roman.replace('XXXX','XL') # 40
    $roman = $roman.replace('VIIII','IX') # 9
    $roman = $roman.replace('IIII','IV') # 4

    Return $Roman

    }



    Commandline example (Block Quote)


     



    PoSH>format-Roman 1987
    MCMLXXXVII
    PoSH>format-Roman 1999
    MCMXCIX

    PoSH>Update-TypeData C:\PowerShell\TypeData\TypedataInt32.ps1xml
    PoSH>(32).toroman()
    XXXII


     


    format an Integer in Roman notation


     


    Type extension as XML (convert whitespace, codehtmler)


     


    <?xml version="1.0encoding="utf-8?> 
    <Types> 
        <Type> 
            <Name>System.Int32</Name> 
            <Members> 
                <ScriptMethod> 
                    <Name>ToRoman</Name> 
                     <Script> 
                        $num = $this

                        $M = [math]::truncate($num / 1000) 
                        $num -= $M * 1000 
                        $D = [math]::truncate($num / 500) 
                        $num -=  $D * 500 
                        $C = [math]::truncate($num / 100) 
                        $num -=  $C * 100 
                        $L = [math]::truncate($num / 50) 
                        $num -=  $L * 50 
                        $X = [math]::truncate($num / 10) 
                        $num -=  $x * 10 
                        $V = [math]::truncate($num / 5) 
                        $num -=  $V * 5
                        
                        $Roman = "M" * $M
                        $Roman += "D" * $D
                        $Roman += "C" * $C
                        $Roman += "L" * $L
                        $Roman += "X" * $X
                        $Roman += "V" * $V
                        $Roman += "I" * $num
                        
                        $roman = $roman.replace('DCCCC','CM') # 900
                        $roman = $roman.replace('CCCC','CD') # 400
                        $roman = $roman.replace('LXXXX','XC') # 90
                        $roman = $roman.replace('XXXX','XL') # 40
                        $roman = $roman.replace('VIIII','IX') # 9
                        $roman = $roman.replace('IIII','IV') # 4
                        
                        Return $roman
                    </Script> 
                </ScriptMethod> 

            </Members> 
        </Type> 
    </Types>

     


    * Edit * Added script again formatted by PowerShell analyzer


     

    # Function Format-Roman
    #
    /\/\o\/\/ 006
    #
    http://ThePowerShellGuy.com

    # HTML Formatting Generated with PowerShell Analyzer

    function format-Roman ($num ) {

    $M = [math]::truncate($num / 1000)
    $num -= $M * 1000
    $D = [math]::truncate($num / 500)
    $num -= $D * 500
    $C = [math]::truncate($num / 100)
    $num -= $C * 100
    $L = [math]::truncate($num / 50)
    $num -= $L * 50
    $X = [math]::truncate($num / 10)
    $num -= $x * 10
    $V = [math]::truncate($num / 5)
    $num -= $V * 5

    $Roman = "M" * $M
    $Roman += "D" * $D
    $Roman += "C" * $C
    $Roman += "L" * $L
    $Roman += "X" * $X
    $Roman += "V" * $V
    $Roman += "I" * $num

    $roman = $roman.replace('DCCCC','CM') # 900
    $roman = $roman.replace('CCCC','CD') # 400
    $roman = $roman.replace('LXXXX','XC') # 90
    $roman = $roman.replace('XXXX','XL') # 40
    $roman = $roman.replace('VIIII','IX') # 9
    $roman = $roman.replace('IIII','IV') # 4

    Return
    $roman

    }



     


    Enjoy, Greetings /\/\o\/\/


     




    posted by /\/\o\/\/
     0 comments

    Thursday, December 14, 2006

     


    PowerShell Community Extensions V1.0



     

     

    On CodePlex there is released a major update to PSCX (PowerShell Community Extensions) that I think folks will find very useful. 

    be sure to Check it out !!

    PowerShell Community Extensions is a PowerShell Snapin that provides a
    number of widely useful cmdlets. PSCX is not affliated with Microsoft
    or the Windows PowerShell team at Microsoft. We are a few (at the moment)
    passionate PowerShell users who wanted more cmdlets than Micorsoft was
    able to deliver in v1.0. So we have taken it upon ourselves to create
    some of those cmdlets and make them available to the community.

    You can download it from:
    http://www.codeplex.com/PowerShellCX/Release/ProjectReleases.aspx
    If you run into problems please report them here:
    http://www.codeplex.com/PowerShellCX/WorkItem/List.aspx
    The readme and about_pscx help topic you can find on the download page also, for you to check out what
    is new in version 1.0. 


    Enjoy!
    --
    The PSCX crew (Keith Hill, Alex Angelopoulos, Mabster, DBMsW, and MoW)
    P.S. If you are passionate about PoSh and can contribute by writing C#
    cmdlets, let us know.  We're looking for more help.

     

    Greetings,/\/\o\/\/
    Tags : PowerShell


    posted by /\/\o\/\/
     1 comments

    Tuesday, December 05, 2006

     


    PowerShell : Access remote eventlogs



    Jeffery Hicks did a 2 part series on his blog about accessing evenlogs in PowerShell

     PowerShell Event Log Filtering ,

    Remote Event Log Filtering 

    in the second part he did use WMI to acces remote eventlogs, I posted an example in the comments using .NET for it.

     

    PoSH>$logs = [System.Diagnostics.EventLog]::GetEventLogs('Server')
    PoSH>$logs[0]
    Max(K) Retain OverflowAction Entries Name
    ------ ------ -------------- ------- ----
    20,480 7 OverwriteOlder 829 Application
    PoSH>$logs[0].machinename
    Server
    PoSH>$logs[0].entries | where `
    >>  {($_.EntryType -eq "Warning" -OR $_.EntryType -eq "Error") `
    >> -AND ($_.TimeWritten -ge $recent)}

    As an reaction to Remote Event Log Filtering in my comment  Here  where I did the example in the first part PowerShell Event Log Filtering again but then using the .NET object directly, he posted about powershell users not (yet) knowing the .net objects  :

    but a good thing about this they allready did learn about then using the CMDlet get-eventlog, as this is merely a wrapper for the .NET objects, as the examples below will show, the output and objects will be the same, only in this case you can use it agains remote computers, also you as you can see in the last example you can even "Switch" to a remote computer using only the PowerShell get-EventLog -list command, so you can see that you actualy did allready learn how to use it in the first part of Jeffery Hicks 's series only you might not know it yet ;-).

    Get-EventLog -list is actualy the same as System.Diagnostics.EventLog]::GetEventLogs() :

     

    PoSH>Get-EventLog -list

    Max(K) Retain OverflowAction Entries Name
    ------ ------ -------------- ------- ----
    20,480 7 OverwriteOlder 829 Application
    ...

    PoSH>[System.Diagnostics.EventLog]::GetEventLogs()

    Max(K) Retain OverflowAction Entries Name
    ------ ------ -------------- ------- ----
    20,480 7 OverwriteOlder 829 Application

    ...

     

    As you can find out like this

    [System.Diagnostics.EventLog] | gm -s | fl *

    in the output from that you will find that the latter method takes also a machinename parameter :

    ...

    TypeName : System.Diagnostics.EventLog
    Name : GetEventLogs
    MemberType : Method
    Definition : static System.Diagnostics.EventLog[] GetEventLogs(), static System.Diagnostics.EventLog[] GetEventLogs(Str
    ing machineName)

    ...

     

    Also $al = get-eventlog Application is almost the same as $al = new-object System.Diagnostics.EventLog('Application')

    only here  you can see that here also the result is alsmost the same, only the latter method it not completely the same, as get-eventlog CMDlets allready calls the entries() method

     

    PoSH>$al = get-eventlog Application
    PoSH>$al

    Index Time Type Source EventID Message
    ----- ---- ---- ------ ------- -------

    PoSH>$al = new-object System.Diagnostics.EventLog('Application')

    PoSH>$al

    Max(K) Retain OverflowAction Entries Name
    ------ ------ -------------- ------- ----
    20,480 7 OverwriteOlder 829 Application

    PoSH>$al.Entries

    Index Time Type Source EventID Message
    ----- ---- ---- ------ ------- -------

    # Remote

    PoSH>new-object System.Diagnostics.EventLog('Application','foo')

    Max(K) Retain OverflowAction Entries Name
    ------ ------ -------------- ------- ----

     

    As again this method has more overloads :

     

    PoSH>[System.Diagnostics.EventLog].GetConstructors() |% {"$_"}
    Void .ctor()
    Void .ctor(System.String)
    Void .ctor(System.String, System.String)
    Void .ctor(System.String, System.String, System.String)

    in this case (no Enums all strings ) that is not that helpfull but we have help here, same as in the example in yesterdays post about the VbScript to PowerShell converting guide online, me (and Others) did this before for MSDN

    /\/\o\/\/ PowerShelled: MSH Get-MSDN Function

    PoSH>Function Get-MSDN ($type = "default") {
    >> (new-object -com shell.application).Open("http://msdn2.microsoft.com/library/$type.aspx")
    >> }
    >>
    PoSH>Get-MSDN System.Diagnostics.EventLog

     

    So again after connecting using the .NET object for the rest it is also the same and you can still use the eventlog methods as with the indexing into the getEventlogs() method in the first example $logs[0] this can even be handy

     

    PoSH>$al = new-object System.Diagnostics.EventLog('Application')
    PoSH>$al | Get-Member -membertype Method

    TypeName: System.Diagnostics.EventLog

    Name MemberType Definition
    ---- ---------- ----------

    PoSH>$al.WriteEvent

    MemberType : Method
    OverloadDefinitions : {System.Void WriteEvent(EventInstance instance, Params Object[] values), System.Void WriteEvent(E
    ventInstance instance, Byte[] data, Params Object[] values)}
    TypeNameOfValue : System.Management.Automation.PSMethod
    Value : System.Void WriteEvent(EventInstance instance, Params Object[] values), System.Void WriteEvent(Ev
    entInstance instance, Byte[] data, Params Object[] values)
    Name : WriteEvent
    IsInstance : True

    Even a more nice example for this is the following example we use the get-eventlog -list command to get the eventlogs and then switch to another machine by changeing the machinename property as that is get / set

    So it is not even needed to use the .NET Object to work agains remote computers :

     

    PoSH>$logs = Get-EventLog -list
    PoSH>$logs[0]

    Max(K) Retain OverflowAction Entries Name
    ------ ------ -------------- ------- ----
    20,480 7 OverwriteOlder 829 Application

    PoSH>$logs[0].machinename = "foo"
    PoSH>$logs[0]

    Max(K) Retain OverflowAction Entries Name
    ------ ------ -------------- ------- ----
    Application

    So while you use get-member and use the Cmdlets in the background you also learn to know the .NET object,

    so this is a good glidepath to using the .net classes when you are in need for the extra power they can give you (e.g. remoting, other functionality and /or Speed), I hope this post will also help by showing it is not that different from using the CMDlets as you get back the same .NET objects.

    I think this is a good middle way ;-)

    Enjoy,

    Greetings,/\/\o\/\/
    Tags : PowerShell




    posted by /\/\o\/\/
     0 comments

    Monday, December 04, 2006

     


    Windows PowerShell Scripting Sweepstakes!



    as you can read here PowerShell Scripting Contest - 2 weeks left, here Don't miss the Microsoft PowerShell Contest, here PowerShell Scripting Contest, herePowerShell Scripting Contest and here PowerShell Scripting Contest - 2 weeks left , there is still some time left  te enter in the

     Windows PowerShell Scripting Sweepstakes!

    November 1 – December 15, 2006

    What also can help is  the excelent:

    VbScript to Windows PowerShell translation guide

    for looking up VbScript commands, or inspiration, for example yo could write a function to do a quick lookup in it

    for example you can write a function to do that from the PowerShell console so yo cando :

     

    PoSH>convert-VbScript
    PoSH>convert-VbScript inputbox
    PoSH>convert-VbScript getObject

     

    to start internet explorer to do a quick lookup in that guide to get help on how to translate the VbScript command you remember to PowerShell, that could be as simple as this :

     

     

    # Use the Scripting Guy's VbScript to Windows PowerShell translation guide
    # for help to convert vbScript commands

    Function convert-VbScript ($Command = "default") { 
      (new-object -com shell.application).Open("http://www.microsoft.com/technet/scriptcenter/topics/winpsh/convert/$command.mspx"
    }

     

    Started without the commandname it starts the index otherwise it will bring you directly to the page about the command given

    or one to remind how much time long you still have

    PoSH>function get-TimeLeftToEnterPowerShellContest {(get-date -date "12/15/2006 23:55").Subtract((get-date))}
    PoSH>get-TimeLeftToEnterPowerShellContest

    Days : 11
    Hours : 2
    Minutes : 21
    Seconds : 43
    Milliseconds : 719
    Ticks : 9589037192755
    TotalDays : 11.0984226767998
    TotalHours : 266.362144243194
    TotalMinutes : 15981.7286545917
    TotalSeconds : 958903.7192755
    TotalMilliseconds : 958903719.2755

     

      hope this guide and the examples get you started with an script for the contest,

    Enjoy,

    Greetings,
    /\/\o\/\/
    Tags : PowerShell




    posted by /\/\o\/\/
     0 comments
     


    PowerShell : making Custum Enums



    In PowerShell you often see the use of enum's

    for some info , examples of Enums and there usage see :

    /\/\o\/\/ PowerShelled: Exploring .NET types, classes and Enums ...,

    /\/\o\/\/ PowerShelled: Get SpecialFolder Locations in Monad,

    /\/\o\/\/ PowerShelled: Adding a Simple AccesRule to a file ACL in MSH

     

    as you can see in the Special Forder function this is very handy for some parameters :

    It is not possible yet to create Enums and classes directly in PowerShell, but there is a way by using Reflection :

    # Make a Custom Enum in PowerShell

    $cd = [AppDomain]::CurrentDomain 
     
    $an = new-object System.Reflection.AssemblyName('Mow') 
    $ab = $cd.DefineDynamicAssembly($an,'RunAndSave') 
    $mb = $ab.DefineDynamicModule($an.Name, "$($an.Name).dll"
    $eb = $mb.DefineEnum("Fruit",'Public', [int]) 
     
    & { 
      $eb.DefineLiteral('Apple',1
      $eb.DefineLiteral('strawberry',2
      $eb.DefineLiteral('Banana',3
    } | out-null 
     
    $t = $eb.CreateType() 

     

    It might not look that way, but this is "Fasten your Seatbells (c) Jeffrey Snover" - Code (you can even create complete classes this way but that's outside my "Scope" (@ reading developers : but this would be real cool material for a CMDlet *Wink* *Wink* )),

    Hence, I won't go into the workings to much, to get an impression after runnin the code try :

    $cd,$an,$mb,$eb | gm

    but the code as is, is easy to customize, e.g. to add a Kiwi to the list with number 4 add a line like this

    $eb.DefineLiteral('Kiwi',4)

    As you can see here this is handy for custom functions :

    ...

    PoSH>$t = $eb.CreateType()

    PoSH>[fruit]

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True Fruit System.Enum

    PoSH>[enum]::GetNames([fruit])
    Apple
    strawberry
    Banana

    PoSH>function new-FruitShake ([fruit]$f) {"Here is your $f Shake"}

    PoSH>new-FruitShake Apple
    Here is your Apple Shake

    PoSH>new-FruitShake chocolate
    new-FruitShake : Cannot convert value "chocolate" to type "Fruit" due to invalid enumeration values. Specify one of the
    following enumeration values and try again. The possible enumeration values are "Apple, strawberry, Banana".
    At line:1 char:15
    + new-FruitShake <<<< chocolate

    If you have my custom tabcompletion

    /\/\o\/\/ PowerShelled: PowerShell : Tabcompletion Part 5 or

    /\/\o\/\/ PowerShelled: PowerShell Tab Completion Part 4

    you can even tabcomplete on it :

    [fruit]A[tab]

    [fruit]Apple

     

    Enjoy,

     

    Greetings /\/\o\/\/

    Tags : PowerShell




    posted by /\/\o\/\/
     0 comments

    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?