Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

arrays - Dynamically Create Host File

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...