David's profileDavid Moisan's ITPhotosBlogLists Tools Help
    November 29

    Learn PowerShell by Rewriting VBScript

    If you're a busy admin and you are new to PowerShell, you might wonder just where to start.  Yes, you've downloaded PowerShell and perhaps played with it, trying a few lines of script.

    But how can you learn all that from scratch?

    If you know VBScript, why not convert a few short scripts to PowerShell?  Here's one example, stolen from SAPIEN Technology's blog:  Play WAVs with SAPI.

    This script uses the Microsoft Speech API in an inventive way to play WAV files in the Windows\Media directory.  It first speaks the name of the file, then plays it. 

    Here's my version:

     

    # speak-wav.ps1
    function DemoWav {
    param ($File)
    trap {return}
        $objFile.Open($file.FullName)
        $objSPVoice.Speak($file.Name)
        $objSPVoice.SpeakStream($objFile)
        $objFile.Close()
    return;
    }
    
    $objSPVoice = new-object -com "SAPI.SpVoice"
    $objFile = new-object -com "SAPI.SpFileStream"
    
    $strMediaDir = join-path $Env:Windir -childpath "Media"
    get-childitem $strMediaDir -filter "*.wav" | foreach {
        write-output $_.name
        demowav $_
        }

    Let's deconstruct this:  PowerShell, unlike VBScript, requires you to declare your functions up-front before they're called by the main code.  Function Demowav flows and performs the same way as in the VBScript version, so we'll get back to that later.

    Continuing on to the start of the main code,  almost all VB scripts except the very simplest access COM objects like so:

    Set objSPVoice = CreateObject("SAPI.SPVoice")

    In PowerShell this is:

     

    $objSPVoice = new-object -com "SAPI.SpVoice"

    This is one of the most common conversions you'll see.  Remember that references to objects within VBScript are almost always COM or ActiveX so you need to specify the -com option in new-object or else PoweShell will think it's a .Net object and error out.

    Another place where the idioms of VBScript and PowerShell are very different is in file and directory processing.  VBScript uses a file system object at a very low level, so your script code has to do all the low-level work of traversing directories and resolving file paths, as in this code:

     

    strWinDir= objShell.ExpandEnvironmentStrings("%WinDir%")
    strMediaDir=strWinDir & "\Media"
    
    Set objFSO=CreateObject("Scripting.FileSystemObject")
    Set objFiles=objFSO.GetFolder(strMediaDir).Files
    
    For Each file In objFiles
    'only get .wav files
        If Right(file.name,3)="wav" Then 
        WScript.Echo file
        DemoWav file
        End If
    Next

    I am sad to recall that in the early days of Windows NT scripting, I was having to do stuff like this in Perl. Parsing a string to get the extension?  Ewww?  Going to yet another object to get the environmental variables? 

    To get the %Windir% environment variable we simply do this in PS:

    $Env:Windir

    Here's the main body of our code:

    $strMediaDir = join-path $Env:Windir -childpath "Media"
    get-childitem $strMediaDir -filter "*.wav" | foreach {
        write-output $_.name
        demowav $_
        }

    Join-path simply takes two filenames and joins them in a path, so that $Env:Windir and "Media" become (when resolved) "C:\windows\media\".

    get-childitem is the most important command:  This cmdlet, when applied to a filesystem, will return a list of objects corresponding to files in that directory.  The -filter parameter tells the cmdlet to filter the output as specified, so in this case, we are only returning *.WAV files.

    Learn this cmdlet;  you'll use it everywhere. 

    The pipe "|" character is where the fun starts.  I mentioned that get-childitem returns a list of objects.  These objects get piped to the next statement:

    | foreach {
        write-output $_.name
        demowav $_
        }

    That's our loop. It's very similar to the VBScript.  Note the $_ variable.  You'll see this everywhere.  That variable just means "current object in the pipeline".  So for each WAV file, the script writes the name of the file to the console with write-host and calls our function demowav.  Note that there are no parentheses in function calls in PowerShell, unlike VBScript and everywhere else.

    Now we're back to the function at the top of the file:

     

    function DemoWav {
    param ($File)
    trap {return}
        $objFile.Open($file.FullName)
        $objSPVoice.Speak($file.Name)
        $objSPVoice.SpeakStream($objFile)
        $objFile.Close()
    return;

    This is straightfoward and almost identical to the VBScript code.  param just declares valid function and program parameters (remember, no parens for function declarations in PS!)  trap {return} just tells this function to return from the function in case of errors.  The only point of difference is that $file is an object, rather than just a string representing a file name, so we need to specify $file.FullName--which is the file's complete path--when we open the file.  ($File.Name provides just the name without the full path.)

    This was a long post, but if you got through it, and have basic knowledge of PowerShell, you'll be able to convert short scripts nearly in your head.  It took me two hours to write this post but only 10 (!) minutes to co

    nvert the original script. 

    Microsoft's Script Center has a guide on converting VBScript to PowerShell that you'll find useful.

    Have Fun!

    Dave

    Comments (2)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    tyzqq lwrote:

    http://www.akkuliste.com/ibm/thinkpad-x60.htm ibm thinkpad x60 akku
    http://www.akkuliste.com/ibm/thinkpad-x60s.htm ibm thinkpad x60s akku
    http://www.akkuliste.com/hp/zt3000.htm hp zt3000 akku
    http://www.akkuliste.com/hp/pavilion-zv5000.htm hp pavilion zv5000 akku
    http://www.akkuliste.com/hp/pavilion-zv6000.htm hp pavilion zv6000 akku
    http://www.akkuliste.com/hp/pavilion-zd8000.htm hp pavilion zd8000 akku
    http://www.akkuliste.com/hp/pavilion-zx5000.htm hp pavilion zx5000 akku
    http://www.akkuliste.com/hp/presario-v4000.htm hp presario v4000 akku
    http://www.akkuliste.com/hp/presario-v2000.htm hp presario v2000 akku
    http://www.akkuliste.com/hp/presario-m2000.htm hp presario m2000 akku
    http://www.akkuliste.com/compaq/presario-v4000.htm compaq presario v4000 akku
    http://www.akkuliste.com/compaq/presario-v2000.htm compaq presario v2000 akku
    http://www.akkuliste.com/compaq/presario-v3000.htm compaq presario v3000 akku
    http://www.akkuliste.com/compaq/presario-v6000.htm compaq presario v6000 akku
    http://www.akkuliste.com/compaq/presario-c700.htm compaq presario c700 akku
    http://www.akkuliste.com/dell/fk890.htm dell fk890 akku
    http://www.akkuliste.com/toshiba/pa3593u-1bas.htm toshiba pa3593u-1bas akku
    http://www.akkuliste.com/hp/m2000.htm hp m2000 akku
    http://www.akkuliste.com/hp/338794-001.htm hp 338794-001 akku
    http://www.akkuliste.com/hp/395789-001.htm hp 395789-001 akku
    http://www.akkuliste.com/compaq/116314-001.htm compaq 116314-001 akku
    http://www.akkuliste.com/toshiba/satellite-a70.htm toshiba satellite a70 akku
    http://www.akkuliste.com/compaq/319411-001.htm compaq 319411-001 akku
    http://www.akkuliste.com/hp/346970-001.htm hp 346970-001 akku
    http://www.akkuliste.com/hp/367759-001.htm hp 367759-001 akku
    http://www.batteryfast.co.uk/toshiba/pa3285u-1bas.htm toshiba pa3285u-1bas battery
    http://www.batteryfast.co.uk/toshiba/pa3285u-1brs.htm toshiba pa3285u-1brs battery
    http://www.batteryfast.co.uk/toshiba/pa3285u-2bas.htm toshiba pa3285u-2bas battery
    http://www.batteryfast.co.uk/toshiba/pa3285u-2brs.htm toshiba pa3285u-2brs battery
    http://www.batteryfast.co.uk/toshiba/pa3285u-3bas.htm toshiba pa3285u-3bas battery
    http://www.batteryfast.co.uk/hp/nc6000.htm hp nc6000 battery
    http://www.batteryfast.co.uk/hp/nx5000.htm hp nx5000 battery
    http://www.batteryfast.co.uk/hp/nw8000.htm hp nw8000 battery
    http://www.batteryfast.co.uk/hp/nc8000.htm hp nc8000 battery
    http://www.batteryfast.co.uk/hp/v1000.htm hp v1000 battery
    http://www.batteryfast.co.uk/hp/dv2100.htm hp dv2100 battery
    http://www.batteryfast.co.uk/hp/dv2200.htm hp dv2200 battery
    http://www.batteryfast.co.uk/hp/v3000.htm hp v3000 battery
    http://www.batteryfast.co.uk/hp/dv9200.htm hp dv9200 battery
    http://www.batteryfast.co.uk/hp/dv9100.htm hp dv9100 battery
    http://www.batteryfast.co.uk/hp/dv9700.htm hp dv9700 battery
    http://www.batteryfast.co.uk/dell/inspiron-b120.htm dell inspiron b120 battery
    http://www.batteryfast.co.uk/dell/inspiron-b130.htm dell inspiron b130 battery
    http://www.batteryfast.co.uk/dell/inspiron-1200.htm dell inspiron 1200 battery
    http://www.batteryfast.co.uk/toshiba/satellite-p200.htm toshiba satellite p200 battery
    http://www.batteryfast.co.uk/mitac/8375.htm mitac 8375 battery

    1 Nov.
    No namewrote:
    http://www.adapterlist.com/gateway/m360.htm gateway m360 battery
    http://www.adapterlist.com/gateway/m460.htm gateway m460 battery
    http://www.adapterlist.com/hp/nc8430.htm hp nc8430 battery
    http://www.adapterlist.com/hp/dv6000.htm hp dv6000 battery
    http://www.adapterlist.com/hp/hstnn-db42.htm hp hstnn-db42 battery
    http://www.adapterlist.com/hp/dv9000.htm hp dv9000 battery
    http://www.adapterlist.com/hp/dv9700.htm hp dv9700 battery
    http://www.adapterlist.com/hp/hstnn-ib44.htm hp hstnn-ib44 battery
    http://www.adapterlist.com/hp/510.htm hp 510 battery
    http://www.adapterlist.com/hp/530.htm hp 530 battery
    http://www.adapterlist.com/sony/vgp-bps2a.htm sony vgp-bps2a battery
    http://www.adapterlist.com/sony/vgp-bps2b.htm sony vgp-bps2b battery
    http://www.adapterlist.com/sony/vgp-bps2.htm sony vgp-bps2 battery
    http://www.adapterlist.com/sony/vgp-bps2c.htm sony vgp-bps2c battery
    http://www.adapterlist.com/toshiba/pa3399u-1brs.htm toshiba pa3399u-1brs battery
    http://www.adapterlist.com/toshiba/satellite-a100.htm toshiba satellite a100 battery
    http://www.adapterlist.com/toshiba/pa3536u.htm toshiba pa3536u battery
    http://www.adapterlist.com/dell/inspiron-1300.htm dell inspiron 1300 battery
    http://www.adapterlist.com/dell/xps-1330.htm dell xps 1330 battery
    http://www.adapterlist.com/dell/xps-m1330.htm dell xps m1330 battery
    http://www.adapterlist.com/dell/inspiron-6000.htm dell inspiron 6000 battery
    http://www.adapterlist.com/dell/inspiron-9300.htm dell inspiron 9300 battery
    http://www.adapterlist.com/dell/e1705.htm dell e1705 battery
    http://www.adapterlist.com/hp/nc8230.htm hp nc8230 battery
    http://www.adapterlist.com/hp/dv9500.htm hp dv9500 battery
    http://www.adapterlist.com/hp/dv2000.htm hp dv2000 battery
    http://www.adapterlist.com/dell/latitude-d620.htm dell latitude d620 battery
    http://www.adapterlist.com/hp/dv6000.htm hp dv6000 battery
    http://www.adapterlist.com/hp/dv2700.htm hp dv2700 battery
    http://www.adapterlist.com/dell/inspiron-b130.htm dell inspiron b130 battery
    http://www.adapterlist.com/toshiba/pa3534u-1brs.htm toshiba pa3534u-1brs battery
    http://www.adapterlist.com/dell/inspiron-1525.htm dell inspiron 1525 battery
    http://www.adapterlist.com/dell/d9200.php Notebook Laptop Battery for Dell D9200 D5318 G5260 laptop battery ,
    http://www.adapterlist.com/acer/tm4200.php Battery ACER BATBL50L6 Travelmate 4200 4203 4230 4260 4280 laptop battery ,
    http://www.adapterlist.com/dell/inspiron-1200.htm dell inspiron 1200 battery
    http://www.adapterlist.com/toshiba/pa3534u-1brs.htm toshiba pa3534u-1brs battery
    http://www.adapterlist.com/toshiba/pa3399u-1bas.htm toshiba pa3399u-1bas battery
    http://www.adapterlist.com/toshiba/pa3399u-2bas.htm toshiba pa3399u-2bas battery
    http://www.adapterlist.com/gateway/m320.htm gateway m320 battery
    http://www.adapterlist.com/toshiba/satellite-a105.htm toshiba satellite a105 battery
    http://www.adapterlist.com/compaq/nc4400.htm compaq nc4400 battery
    http://www.adapterlist.com/compaq/tc4200.htm compaq tc4200 battery
    http://www.adapterlist.com/compaq/nc4200.htm compaq nc4200 battery
    http://www.adapterlist.com/dell/e1405.htm dell e1405 battery
    http://www.adapterlist.com/hp/hstnn-db02.htm hp hstnn-db02 battery
    29 Oct.

    Trackbacks

    The trackback URL for this entry is:
    http://dmoisan.spaces.live.com/blog/cns!95CB015E3E4A702A!173.trak
    Weblogs that reference this entry
    • None