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"
}
-argumentListparameter also looks to be incorrectly placed. It's currently inside the script block.foreachloop is missing a closing curly bracket, and the first argument to the-ArgumentListparameter is missing a closing double quote.