Posted On March 29, 2019

PowerShell: Passing Local Functions to Remote WinRM Sessions

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Passing Local Functions to Remote WinRM Sessions
# Dynamic Credential Method 1
$who = whoami
	if ($who.substring($who.length-2, 2) -eq "-a"){$username=$who;}
    else {$username=$who+"-admin";}
#$password = Read-Host -Prompt "Input the password for account $username" -AsSecureString
$password=convertto-securestring "PASSWORD" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$password	#To be Invoked with switch -Credential
$elevate = New-PSSession -ComputerName localhost -Credential $cred	#$To be Invoked with switch -Session

# Local function
function localFunc ($x, $y)
{
    ping google.com
}

Invoke-Command -Session $elevate -ScriptBlock { 
    param( $x, $y, $importedFunc)

    # Import the function from the variable inside parameters
    [ScriptBlock]::Create($importedFunc).Invoke($x,$y)

} -ArgumentList $xVariable, $yVariable, ${function:localFunc}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Automating Process of Setting Up a New Windows Machine

# Step -2: Creating a New Virtual Machine # Example A: Hyper-V # Compulsory variables…

PowerShell: Allow Log On To Remote Desktop Service

# editWindowsSecurity.ps1 # Version 0.0.1 # Notes: # - This has NOT been thoroughly tested.…

Basic CSS: Size Your Images

<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet" type="text/css"><style>.red-text {color: red;}h2 {font-family: Lobster, monospace;}p {font-size: 16px;font-family: monospace;}</style><h2 class="red-text">CatPhotoApp</h2><main><p class="red-text">Click here…