Skip to content

Instantly share code, notes, and snippets.

@gravejester
Last active October 1, 2016 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravejester/59167ac61729f09f51b3ae3d3fac1bd1 to your computer and use it in GitHub Desktop.
Save gravejester/59167ac61729f09f51b3ae3d3fac1bd1 to your computer and use it in GitHub Desktop.
# 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