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.