Posted On June 15, 2020

PowerShell: Excute Function Remotely

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Excute Function Remotely
$computername=$env:computerName
$adminUsername='doeMainAdmin'
$adminPassword='frackingPassword'
$adminCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminUsername,$(ConvertTo-securestring $adminPassword -AsPlainText -Force)

function execFunctionRemotely{
    param(
        [Parameter(Mandatory=$true)][string]$computerName,
        [Parameter(Mandatory=$true)][string]$functionName,
        [Parameter(Mandatory=$False)][string[]]$parameters=@(),
        [Parameter(Mandatory=$False)]$credential
    )
    $ErrorActionPreference = "Stop"
    #$scriptBlock=${function:$functionName} # another method to retrieve the inside codes of a provided function
    $scriptBlock=(Get-Command $functionName -CommandType Function).Definition;        
    try{
        if($credential){$session=new-pssession -ComputerName $computername -Credential $credential}
        else{$session=new-pssession -ComputerName $computername}
        write-host "connected to $computername"
        }
    catch{
        write-warning "Unable to connect to remote server.";
        return $false
        }
    
    if ($session){
        write-host 'Please wait while the program executes $functionName on $computerName...'
        $result=invoke-command -Session $session -ScriptBlock{
            param ($importedFunction,$params)
                return [ScriptBlock]::Create($importedFunction).invoke($params);
            } -args $scriptBlock,$parameters
        Remove-PSSession $session
        return $result
        }
}

# Test function: get tracert information
function getTraceRoutes([string[]]$name=('gmail.com','office365.com')){
    $name|%{pathPing $_}
}
execFunctionRemotely $computername getTraceRoutes

Leave a Reply

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

Related Post

PowerShell: Set Virtual Machine Default Paths on Hyper-V Host of a Cluster

$newVirtualMachinePath='D:\VirtualMachines' $newVirtualHardDiskPath='D:\VirtualMachines' function getHyperVHostsInForest{ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709 =…

Basic CSS: Give a Background Color to a div Element

<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;}.thick-green-border {border-color: green;border-width: 10px;border-style:…

PowerShell: Add Accounts into Local Administrators Group

# addAccountToLocalAdmins.ps1# Requires: PowerShell Version 4.0+$accountToAdd='ServerAdmins'$groupName="Administrators"$servers=@( "CONCU1", 'CONCU2', 'CONCU100', 'CONCU80665', 'CONCU6547354', 'CONWHAT989', 'CONCU3')$servers|%{ $session=new-pssession $_…