2

Sorry noob to powersehell so forgive me if im asking a stupid question.

I am trying to dynamically create a hostfile for new users based on locally worked on websites. I am running a Get-IISSites against our staging WWW server and entering those into an array. as an example i am running a test vm with iis installed

    $Array = @()
$Websites = Get-IISSite | Select-Object name, ID

foreach($WB in $Websites)
{
    If($WB.ID -gt 1)
    {
        $Array += New-Object psobject -property @{'name' = $WB.name}
    }
}

this is the output

Test1.mav359.co.uk  
test2.mav359.co.uk  
test3.mav359.co.uk  
test4.mav360.co.uk

as you can see there are two domains, i need to achive 2 things

first i need to rename each site to test1-local.mav359.co.uk and i want to add IP1 or IP2 based on the domain to the end of the line, these will be replaced later based IPs generated and assigned to varibles generated against the local PC

Test1-local.mav359.co.uk    IP1
test2-local.mav359.co.uk    IP1
test3-local.mav359.co.uk    IP1
test4-local.mav360.co.uk    IP2

Im struggling to know how to manipluate the data i have in the array (or whether thats even the best way to go). I was thinking along the lines off doing a

foreach containing add ip1 or containg add ip2 i dont know how to/can you use a wild card to search/replace *.mav359.co.uk with *.-local.mav359.co.uk whilst retaining the original part of the line

Again knew to all this so id apppricate some help or pointing in the direction, are arrays the wrong way to go?

Cheers for any help

Mav359
  • 43
  • 2
  • Arrays are not a bad thing (although you might consider to pipe each item directly to the final results, e.g. a file or the display) but [try to avoid using the increase assignment operator (+=) to create a collection](https://stackoverflow.com/a/60708579/1701026) as it is exponentially expensive. – iRon Jan 06 '21 at 11:40

1 Answers1

1

Depending on whether you want the output as array of objects you later need to combine as string in the hosts file, you could do

$Array = Get-IISSite | ForEach-Object {
    # $_ is an automatic variable and represents one object of each iteration
    if ($_.ID -gt 1) {
        # output an object to be collected in variable `$Array`
        [PsCustomObject]@{
            Name = $_.Name -replace '^(\w+\d+)', '$1-local'
            IP   = if ($_.Name -like '*mav359*') { 'IP1' } else { 'IP2' }
        }
    }
}

$Array

Result:

Name                      IP
----                      --
Test1-local.mav359.co.uk  IP1
test2-local.mav359.co.uk  IP1
test3-local.mav359.co.uk  IP1
test4-local.mav360.co.uk  IP2

If however what you want is to get an array of formatted strings to use in a hosts file, use this:

$Array = Get-IISSite | ForEach-Object {
    if ($_.ID -gt 1) {
        '{0}    {1}' -f ($_.Name -replace '^(\w+\d+)', '$1-local'),
                        $(if ($_.Name -like '*mav359*') { 'IP1' } else { 'IP2' })
    }
}

$Array

Result:

Test1-local.mav359.co.uk    IP1
test2-local.mav359.co.uk    IP1
test3-local.mav359.co.uk    IP1
test4-local.mav360.co.uk    IP2
Theo
  • 57,719
  • 8
  • 24
  • 41
  • perfect thank you, the second example will work great. Serves another purpose cause now i can pull this apart to understand it better. Thank you again – Mav359 Jan 06 '21 at 12:59