Invoke-Command not picking up after Pipe

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5310

    by surge3333 at 2012-11-28 08:08:22

    I’m having an issue trying to get the portion of my scriptblock after a pipe(|) within a Invoke-Command to work.

    Running the command outside of the Invoke-Command works as it should. These commands –

    $Days = “-1”
    $Yesterday = ((Get-Date).AddDays($Days)).ToShortDateString()
    $Yesterday
    Get-ChildItem "\\servername\d$\Program Files\whatever\Server.0.*.log" | Where-Object {$_.LastWriteTime -ge $Yesterday }

    Properlys return this output –

    11/27/2012

    Directory: \\servername\d$\Program Files\whatever

    Mode LastWriteTime Length Name
    —- ————- —— —-
    -a— 11/28/2012 10:46 AM 9298444 Server.0.0.log

    However I need to run from a different server, and am using this –

    $Days = "-1"
    $Yesterday = ((Get-Date).AddDays($Days)).ToShortDateString()
    $Yesterday

    # Define credentials used to connect to the server.
    $User = "domain\user"
    $secpasswd = ConvertTo-SecureString $DomainPswd -AsPlainText -Force
    $Creds = New-Object System.Management.Automation.PSCredential ($User, $secpasswd)

    $Server = "servername"

    $s = new-pssession -computername $Server -credential $Creds
    Invoke-Command -session $s -scriptblock {Get-ChildItem "\\servername\d$\Program Files\whatever\Server.0.*.log" | Where-Object {$_.LastWriteTime -ge $Yesterday } }

    Returns all log files found, regardless of the LastWriteTime specified –

    11/27/2012

    Directory: \\servername\d$\Program Files\whatever

    Mode LastWriteTime Length Name PSComputerName
    —- ————- —— —- ————–
    -a— 11/28/2012 10:39 AM 9296684 Server.0.0.log servername
    -a— 11/23/2012 8:14 AM 10485804 Server.0.1.log servername
    -a— 11/18/2012 5:14 PM 10485820 Server.0.2.log servername
    -a— 11/14/2012 5:27 AM 10486282 Server.0.3.log servername
    -a— 11/10/2012 9:54 AM 10485980 Server.0.4.log servername

    Any thoughts as to what I’m missing here?

    by DonJ at 2012-11-28 08:26:38

    The contents of the -ScriptBlock are passed to the remote server. It doesn't know what $Yesterday is – that variable exists on your local machine. You can look into the -Arguments (I think) parameter of Invoke-Command – especially the examples in the help – for a way to pass variables from your local machine to the remote one. Or, and this is easier, just include your $Yesterday in the -ScriptBlock:


    Invoke-Command -session $s -scriptblock {$Yesterday = ((Get-Date).AddDays($Days)).ToShortDateString() ; Get-ChildItem "\\servername\d$\Program Files\whatever\Server.0.*.log" | Where-Object {$_.LastWriteTime -ge $Yesterday } }

    by surge3333 at 2012-11-28 09:51:27

    Thanks Don, I was able to get that to work using both your example and using the –ArgumentList –

    Invoke-Command -session $s -scriptblock {param($Yesterday) Get-ChildItem "\\servername\d$\Program Files\whatever\Server.0.*.log" | Where-Object {$_.LastWriteTime -ge $Yesterday } } -ArgumentList $Yesterday

    However trying a different command, I’m having issues as well. This guy is just returning blank lines for me, however when running the Get-Content….Select-String locally it works fine –

    Invoke-Command -session $s -scriptblock {param($ParseExpression) Get-Content "\\servername\d$\Program Files\whatever\Server.0.0.log" | Select-String $ParseExpression } -ArgumentList $ParseExpression

    Thanks……..

    by DonJ at 2012-11-28 11:06:05

    Without having access to the files, the content of $ParseExpression, and all that, I can't really troubleshoot that (but no need to upload the file here). The syntax looks correct. Consider troubleshooting it by removing variables. Just pass it a static expression for Select-String, and eliminate the argumentlist entirely, as a test. *Something* is different than what you're running locally. You need to find out what that is.

    by surge3333 at 2012-11-28 12:02:26

    Thanks – I wasn't able to find any combination that would get it to work as a single command. I ended up splitting the Select-String into a separate command and that now works for me –

    Invoke-Command -session $s -scriptblock {$a = Get-Content "\\servername\d$\Program Files\whatever\Server.0.0.log" }
    $a | Select-String $ParseExpression

    Thanks again for the help.

    by Aleksandar at 2012-11-30 09:36:07

    If you have PowerShell 3.0, you can use Using scope. Also, you don't need "\\servername\d$" because you are executing a script block on that server thanks to your $s session.

    Invoke-Command -session $s -scriptblock {Get-ChildItem "d:\Program Files\whatever\Server.0.*.log" | Where-Object {$_.LastWriteTime -ge $Using:Yesterday } }

You must be logged in to reply to this topic.