powershell command not running

Any reason why this is not returning any files. it works locally but when I try to run remotely no error messages or anything is returned.

$path=c:\temp\logs
Invoke-Command -computername server {Get-ChildItem -directory $path -recurse | Where { ((get-date)-$_.LastWriteTime).days -gt 1 }}

Open in new window

techdriveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
You need to pass the path as variable to the scriptblock (positional only).
$Path = c:\temp\logs
Invoke-Command -Computername server -ArgumentList $Path -ScriptBlock {Param($Path) Get-ChildItem -directory $path -recurse | Where { ((get-date)-$_.LastWriteTime).days -gt 1 }}

Open in new window

0
techdriveAuthor Commented:
One more question do I have to enter the param for each argument I use for example if I use the following with adding a list of servers


$listofservers=get-content c:\temp\servers.csv
foreach(server in $listofservers) {
$Path = c:\temp\logs
Invoke-Command -Computername $server -ArgumentList $Path -ScriptBlock {Param($Path) Get-ChildItem -directory $path -recurse | Where { ((get-date)-$_.LastWriteTime).days -gt 1 }}

write-host "$server with files"

}
0
oBdACommented:
Any variable from the main script that you want to use in the remote scriptblock needs to be passed as parameter. The scriptblock by itself will know nothing about your main script variables.
Maybe this will make it clearer:
$Foo = "Foo"
$Bar = "Bar"
"Main script:"
"`t`$Foo: $($Foo)"
"`t`$Bar: $($Bar)"
"Invoking scriptblock, passing two positional arguments `$Foo and `$Bar ..."
Invoke-Command -ComputerName mr1idcpb001 -ArgumentList $Foo, $Bar -ScriptBlock {
Param(	## can be named anything, will be passed by position only.
	$Arg1,
	$Arg2
)
	"`tInside the scriptblock with two positional arguments `$Arg1 and `$Arg2:"
	"`t`t`$Foo: $($Foo)"	## not known inside the scriptblock!
	"`t`t`$Bar: $($Bar)"	## not known inside the scriptblock!
	"`t`t`$Arg1: $($Arg1)"	## First argument passed, "Foo"
	"`t`t`$Arg2: $($Arg2)"	## Second argument passed, "Bar"
	"`tLeaving the scriptblock."
}
"... done."

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.