Function to perform commands on remote server(s)

This topic contains 4 replies, has 2 voices, and was last updated by  Kawika Moss 3 years, 3 months ago.

  • Author
  • #23660

    Kawika Moss

    What I'm trying to do is, within the function, set-content of a bat file to a bat file cmd based on the version of the application then run the cmd, then get-content of a log that is written by the command ad return the results. I can get this to work on a locally but if I try to do it remotely I'm hitting issues...

    Any suggestions?

    See attached

  • #23662

    Don Jones

    Magazines have issues. Sounds like you're having [i]problems[/i]. It'd help to know what this problems are, as I can't run your code in your environment :).

    You don't describe how you're running this function. If you're doing so by using Remoting (Invoke-Command), then you're very likely running into the "second hop" problem that many people run into. It's well-covered in "Secrets of PowerShell Remoting" (free, from the Resources/eBooks menu here). Your credentials get delegated by Invoke-Command to the remote machine, but by default it can't delegate them any further, so accessing a UNC would fail.

    You would either need to accommodate additional delegation hops, likely via CredSSP, or re-think the way you're moving information around to avoid the double-hop.

    Now, I don't [b]know[/b] that this is your problem, because I don't know how you're running this function remotely, nor do I know what problems you're running into when you do so. Right now, I'm just guessing, and hoping you'll share some more details.

  • #23667

    Kawika Moss

    So this is how I'm doing it when I run it locally and it works fine

        Set-Content D:\TestScript\NUIX\NuixLicenseTest.bat $QueryText -Encoding Ascii
        	[void](cmd.exe "/c D:\TestScript\NUIX\NuixLicenseTest.bat")
        $LicenseStatus = Get-Content D:\TestLogs\NUIX\NuixLicenseTest.log
        Write-Verbose -Message  "License Status: $LicenseStatus"

    but I need to be able to do this on remote servers, so I've tried:

    This way the path wasn't recognized...

        Set-Content \\$ServerName\D$\TestScript\NUIX\NuixLicenseTest.bat $QueryText -Encoding Ascii
        	[void](cmd.exe "/c \\$ServerName\D$\TestScript\NUIX\NuixLicenseTest.bat")
        $LicenseStatus = Get-Content \\$ServerName\D$\TestLogs\NUIX\NuixLicenseTest.log
        Write-Verbose -Message  "License Status: $LicenseStatus"

    This way didn't write the $QueryText that is stored as a variable

        Invoke-Command -ComputerName $ServerName {Set-Content D:\TestScript\NUIX\NuixLicenseTest.bat $QueryText -Encoding Ascii}
        	Invoke-Command -ComputerName $ServerName {[void](cmd.exe "/c $BatchFileLocation")}
        $LicenseStatus = Invoke-Command -ComputerName $ServerName {Get-Content D:\TestLogs\NUIX\NuixLicenseTest.log}
        Write-Verbose -Message  "License Status: $LicenseStatus"

    If I'm approaching this correctly, I'd love some advice on a different approack/

    Thank You

  • #23669

    Don Jones

    It'd actually help if you posted actual error messages.

    One possibility:

    $LicenseStatus = Get-Content \\$ServerName\D$\TestLogs\NUIX\NuixLicenseTest.log

    That path should probably be in double quotes. Also, notice D$. The dollar sign is going to confuse PowerShell, because it's going to seem like a variable. Put a backtick in front of that dollar sign so that it's treated as a literal character.

    In your Invoke-Command example, the problem is that you're asking the remote computer to evaluate $QueryText. The remote computer doesn't HAVE a $QueryText variable, though. You can use $using:QueryText (PowerShell v4 and later, I believe) instead, which will grab the value from $QueryText on your local computer, and send that.

    You do have to keep track of what code is running locally, and what code is running remotely, and pay attention to how PowerShell is parsing your commands and values. It's definitely complicated.

  • #23676

    Kawika Moss

    Ok, I tested the following:

    $ServerName = 'mldpanux12'
    $LicenseStatus = Get-Content "\\$ServerName\D`$\TestLogs\NUIX\NuixLicenseTest.log"
    Write-Host $LicenseStatus

    which returned the following error:

    Get-Content : Cannot find path '\\mldpanux12\D$\TestLogs\NUIX\NuixLicenseTest.log' because it does not exist.
    At line:2 char:18
    + $LicenseStatus = Get-Content "\\$ServerName\D`$\TestLogs\NUIX\NuixLicenseTest.lo ...
    +                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (\\mldpanux12\D$...LicenseTest.log:String) [Get-Content], ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

    but this seemed to work

        Invoke-Command -ComputerName $ServerName {Set-Content D:\TestScript\NUIX\NuixLicenseTest.bat $using:QueryText -Encoding Ascii}
    	Invoke-Command -ComputerName $ServerName {[void](cmd.exe "/c D:\TestScript\NUIX\NuixLicenseTest.bat")}
        $LicenseStatus = Invoke-Command -ComputerName $ServerName {Get-Content D:\TestLogs\NUIX\NuixLicenseTest.log}
        Write-Host "License Status: $LicenseStatus"

    Thanks for your help!!!

You must be logged in to reply to this topic.