3

In the below code I was using $scripts variable to iterate through a foreach loop inside the Invoke-Command statement. But $script values are not replacing properly and outcome seems to be single string as "count.sql size.sql". The foreach loop is executing properly if defined outside the Invoke-Command loop.

Is there any particular way to define foreach loop inside Invoke-Command?

$scripts = @("count.sql", "size.sql")
$user = ""
$Password = ""
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $User, $SecurePassword

foreach ($server in $servers) {
    Invoke-Command -ComputerName $Server -Credential $cred -ScriptBlock {
        Param($server, $InputFile, $scripts, $url)

        foreach ($script in $scripts) {
            echo "$script"
    } -ArgumentList "$server,"$scripts","$url"
}
5
  • this script doesnt look complete. You dont seem to be calling the varables on the server correctly Commented Sep 6, 2017 at 3:28
  • since your edit it looks like your argument list is wrong. You are declaring all your varables as string by using the " around them. Change argument list to -ArgumentList $server, $scripts, $url Also your not declaring all the arugments in order....Server, Inputfile, Scripts, URL. Currently $Scripts is = to $inputfile Commented Sep 6, 2017 at 3:34
  • The -argumentList parameter also looks to be incorrectly placed. It's currently inside the script block. Commented Sep 6, 2017 at 3:38
  • 1
    seems like issue is with the double quotes in ArgumentList.., It worked fine after removing the qoutes. Thanks for the response :) Can we define new variables inside the invoke-command script block? If I define then it was showing error as param() is not recognized. How can I define new variable inside the invoke-command block? Commented Sep 6, 2017 at 3:54
  • It's clearly not possible that the code you posted could ever have run successfully. the nested foreach loop is missing a closing curly bracket, and the first argument to the -ArgumentList parameter is missing a closing double quote. Commented Sep 6, 2017 at 9:23

1 Answer 1

2

I'm going to assume that the syntax errors in your code are just typos in your question and are not present in your actual code.

The problem you describe has nothing to do with the nested foreach loop. It's caused by the double quotes you put around the arguments you pass to the invoked scriptblock. Putting an array in double quotes mangles the array into a string with the string representations of the values from the array separated by the output field separator defined in the automatic variable $OFS (by default a space). To avoid this behavior don't put variables in double quotes when there is no need to do so.

Change the Invoke-Command statement to something like this:

Invoke-Command -ComputerName $Server -Credential $cred -ScriptBlock {
    Param($server, $scripts, $url)
    ...
} -ArgumentList $server, $scripts, $url

and the problem will disappear.

Alternatively you could use the variables from outside the scriptblock via the using scope modifier:

Invoke-Command -ComputerName $Server -Credential $cred -ScriptBlock {
    foreach ($script in $using:scripts) {
        echo "$script"
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.