- Function Open-FunctioninISE {
- <#
- .SYNOPSIS
- Opens a function in ISE
- .DESCRIPTION
- This enables you to specify the function to open. The definition
- comes from (Get-Command <command>).definition.
- You specify the name of the function to open, or select it
- in the ISE
- .NOTES
- File Name : Open-FunctionInIse.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0
- Based on a technet script published at http://gallery.technet.microsoft.com/scriptcenter/Open-defined-functions-in-22788d0f
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .PARAMETER Function
- The name of a defined function to open in new PowerShell ISE tab.
- .EXAMPLE
- C:\Psh> Open-FunctionInIse Open-FunctioninISE
- Opens Open-FunctionInIse in a new PowerShell ISE tab
- .EXAMPLE
- Select a function name in an open edit window, then hit
- Ctrl+Shift+E (or use the Add-ons menu)
- #>
- # Define parameters
- [Cmdletbinding()]
- Param(
- [Parameter(Position=0)]
- [ValidateScript({ Get-Command -commandtype function -name $_ })]
- [String] $function
- )
- # Start of the function.
- Process{
- # Get the function name (i.e. the selected text) and ensure it's not empty
- $fn = $psise.currentfile.Editor.selectedText
- # if nothing selected, see if we got called with it
- If (!$fn -or ($fn.length -LE 0)) {
- if ($function -and ($function.length -GT 0)){
- $fn = $function}
- Else {
- $fn = Read-Host -Prompt "Enter function name to view"
- }
- }
- If (!$Fn) {'No function to edit';return}
- # Get the definition, if there is one
- $definition = (Get-Command -commandtype function -name $fn).definition
- If (!$definition -or ($Definition.Length -le 0)) {return "Function [$fn] not found"}
- # Create What to see
- $FunctionHeader = "Function $fn {`n"
- $comments = "`# Description : $($definition.description)`n"
- $comments += "`# Module : $($definition.module)`n`n"
- $FunctionTrailer = "`n}"
- # Wrap it all up
- $definition = $functionHeader + $Comments + $Definition + $FunctionTrailer
- "function $fn {" + $definition + "}"
- # Add a tab, add text to the tab, set caret position to first character
- $tab = $Psise.CurrentPowerShellTab.Files.Add()
- $tab.Editor.text = $definition
- $tab.Editor.SetCaretPosition(1,1)
- # Sleep for a moment. Ran into issues without this.
- Start-Sleep -Milliseconds 200
- } # End process block
- } # End Function
- Set-ALias OFISE .\Open-FunctionInISE
- # Here we could test the function from the command line.
- # Function test123 {'testing 1-2-3'}
- # Open-FunctioninISE test123
- # I assume you'll just use this function so I've left the following
- # Code in place to add this as a menu item
- # Here add to the ISE as long as it's not there already!
- $x = ($psise.CurrentPowerShellTab.AddOnsMenu.Submenus).displayname
- if (! ($x -contains "_Edit Function in ISE")) {
- $Psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("_Edit Function in ISE", {Open-FunctioninISE},
- "Ctrl+Shift+E") | Out-Null
- }
- Else {
- 'Function already added in ISE'
- }
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Sunday, 20 January 2013
Open-FunctionInISE
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment