Forum Discussion

Cole_McDonald's avatar
Cole_McDonald
Icon for Professor rankProfessor
2 years ago

increasing speed of 'Sources using Powershell Remote Sessions

TLDR: don't load session profiles when using Powershell Remote.  Use an explicit pssession with the -nomachineprofile flag present.

Several LM provided 'Sources use it.  Many of the 'Sources I've written in the past use it as well.  Here's the find/replace for the LM provided ones (find & replace are separated into comment regions):

#region FIND
try {
    #-----Determin the type of query to make-----
    # check to see if this is monitoring the localhost collector, as we will not need to authenticate.
    if ($hostname -like $collectorName) {
        $response = Invoke-Command -ScriptBlock $scriptBlock
    }
    # are wmi user/pass set -- e.g. are these device props either not substiuted or blank
    elseif (([string]::IsNullOrWhiteSpace($wmi_user) -and [string]::IsNullOrWhiteSpace($wmi_pass)) -or (($wmi_user -like '*WMI.USER*') -and ($wmi_pass -like '*WMI.PASS*'))) {
        # no
        $response = Invoke-Command -ComputerName $hostname -ScriptBlock $scriptBlock
    } else {
        # yes. convert user/password into a credential string
        $remote_pass = ConvertTo-SecureString -String $wmi_pass -AsPlainText -Force;
        $remote_credential = New-Object -typename System.Management.Automation.PSCredential -argumentlist $wmi_user, $remote_pass;
        $response = Invoke-Command -ComputerName $hostname -Credential $remote_credential -ScriptBlock $scriptBlock
    }
    exit 0
} catch {
    # exit code of non 0 will mean the script failed and not overwrite the instances that have already been found
    throw $Error[0].Exception
    exit 1
}
#endregion

#region REPLACE
try {
    $option        = New-PSSessionOption -NoMachineProfile

    #-----Determin the type of query to make-----
    if ($hostname -like $collectorName) {
        # check to see if this is monitoring the localhost collector,
        # as we will not need to authenticate.
        $session = new-pssession              `
            -SessionOption $option
    } elseif (
        ([string]::IsNullOrWhiteSpace($wmi_user)          `
        -and [string]::IsNullOrWhiteSpace($wmi_pass)) `
        -or (
            ($wmi_user -like '*WMI.USER*')           `
            -and ($wmi_pass -like '*WMI.PASS*')
        )
    ) {
        # are wmi user/pass set
        # -- e.g. are these device props either not substiuted or blank
        # no
        $session = new-pssession              `
            -computername  $hostname          `
            -SessionOption $option
    } else {
        # yes. convert user/password into a credential string
        $remote_pass       = ConvertTo-SecureString `
            -String        $wmi_pass `
            -AsPlainText `
            -Force;
        $remote_credential = New-Object `
            -typename      System.Management.Automation.PSCredential `
            -argumentlist  $wmi_user, $remote_pass;
        $session           = new-pssession              `
            -computername  $hostname          `
            -credential    $remote_credential `
            -SessionOption $option
    }

$response = Invoke-Command      `
    -session       $session     `
    -ScriptBlock   $scriptBlock        

    exit 0
} catch {
    # exit code of non 0 will mean the script failed and not overwrite the instances
    # that have already been found
    throw $Error[0].Exception
    exit 1
}
#endregion

p.s. the replacement code has the formatting I prefer.  Feel free to change it to suit your whitespace/line length needs.  Mine is a blend of every language I've ever used as I traditionally have been the only one looking at my code.  I call my formatting method the "I have to fix this 2 years from now and have a half an hour to figure it out" format.  generally, 1 specific function per line, sections that collapse into a single line to make it easier to work through the code.  The first character of each line should inform how it relates to the line above it.  spaces added to make neat functional columns of similar parts of the line (parameter name, variable).  Most programmers hate the formatting I use, but it works for me.  There are line continuation characters " `" to make it all fall into place.

No RepliesBe the first to reply