0

I have the next code, but when i use the arguments, it doesn't work like I try to find all the txt files in one drive:

Invoke-Command -ComputerName $param3 -Credential $cred -ArgumentList $param4,$param5,$param6,$param7 -ScriptBlock {$A = Get-ChildItem $args[0] -Recurse | Where-Object {$_.name -match ".*$args[1].*"} |  Format-Table Name, CreationTime, Length, FullName -auto***'

*The variable $param5 has the text ".txt"

techguy1029
  • 743
  • 10
  • 29
  • Given that `-match` performs _substring_ matching by default, you needn't surround a search term with `.*`, so you could just use `$args[1]` by itself, but the only real problem with your answer is the direct use of `$args[1]` inside `"..."` - as an _expression_, it must be enclosed in `$(...)` - `".*$($args[1]).*"` - as explained in the [linked post](https://stackoverflow.com/a/50215337/45375). – mklement0 Oct 10 '19 at 03:15

1 Answers1

1

Take the quotes and periods off. That would never work, even locally.

where-object { $_.name -match $args[1] } 
where name -match $args[1]

This doesn't seem to be well documented, but you can add a param section to the script block.

invoke-command c001 { param ($param1,$param2) echo $param1 $param2 } -args 1,2
js2010
  • 23,033
  • 6
  • 64
  • 66