Posted On February 10, 2020

PowerShell: Function to Execute Function Remotely

kimconnect 0 comments
blog.KimConnect.com >> Codes >> 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 *

Related Post

PowerShell: Backup, Archive, and Remove a Microsoft SQL Database

$sqlServer='sqlsherver007' $databaseName="someDatabaseName" $saCredential=get-credential $backupDestination="\\Archive03\MSSQL_Backups" function triggerSqlBackup($sqlServer,$databaseName){ # Ensure the the Jump Box has SQL PowerShell…

PowerShell: Replacing Characters Inside Text Files

$textFilesDirectory="C:\Users\$env:computername\Desktop\test" $textFileExtensions='.txt' $regexNewlineOnly="(?<!\r)\n$" $replaceWith="`r`n" function updateFile{ param( $file="C:\Users\$env:username\Desktop\test.txt", $regexMatch="(?<!\r)\n$", $replaceWith="`r`n", $output ) $output=if($output){$output}else{$file} if($file -ne…

Check Whether an Entity Has Access to a Directory or Its Children

Write-Host "This script just be ran in the context of a File Server Administrators member"…
Other project: DragonCoin.com