Archive for the ‘Script’ Category

Articles

Automate your processes used to manage your PowerShell code–5 part series.

In Function,Help,Module,PowerShell,PowerShell Version 3.0,PSDefaultParameterValues,PSDrive,PSHelp,PSModulePath,PSProvider,Script,SkyDrive on September 1, 2012 by brwilkinson Tagged: , , ,

I have written a brief 5 part series on Automating your PowerShell environment to increase productivity.

This involves several key components that come together to streamline writing and accessing scripts, functions, modules and Help information in our PowerShell environment.

One of the important factors is the SkyDrive that does the back end replication of our files. One thing to note is that some of the features such as updateable Help discussed in the series is based around PowerShell Version 3.0.

Here is some of the architecture:

image

Here are the links to the articles:

  1. Are you following me? PowerShell everywhere, with SkyDrive. How to keep your files in Sync.
  2. Save your Pennies in a jar! Building your own module to save your code.
  3. Save time, Save your settings, Set Defaults with PSDefaultParameterValues.
  4. Help is on the way. Easily Save and Update Help across all of your systems.
  5. Save your Pennies in a jar! Create some shortcuts to easily manage your code and scripts.

I hope that you may find some useful ideas from these posts, I certainly enjoy being able to work on any of my own machines and know that I will have ALL of my code and functions available.

I particularly like some of the new PowerShell v 3.0 features such as the default parameter sets and I bet you could think of many new uses to leverage this feature.

Also if you have any more tips and tricks I would love to hear some of them . . .

Articles

Save your Pennies in a jar! Create some shortcuts to easily manage your code and scripts.

In Function,Module,PowerShell,Script on August 31, 2012 by brwilkinson

Our personal module will be our library for all of our code that we will collect and work on.

If we can easily save our functions into that module and also easily open up previous functions for editing then we can save time and increase productivity. Incremental changes to code over time should mean it can become more robust.

The following two functions can help to achieve this goal.

Add-FunctionToModule will be the function that saves our functions from the ISE.

Edit-CmdLet will be the function to easily open them up again in the ISE.

Edit the Module File to add these two script lines to the PS:\MODS\BRW\BRW.psm1 file.

* This is the process that the script will do automatically for us in the future.

. $psScriptRoot\Set-PSDefaultParameterValues.ps1            
. $psScriptRoot\Add-FunctionToModule.ps1            
. $psScriptRoot\Edit-Cmdlet.ps1            
Export-ModuleMember -function * -alias *            

Then save the two functions below into the associated file paths.

* The saving of the files will also be automated when using these scripts in the future.

E.g.

PS:\MODS\BRW\Add-FunctionToModule.ps1

PS:\MODS\BRW\Edit-Cmdlet.ps1

Functions are below:

Add-FunctionToModule.ps1

function Global:Add-FunctionToModule            
{            
#Requires -Version 2.0            
[CmdletBinding()]            
 Param             
   (            
    [Parameter(Mandatory=$true,            
               ValueFromPipeline=$false,            
               ValueFromPipelineByPropertyName=$false)]            
    [String]$FunctionName,            
    [String]$ModuleName = "BRW"            
   )#End Param            
            
Begin            
{            
    $BasePath = Get-Module $ModuleName -ListAvailable -ea Stop |             
                Select -ExpandProperty ModuleBase -First 1            
    if ( $BasePath.length -ne 0 )            
        {            
            "`nAdding Function to Module $ModuleName . . .`n"            
            "Module base path: $BasePath"            
        }            
    else            
        {            
            "Module $ModuleName was not found."            
            Break            
        }            
}#Begin            
Process            
{            
    $ExportAll = 'Export-ModuleMember -function * -alias *'            
    $Current = $psise.CurrentFile.FullPath            
    $Filename = $FunctionName + '.ps1'            
    [String]$Functionpath = '. $psScriptRoot\' + $Filename            
    $currentfiles = Get-Content -Path "$BasePath\$ModuleName.psm1"            
    if (!($currentfiles -contains $Functionpath))            
        {            
            $currentfiles = $currentfiles[0..($currentfiles.count-2)] + $Functionpath + $ExportAll            
            $currentfiles | Out-File -FilePath "$BasePath\$ModuleName.psm1" -Encoding ASCII            
        }            
    else             
        {            
            "Function is already in $ModuleName Mods file."            
            Remove-Item -Path $Current -Confirm            
        }            
                
    $psise.CurrentFile.SaveAs("$BasePath\$Filename")            
                
}#Process            
End            
{            
    Import-Module $ModuleName -Force -Global            
    $Filename, $Functionpath = $Null            
}#End            
            
            
}#Function Add-FunctionToModule            

 

Edit-CmdLet.ps1.

function Edit-Cmdlet {            
param             
    (            
        [String]$Cmdlet            
    )            
            
try {            
            
        $Base = Get-Command -Name $Cmdlet -ErrorAction Stop  |             
                Select-Object Module |             
                ForEach-Object {            
                    Get-Module $_.Module -ErrorAction Stop |             
                    Select-Object -ExpandProperty ModuleBase            
                    }            
        $Path = (Join-Path -Path $Base -ChildPath ($Cmdlet + ".*") -Resolve -ErrorAction Stop)             
        psedit -filenames $Path            
    }            
catch            
    {            
        "Cannot find that file, this is likely not a user written cmdlet"            
    }            
            
}#Edit-Cmdlet            

Summary

Now do your work to create your brilliant one liner (up to a complex function) in PowerShell, then wrap it in a function and add some parameters. Now ensure the script/function is the current open tab in the PowerShell ISE, now you can execute the :

Add-FunctionToModule -FunctionName Get-MyDATA

Now the function will automatically be added your you Module in a new script file called Get-MyData.ps1 that will be located in the PS:\PS\MODS\BRW\ directory .

Now if you need to edit the function to modify it, you run:

Edit-Cmdlet -Cmdlet Get-MyDATA
 

Now the script/function will be loaded up in the ISE for quick editing.

One thing to note, is that each time you edit a function that is already loaded into memory, you must re-import the function to get the updated functionality. You can leverage the function that we already defined in our profile for this that is called Import-BRW.

function Import-BRW {import-module brw -force -global -Verbose}
 
So remember to run that each time after you make changes to functions in your module.