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: Find Windows RDS Roles and Their Licensing Servers

# Get TS Licensing Servers (Enterprise or AD Registered) $termLicenseServers=Get-ADGroupMember -Identity "Terminal Server License Servers"…

PowerShell: Remove A-record in Bluecat

$bluecatUri='https://bluecat.kimconnect.com/Services/API' $bluecatUsername='bluecat-service-api' $bluecatPassword='SOMECOMPLEXPASSWORD' $hostRecord='testrecord.kimconnect.com' function loginBluecat{ param( $username, $password, $uri ) $proxy = New-WebServiceProxy -Uri…

How to Restart Domino Services without Reboot

--------------Restart_Domino.bat------------------------------------------------------net stop "Lotus Domino Server (LotusDominoData)"c:\bats\pulist | findstr /I /C:"nadminp.exe" >c:\bats\pid.lstc:\bats\pulist | findstr /I /C:"naldaemn.exe"…