Author Posts

January 1, 2012 at 12:00 am

by mauroj at 2012-12-14 14:44:19

I'm new to PowerShell and already facing a big challenge.
The goal is to read a csv file. Each row of the file will be a string with biding parameters like:

"Website1","116.167.74.172:443:www.xyz.com 116.167.74.174:443:www.xyz.com"
"Website2",":80:www.xyz.com 216.157.74.172:80:"
"Website3","216.157.75.155:443: 216.157.75.163:443:"
"Website4",":80:"

The command to be used is:
New-WebBinding -Name "Default Web Site" -IPAddress "*" -Port 80 -HostHeader TestSite

The problem is: how do I read the one string and break it into 3 parameters: IP, Port and HostHeader, even when I don't have all the 3 parameters specified?

by ArtB0514 at 2012-12-17 09:10:36

So, what you're saying is that the binding information in the CSV file consists of one or more bindings in the form :: separated by a space character, correct?

If so, then you can use the string SPLIT method the break things apart where BINDINGS is the name of the csv field. Something like this should work:

$_.Bindings.Split(' ') | foreach {
$Site = $_.Split(':')
$IP = $Site[0]
If ([string]::IsNullOrEmpty($IP)) {$IP = '*'}
$Port = $Site[1]
If ([string]::IsNullOrEmpty($Port)) {$Port = '80'}
$Header = $Site[2]
If ([string]::IsNullOrEmpty($Header)) {$SiteName = "Default Web Site"} Else {$SiteName = ($Header -split '.',0,'SimpleMatch')[0]}
New-WebBinding -Name $SiteName -IPAddress $IP -Port $Port -HostHeader $Header

by mauroj at 2012-12-19 13:00:54

@artb0514

Thank you very much. You were right on the money.
The one fine tune needed was to add [StringSplitOptions]'RemoveEmptyEntries' to the split(' ') since between binding setting it could be one or more spaces.

In case you ask: the binding setting in this format is the result of spyxml generated by reading the metadata.xml from IIS 6.0, and my objective was to read this file and create an IIS 7.5 infrastructurefrom this file.

Thank you for all your help