0

I have the script below that lets me switch between the different elements and runs the functions in them one by one.

But what I need to do now is make it so I can select multiple ones and have them run and pause between them to verifiy if things were loaded correctly. So that way I don't run into the issue having to re rerun the full script again and redo the same one over.

Can anybody show me how to do this? I am lost as to how to get this completed and working properly.

write-host "Sets up location you want to run staging"
$ElementDistro = Read-Host -Prompt "Which Element do you want to run? (TV30/TV30BP/TV30LM/TV30PV/LT101/XR2/MU11/SAP)"
while ($ElementDistro -notmatch "^(TV30|TV30BP|TV30LM|TV30PV|LT101|XR2|MU11|SAP)$")
{
    write-host "you have enterd an error" -ForegroundColor Red
    write-host "You must type TV30 or TV30BP or TV30LM or TV30PV or LT101 or XR2 or MU11 or SAP"
    write-host "you typed $ElementDistro"
    write-host "set location you want to run staging"
    $ElementDistro = Read-Host -Prompt "Which Element do you want to run? (TV30/TV30BP/TV30LM/TV30PV/LT101/XR2/MU11/SAP)"
}

switch ($ElementDistro)
{
    'TV30'
    {
        # Do TV30 Stuff
        write-host "you have entered TC TV30"
        $passwd = convertto-securestring -AsPlainText -Force -String ''
        $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
        $session = enter-pssession -computername '' -credential $cred
        $source = Select-TC
        $destination = 'Desktop'
        "Calling Copy-Item with parameters source: '$source', destination: '$destination'."
        Copy-Item -Path $source -Destination $destination
        exit-pssession
        break
    }

    'TV30BP'
    {
        # Do TV30BP Stuff
        $passwd = convertto-securestring -AsPlainText -Force -String ''
        $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
        $session = enter-pssession -computername '' -credential $cred
        $source = Select-TC
        $destination = 'Desktop'
        "Calling Copy-Item with parameters source: '$source', destination: '$destination'."
        Copy-Item -Path $source -Destination $destination
        # exit-pssession
        break
    }

    'TV30LM'
    {
        # Do TV30LM stuff
        $passwd = convertto-securestring -AsPlainText -Force -String ''
        $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
        $session = enter-pssession -computername '' -credential $cred
        $source = Select-TC
        $destination = 'Desktop'
        "Calling Copy-Item with parameters source: '$source', destination: '$destination'."
        Copy-Item -Path $source -Destination $destination
        exit-pssession
        break
    }

    'TV30PV'
    {
        # Do TV30PV stuff
        $passwd = convertto-securestring -AsPlainText -Force -String ''
        $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
        $session = enter-pssession -computername '' -credential $cred
        $source = Select-TC
        $destination = 'Desktop'
        "Calling Copy-Item with parameters source: '$source', destination: '$destination'."
        Copy-Item -Path $source -Destination $destination
        exit-pssession
        break
    }

    'LT101'
    {
        # Do LT101 stuff
        $passwd = convertto-securestring -AsPlainText -Force -String ''
        $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
        $session = enter-pssession -computername '' -credential $cred
        break
    }

    'XR2'
    {
        # Do XR2 stuff
        $passwd = convertto-securestring -AsPlainText -Force -String ''
        $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
        $session = enter-pssession -computername '' -credential $cred
        break
    }

    'MU11'
    {
        # Do TF10 stuff
        $passwd = convertto-securestring -AsPlainText -Force -String ''
        $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
        $session = enter-pssession -computername '' -credential $cred
        break
    }

    'SAP'
    {
        # Do SAP stuff
        $passwd = convertto-securestring -AsPlainText -Force -String ''
        $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
        $session = enter-pssession -computername '' -credential $cred
        break
    }
}
break
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
bgrif
  • 253
  • 1
  • 4
  • 14
  • You could wrap that whole bit inside a `Do{}While(!($ElementDistro -ieq "q"))` and it will just loop back to the menu until you enter Q. Then add Q to your current options, as a Quit option. – TheMadTechnician Jul 02 '14 at 15:01
  • That might work out for what i am trying to accomplish. I am still looking for how to make it so i can select multiple at one time and have it run. – bgrif Jul 02 '14 at 15:13
  • FYI your last closing brace doesn't match anything. Also, you had missing opening quotes before `Desktop` in the TV30BP and TV30PV sections. – Adi Inbar Jul 02 '14 at 15:49
  • Thank you for the info. I didn't even notice I left that stuff out. – bgrif Jul 02 '14 at 15:55
  • Use a good code editor with syntax highlighting. The missing quotes jumped out at me the moment I added syntax highlighting. Or if you don't have a good code editor, you can use the PowerShell ISE. ;) – Adi Inbar Jul 02 '14 at 16:15
  • Thank you i will keep that in mind when testing my scripts. – bgrif Jul 02 '14 at 16:18

2 Answers2

1

The quick answer is that Powershell's switch statement accepts an array for input. If you leave out the break statement at the end of each switch case it will execute each case that is a match. Enter your choices as a comma-separated list and put them into an array using the split statement.

Each choice in you $Choices array will be executed. If you put a Pause statement where your break statements are you can pause at the completion of each step.

$Choices = @('TV30','MU11')
switch ($Choices)
{
'TV30' {some code}
'TV30BP' {some code}
'MU11' {some code}
}
WiiBopp
  • 2,750
  • 1
  • 14
  • 9
  • That works out great. But I am still using the while statement above it to bring a window up and have me enter in the choice I want. So would I need to change that part to read choices to have it fill the switch statment. – bgrif Jul 02 '14 at 16:02
  • Thank you for the help. You actually helped me with another script I am working on. – bgrif Jul 02 '14 at 16:08
1

If you got at least V3, you can use Out-GridView with -OutPutMode Multiple as a menu to select multiple items from:

$Menu = 'TV30','TV30BP','TV30LM','TV30PV','LT101','XR2','MU11','SAP','ALL'

$Choices = $Menu | Out-GridView -OutputMode Multiple -Title 'Select Locations you want to run staging, and click OK.'

Switch ($Choices)
{ 
  .....
mjolinor
  • 66,130
  • 7
  • 114
  • 135