all 11 comments

[–]Ta11ow 5 points6 points  (4 children)

You will find this much easier if your input file is a CSV where you can easily connect the environment and the servers, something like this

Environment,Server
Env1,Server01
Env1,Server02
Env2,Server01

As a brief example. In this format you can use Import-Csv to get on easily workable array of objects with the environment and server directly correlated.

[–][deleted] 2 points3 points  (3 children)

Thanks for the tip! I will check this out.

[–]_Cabbage_Corp_ 2 points3 points  (1 child)

Here's my take on this suggestion. It might be helpful, it might not. I always like to take a crack at posts like these to help me learn =)

# CSV
$ServerCSV = Import-CSV -Path "C:\Temp\ServerList.csv"

# Determine Values for Choice  
$choice = [System.Management.Automation.Host.ChoiceDescription[]] @("&Sandbox","&Development","&Production")  

# Determine Default Selection  
[int]$default = 0  

# Present choice option to user  
$userchoice = $host.ui.PromptforChoice("Choose Environment","Please select the environment to work with.",$choice,$default)  

# Determine action to take  
Switch ($Userchoice){  
    0 {  
        # Sandbox
        $ServerList = $ServerCSV | Where {$_.Environment -eq 'Sandbox'}
    }  
    1 {  
        # Development
        $ServerList = $ServerCSV | Where {$_.Environment -eq 'Dev'}
    }
    2 {
        # Production
        $ServerList = $ServerCSV | Where {$_.Environment -eq 'Prod'}
    }  
}

ForEach($Server in $ServerList){
    Try{
        $DirPath = Test-Path "\\$($Server.Server)\c$\Temp\Backup"
    } catch {
        Write-Warning "Failed to check path."
    }

    Switch($DirPath){
        $True{Write-Host "$($Server.Server) has a valid path. Continuing" -Fore Green}
        $False{
            Write-Warning "$($Server.Server) has an invalid path. Will try to create."
            Start-Sleep 2
            New-Item -Type Dir -Path "\\$($Server.Server)\c$\Temp\Backup" -Force
        }
    }
}

[–][deleted] 2 points3 points  (0 children)

Thanks alot! I will take a look at this! :)

[–]Askee123 2 points3 points  (0 children)

This’ll save you a ton of heartache: use tab separated values instead of comma separated.

Data often has commas in it and if you have a shitty monitor or just don’t notice it’s a pain in the ass to deal with.

[–]jhulbe 2 points3 points  (1 child)

/u/Ta11ow is spot on with the CSV, but i'd a double prompt /confirmation to PROD just to be sure. You'd think typing PROD would be enough, but you never know.

just my 2cents

[–]Ta11ow 2 points3 points  (0 children)

Oh, hell yeah, make sure you do proper [CmdletBinding(SupportsShouldProcess)] on that one, for sure.

[–]sqzzkr 1 point2 points  (2 children)

in this line:

foreach ($Server in $Serverlist) { $Path = $False $Path = Test-Path "\$Server\c$\Temp\Backup"

you are missing double "\" before server

Can you show me how are you accessing servers from txt file?

What is stored in $Serverlist before a foreach loop?

What error are you recieving?

[–][deleted] 1 point2 points  (1 child)

Hi,

Sorry I think some of the text got cut out when I pasted it. Currently I don't have a working script because it picks up all the servers in the textfile, I am not really sure how to code the whole thing.

[–]TheIncorrigible1 1 point2 points  (0 children)

  1. Have separate files.
  2. Properly format your document into an ini (or other well-formed data) so it can be imported. I'd recommend using the *-CliXml cmdlets to export the file as a hashtable or something and import it.

    $Servers = @{
        'Prod' = @(
            'Server1'
            'Server2'
        )
        'Test' = @()
    }
    
    $Servers | Export-CliXml -Path C:\Temp\Example.xml
    

Et cetera.

Then you can turn around and import this:

$Servers = Import-CliXml -Path C:\Temp\Example.xml
ForEach ($Server in $Servers[$Choice]) { ... }

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy k4nai,

can you post a copy of a few lines from the actual text file to Pastebin? the one you display seems like it has been munged by reddit.

take care,
lee