forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy-PSFunction.ps1
More file actions
54 lines (42 loc) · 1.92 KB
/
Copy-PSFunction.ps1
File metadata and controls
54 lines (42 loc) · 1.92 KB
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
#This assumes you've already created a PowerShell Remoting session
#You must use a PSSession
#get the locally loaded function
# $f = $(get-item function:\Get-Foo).scriptblock
#copy it to the remote session
# Invoke-Command { New-Item -Name Get-Foo -Path Function: -Value $($using:f)} -session $s
#now you can use it remotely
# Invoke-Command { Get-Foo } -session $s
Function Copy-PSFunction {
[cmdletbinding()]
[OutputType("Deserialized.System.Management.Automation.FunctionInfo")]
[Alias("cpfun")]
Param(
[Parameter(Position = 1, Mandatory, ValueFromPipeline, HelpMessage = "Enter the name of a local function.")]
[ValidateNotNullorEmpty()]
[string[]]$Name,
[Parameter(Mandatory, HelpMessage = "Specify an existing PSSession")]
[System.Management.Automation.Runspaces.PSSession]$Session,
[switch]$Force
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
} #begin
Process {
foreach ($item in $Name) {
#verify the function Exists
$funPath = Join-Path -path Function: -childpath $item
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Validating $funPath"
if (Test-Path -path $funPath) {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Copying $item to $($session.computername)"
$f = $(Get-Item -path $funPath).scriptblock
Invoke-Command -scriptblock { New-Item -Name $using:item -Path Function: -Value $($using:f) -force:$using:force} -session $Session
} #if Test-Path
Else {
Write-Warning "Can't find a local function called $item."
}
} #foreach item
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
} #close Copy-PSFunction