Problems using 2D arrays when PS Remoting

Hi,

Apologies in advance, I’m somewhat green when it comes to scripting!

I’m hoping someone can point me in the right direction or offer some advice on the below. I’m trying to set IIS bindings on multiple web servers, remotely. The challenge is that I can’t get my head around the Using scope modifier (or I may be way off the mark!), in order to pass the right variables to New-WebBinding. So right now I’m copying CSV files to each web server in the below as a work around.

$servers = Get-Content "\\FQDN\servers.txt"
ForEach ($server in $servers) {

    New-Item -Path \\$server\c$\temp\bind -ItemType Directory
    Copy-Item -Path "\\FQDN\$server.csv" -Destination \\$server\c$\temp\bind
    
    Invoke-Command -ComputerName $server -scriptblock {
        Import-Module WebAdministration
        Remove-WebBinding
        Import-Csv c:\temp\bind\$env:computername.domain.csv | ForEach-Object {
            $site = $_.Site
            $prot = $_.Protocol
            $IP = $_.IPAddress
            $Port = $_.Port
            New-WebBinding -Name $site -IPAddress $IP -Port $Port -Protocol $prot
        }
    }
    Remove-Item \\$server\c$\temp\bind -force -recurse
}

Any help gratefully received and thanks!

James

Define your variables outside the scriptblock and pass them in as arguments. This way you don’t have to worry about copying the csv.

Define your variables outside the scriptblock and pass them in as arguments. This way you don’t have to worry about copying the csv.

Hi Jim,

Try something along these lines:

$servers = @('server1', 'server2')

$bindings = @(
    'Binding1',
    'Binding2'
)

Invoke-Command -ComputerName $servers -ScriptBlock {
    foreach ($binding in $Using:bindings)
    {
        Write-Host "Binding: $binding"
    }
}

I haven’t tested the above, but think that should get you on the right path

$splat = @{

name = 'denver'
protocol = 'tcp'
ip = '1.2.3.4'
port = 80

}



$scriptblock = {

param($splat)

New-WebBinding @splat

}



invoke-command -scriptblock $scriptblock -argumentlist $splat -computername ...




Thanks both, that’s an interesting line of thought I’d not considered.