Open-IseFunction.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<#PSScriptInfo
 
.VERSION 1.0.2
 
.GUID 27460bb0-a2ad-4826-8327-025cfe826845
 
.AUTHOR Warren F.
 
.TAGS
    ISE, Author, Script, Productivity
 
.LICENSEURI
    https://github.com/RamblingCookieMonster/PowerShell/blob/master/LICENSE
 
.PROJECTURI
    https://github.com/RamblingCookieMonster/PowerShell
 
.DESCRIPTION
    Open a function in ISE. Any function that can be obtained by (get-command <command>).definition. Pretty much anything that isn't compiled in a DLL or obfuscated in some other manner.
#>


function Open-IseFunction {
    <#
        .SYNOPSIS
            Open a function in ISE
        .DESCRIPTION
            Open a function in ISE. Any function that can be obtained by (get-command <command>).definition. Pretty much anything that isn't compiled in a DLL or obfuscated in some other manner.
        .FUNCTIONALITY
            General Command
    #>

    [cmdletbinding()]
    param(

        #In the validation block, check if input is a function
        [ValidateScript({ Get-Command -commandtype function -name $_ })]
        [string[]]
        $function
    )

    foreach($fn in $function){
    
        #Get the definition
        $definition = (Get-Command -commandtype function -name $fn).definition
    
        #If the definition exists, add a new tab with the contents.
        if($definition){
        
            #Definition won't include function keyword. Add it.
            $definition = "function $fn { $definition }"
        
            #Add the file and definition content
            $tab = $psise.CurrentPowerShellTab.files.Add()
            $tab.editor.text = $definition

            #set the caret to column 1 line 1
            $tab.editor.SetCaretPosition(1,1)

            #Sleep a few milliseconds. Not sure why but omitting this has caused issues for me.
            Start-Sleep -Milliseconds 200
        }
    }
}