Problems using 2D arrays when PS Remoting

Welcome Forums General PowerShell Q&A Problems using 2D arrays when PS Remoting

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
2 years, 2 months ago.

  • Author
    Posts
  • #56035

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 21
    Rank: Member

    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

    Participant
    Points: 21
    Rank: Member

    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

    Participant
    Points: 33
    Rank: Member

    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

    Participant
    Points: 21
    Rank: Member
    
    $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

    Participant
    Points: 0
    Rank: Member

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

The topic ‘Problems using 2D arrays when PS Remoting’ is closed to new replies.