This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add custom type with static method | |
Add-Type -MemberDefinition @" | |
public static string SayHello() | |
{ | |
string Hello = "Hello"; | |
return Hello; | |
} | |
"@ -Name Stuff -Namespace My | |
# Define custom function calling my custom type | |
function Invoke-CustomType { | |
[My.Stuff]::SayHello() | |
} | |
# Define custom function NOT using my custom type | |
function Invoke-CustomFunction { | |
Write-Output 'Not using Custom Type!' | |
} | |
# Define script using my Invoke-CustomFunction function | |
$callingFunction = { | |
Write-Output (Invoke-CustomFunction) | |
} | |
# Define script using my Invoke-CustomType function | |
$callingType = { | |
Write-Output (Invoke-CustomType) | |
} | |
## | |
## TEST 1 - Not using my custom type | |
## | |
# Setting up an initial session state object | |
$initialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault() | |
# Getting the function definition for the function I want to add | |
$functionDefinition = Get-Content function:\Invoke-CustomFunction | |
$functionEntry = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList "Invoke-CustomFunction", $functionDefinition | |
# And add it to the iss object | |
$initialSessionState.Commands.Add($functionEntry) | |
# Create a new runspace using my iss object | |
$runspace = [System.Management.Automation.PowerShell]::Create($initialSessionState) | |
# Add the script I want to run in the runspace | |
[void]$runspace.AddScript($callingFunction) | |
# Start the job | |
$handle = $runspace.BeginInvoke() | |
# Sleep for a while so the job gets time to finish | |
Start-Sleep -Milliseconds 50 | |
# Check if job is complete, and retreive the results if it is. | |
if ($handle.IsCompleted) { | |
$runspace.EndInvoke($handle) | |
} | |
else { | |
"Job not complete!" | |
} | |
# Dispose of the runspace object | |
$runspace.Dispose() | |
## CLEAN-UP | |
Remove-Variable -Name initialSessionState,functionDefinition,functionEntry,runspace,handle -ErrorAction SilentlyContinue | |
## | |
## TEST 2 - Using my custom type | |
## | |
# Setting up an initial session state object | |
$initialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault() | |
# Getting the function definition for the function I want to add | |
$functionDefinition = Get-Content function:\Invoke-CustomType | |
$functionEntry = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList "Invoke-CustomType", $functionDefinition | |
# And add it to the iss object | |
$initialSessionState.Commands.Add($functionEntry) | |
# Get the type data for the custom type that I want to add | |
$typeData = New-Object System.Management.Automation.Runspaces.TypeData -ArgumentList "My.Stuff" | |
$typeEntry = New-Object System.Management.Automation.Runspaces.SessionStateTypeEntry -ArgumentList $typeData,$false | |
# And add it to the iss object | |
$initialSessionState.Types.Add($typeEntry) | |
# Create a new runspace using my iss object | |
$runspace = [System.Management.Automation.PowerShell]::Create($initialSessionState) | |
# Add the script I want to run in the runspace | |
[void]$runspace.AddScript($callingType) | |
# Start the job | |
$handle = $runspace.BeginInvoke() | |
# Sleep for a while so the job gets time to finish | |
Start-Sleep -Milliseconds 100 | |
# Check if job is complete, and retreive the results if it is. | |
if ($handle.IsCompleted) { | |
$runspace.EndInvoke($handle) | |
} | |
else { | |
"Job not complete!" | |
} | |
# Dispose of the runspace object | |
$runspace.Dispose() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment