Problems using 2D arrays when PS Remoting

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Jim Canfer Jim Canfer 1 month, 1 week ago.

  • Author
    Posts
  • #56035
    Profile photo of Jim Canfer
    Jim Canfer
    Participant

    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

  • #56036
    Profile photo of Dan Potter
    Dan Potter
    Participant

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

  • #56038
    Profile photo of Dan Potter
    Dan Potter
    Participant

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

  • #56042
    Profile photo of Christian Sandfeld
    Christian Sandfeld
    Participant

    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

  • #56044
    Profile photo of Dan Potter
    Dan Potter
    Participant
    
    $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 ...
    
    
    
    
    
  • #56087
    Profile photo of Jim Canfer
    Jim Canfer
    Participant

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

You must be logged in to reply to this topic.