The Glory of Quick and Dirty Scripting

The Glory of Quick and Dirty Scripting

  • Comments 1

One of the things I want us to consider doing in the future is to leverage our Universal Code Execution infrastructure to execute any kind of code. For example, imagine that I have some script foo.vbs, I’d like to do the following:

PS>  Invoke-Command –FilePath foo.vbs –computer (cat servers.txt)

I was about to file a record for us to consider this and thought – wait – I bet that there are other commands that take a FilePath need to be updated as well.  The question is – which commands take the FilePath parameter?   As I write this up, I now remember the correct solution to this question.  We all know that you can Get-Help on a command like Invoke-Command

PS> Get-Help Invoke-Command
…….

But did you realize that you can also get help about the parameters of a command?

PS> Get-Help Invoke-Command -Parameter FilePath

-FilePath <string>
    Runs the specified local script on one or more remote computers. Enter the path and file name of the script, o
    r pipe a script path to Invoke-Command. The script must reside on the local computer or in a directory that th
    e local computer can access. Use the ArgumentList parameter to specify the values of parameters in the script.
    When you use this parameter, Windows PowerShell converts the contents of the specified script file to a script
     block, transmits the script block to the remote computer, and runs it on the remote computer.
    Required?                    true
    Position?                    2
    Default value               
    Accept pipeline input?       false
    Accept wildcard characters?  false

 

 

A lot of people know that one but I wonder how many people realized that you can actually omit the command name to find all the commands that use a parameter:

PS> get-help * -Parameter FilePath

Name                              Category  Synopsis                                                                                                                      
----                              --------  --------                                                                                                                      
Invoke-WSManAction                Cmdlet    Invokes an action on the object that is specified by the Resource URI and by the selectors.                                   
Set-WSManInstance                 Cmdlet    Modifies the management information that is related to a resource.                                                            
Invoke-Command                    Cmdlet    Runs commands on local and remote computers.                                                                                  
Start-Job                         Cmdlet    Starts a Windows PowerShell background job.                                                                                   
Out-File                          Cmdlet    Sends output to a file.                                                                                                       
Tee-Object                        Cmdlet    Saves command output in a file or variable, and displays it in the console.                                                   
Set-TraceSource                   Cmdlet    Configures, starts, and stops a trace of Windows PowerShell components.                                                       
Trace-Command                     Cmdlet    Configures and starts a trace of the specified expression or command.                                                         
Start-Process                     Cmdlet    Starts one or more processes on the local computer.                                                                           
Get-PfxCertificate                Cmdlet    Gets information about .pfx certificate files on the computer.                                                                
Get-AuthenticodeSignature         Cmdlet    Gets information about the Authenticode signature in a file.                                                                  
Set-AuthenticodeSignature         Cmdlet    Adds an Authenticode signature to a Windows PowerShell script or other file.                                                  

 

Pretty awesome isn’t it?

 

But here is the deal – I forgot about that when I had the problem so I wrote myself a quick and dirty script that gave me exactly what I needed.  Notice that when I’m writing throw away scripts, I almost allows name them “t” or “t1” or “t2”…   Why? Because its easy to type and I’m throwing the thing away when I’m done.

PS> function t ($p) {gcm -Type Cmdlet  |where {$_.Parameters.$p}}
PS> t filepath

CommandType     Name                                              Definition                                     
-----------     ----                                              ----------                                     
Cmdlet          Get-AuthenticodeSignature                         Get-AuthenticodeSignature [-FilePath] <String...
Cmdlet          Get-PfxCertificate                                Get-PfxCertificate [-FilePath] <String[]> [-V...
Cmdlet          Invoke-Command                                    Invoke-Command [-ScriptBlock] <ScriptBlock> [...
Cmdlet          Invoke-WSManAction                                Invoke-WSManAction [-ResourceURI] <Uri> [-Act...
Cmdlet          New-WSManInstance                                 New-WSManInstance [-ResourceURI] <Uri> [-Sele...
Cmdlet          Out-File                                          Out-File [-FilePath] <String> [[-Encoding] <S...
Cmdlet          Set-AuthenticodeSignature                         Set-AuthenticodeSignature [-FilePath] <String...
Cmdlet          Set-TraceSource                                   Set-TraceSource [-Name] <String[]> [[-Option]...
Cmdlet          Set-WSManInstance                                 Set-WSManInstance [-ResourceURI] <Uri> [[-Sel...
Cmdlet          Start-Job                                         Start-Job [-ScriptBlock] <ScriptBlock> [[-Ini...
Cmdlet          Start-Process                                     Start-Process [-FilePath] <String> [[-Argumen...
Cmdlet          Tee-Object                                        Tee-Object [-FilePath] <String> [-InputObject...
Cmdlet          Trace-Command                                     Trace-Command [-Name] <String[]> [-Expression...

 

 

 

That was the real point of this blog – that sometimes you just want to write a quick and dirty script and that is perfectly OK – you can skip all the best practices and just go go go! 

 

But wait – it get’s better.  When I looked at the output of those 2 approaches, something didn’t seem right so I thought I would do a little more investigation

PS> $x1 = get-help * -param filepath |select -expand name |sort
PS> $x2 = t filepath |select -expand name |sort
PS> compare $x1 $x2

InputObject                                               SideIndicator                                          
-----------                                               -------------                                          
New-WSManInstance                                         =>                                                     

PS> get-help New-WSManInstance -param  f* |select name

name                                                                                                             
----                                                                                                             
File                                                                                                             

 

So what that means is that we have a documentation bug (a benign one but still a bug).  It has documented the parameter as “FILE” when in fact it is “FILEPATH”.

 

 

I gotta tell you – I just LOVE working with PowerShell.  It makes you feel so POWERFUL.  That sounds corny and trite but it’s true!

Go have some fun!

Jeffrey Snover [MSFT]
Distinguished Engineer
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 6 and 6 and type the answer here:
  • Post
  • Sounds like you just found a way to regress-test the documentation of all parameters of all functions.

Page 1 of 1 (1 items)