# Invoke-Command not picking up after Pipe

**URL:** <https://forums.powershell.org/t/invoke-command-not-picking-up-after-pipe/365>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/invoke-command-not-picking-up-after-pipe/365 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/invoke-command-not-picking-up-after-pipe/365/1 "2011-12-31T18:30:00Z")

</div>

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 } }`

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:03pm UTC](https://forums.powershell.org/t/invoke-command-not-picking-up-after-pipe/365/2 "2024-05-16T21:03:38Z")

</div>


