PowerShell: Function to Execute Function Remotely

Function this function that – this quick snippet is to invoke those func things remotely…

More complex version:

$computerNames=$env:computerName,"localhost"

function pingSomething{
param($names)
$result=$names|%{ping $_}
return $result;
}

# Limitation: this function currently only takes one function with multiple parameters (that are not nested functions)
function execFunctionAsJob{
param($computername,$functionName,$parameters)
#$scriptBlock=${function:$functionName} # another method to retrieve the inside codes of a provided function
$scriptBlock=(Get-Command $functionName -CommandType Function).Definition;

<# Alternative method
$session=new-pssession -ComputerName $computername
$job=invoke-command -Session $session -ScriptBlock{
param ($importedFunction,$params)
return [ScriptBlock]::Create($importedFunction).invoke($params);
} -args $scriptBlock,$parameters
Remove-PSSession $session
#>
$job=invoke-command -ComputerName $computername -ScriptBlock{
param ($importedFunction,$params)
return [ScriptBlock]::Create($importedFunction).invoke($params);
} -args $scriptblock,$parameters -AsJob
return $job
}

function simultaneousExec{
param($computerNames,$functionName,$parameters)
$timer=[System.Diagnostics.Stopwatch]::StartNew();
$jobIds=@()
foreach ($computer in $computerNames){
$job=execFunctionAsJob -computername $computer -functionName $functionName -parameters $parameters
#$job=execFunctionAsJob -computername $computer -functionName "pingSomething" -parameters "yahoo.com","google.com"
$jobIds+=,$job.Id
}
Wait-Job -Id $jobIds;
$jobResults=Receive-Job -Id $jobIds;
Remove-Job -id $jobIds;
$time=[math]::round($timer.Elapsed.TotalHours,2);
$timer.Stop();
$timer.Reset();
$closingMessage="`r`nOverall Time: $time hours"
write-host $closingMessage;

return $jobResults
}

simultaneousExec -computerNames $computerNames -functionName "pingSomething" -parameters "kimconnect.com","google.com","yahoo.com"

Simple version:

function pingSomething{
param($name)
return ping $name;
}

function execFunctionRemotely{
param($computername,$functionName,$parameters)
#$scriptBlock=${function:$functionName} # another method to retrieve the inside codes of a provided function
$scriptBlock=(Get-Command $functionName -CommandType Function).Definition;
$session=new-pssession -ComputerName $computername
$result=invoke-command -Session $session -ScriptBlock{
param ($importedFunction,$params)
return [ScriptBlock]::Create($importedFunction).invoke($params);
} -args $scriptBlock,$parameters
Remove-PSSession $session
<# Alternative method
$result=invoke-command -ComputerName $computername -ScriptBlock{
param ($importedFunction,$params)
return [ScriptBlock]::Create($importedFunction).invoke($params);
} -args $scriptblock,$parameters
#>
return $result
}
execFunctionRemotely -computername $env:computername -functionName "pingSomething" -parameters "google.com"

Leave a Reply

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