Jump to content

Powerhsell - runn command based on the option?


Recommended Posts

Posted

Hi,

 

I would like it so when you run a power shell script that it will present 4 options to the end user. Depending on what option the user picks it will then run 1 command. all 4 options will have different commands.

 

Does that make sense please?

 

Thanks

Posted

Makes sense, and simple enough really. Simple version below.

 

Write-Host "Press 1 for option 1"

Write-Host "Press 2 for option 2"

 

Read-Host $command

 

switch ($command) {

1 {do stuff for command 1}

2 {do stuff for command 2}

default {Write-Host "Didn't recognise the command"}

}

 

If you want to force them to choose an option you can stick the whole thing in a while loop, and use the default case to set a variable so that it'll loop again.

  • Thanks 1
Posted

When we add a computer to AD we have a couple of things to do. Like create it in SCCM, add mac address and add a group. So making a little powershell script to speed the process up.

 

The options will be

 

Staff Desktop

Staff Laptop

Student Desktop

Student Laptop

 

Thanks

  • Thanks 1
  • 3 weeks later...
Posted

hi @jamesb there is a slight error i notice it should be something like This:

 

Write-Host "Press 1 for option 1"

Write-Host "Press 2 for option 2"

 

$command = Read-Host "Please Enter An Option"

 

switch ($command) {

1 {do stuff for command 1}

2 {do stuff for command 2}

default {Write-Host "Didn't recognise the command"}

}

 

Apart from that all is good and it seems to work :)

 

Thanks

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...