0

Long story short, creating a migration script for IIS. The script digs into IIS, finds out what's there and creates a new PS1 based upon that.

I am stuck on a generic PowerShell function, I need to create a unique variable for each item found within a foreach. For example

$servername = $env:computername # Using alias
Get-Website |
    select name, id, state, physicalpath, applicationpool,
        @{n="Bindings";e={($_.bindings | select -expa collection) -join ';'}} ,
        @{n="LogFile";e={ $_.logfile | select -expa directory}}, 
        @{n="attributes";e={($_.attributes | % {$_.name + "=" + $_.value}) -join ';'}} |
    Export-Csv -NoTypeInformation -Path D:\$servername-iis-sites.csv

$csv =  import-csv -Path D:\$servername-iis-sites.csv

foreach ($line in $csv) {
    $Name = $line.Name
    $id = $line.id # Not used, maybe one day it will be loved.
    $Physicalpath  = $line.physicalpath
    $port = $line.Bindings.split(":")[1]
    $apppool = $line.applicationpool
    $header = $line.Bindings.split(":")[-1] -replace 'sslflags=0', ''

For each $header found, I need to create a unique variable. This I can then pass into another function later in the script. It needs to be purely as the format above, just the header value, nothing else. But the variable needs to be entirely unique.

Is this possible?

My colleague has done something like

$i = 0
foreach ($dog in $richardshouse) do
{
    $i+1
    $var$i = $dog
    $i++
}

But this as a limitation.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Richard Dakin
  • 413
  • 4
  • 16

0 Answers0