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"

1
  • 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. Commented Oct 10, 2019 at 3:15

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.