# Option 1\n$Company = Read-Host -Prompt 'Please select the company'\n$Address = Read-Host -Prompt 'Enter your address'\n$Postal = Read-Host -Prompt 'Enter your post'\n# Overwrite $Company with the company name\nswitch ($Company) {\n '1' {$Company = \"Company1\"}\n '2' {$Company = \"Company2\"}\n default {$Company = \"ACME Inc.\"} # Just kidding ;)\n}\n\n# Option 2\n# Define company defaults\n$CompanyDefaults['Company1'] = @{Name = 'Company 1'; Address = \"Somewhere\"; Postal = \"99358\"}\n$CompanyDefaults['Company2'] = @{Name = 'Company 2'; Address = \"Somewhere else\"; Postal = \"99359\"}\n$CompanyDefaults['ACME Inc.'] = @{Name = 'ACME Inc.'; Address = \"42\"; Postal = \"42\"}\n\n$Company = Read-Host -Prompt 'Please select the company'\n$Address = Read-Host -Prompt 'Enter your address'\n$Postal = Read-Host -Prompt 'Enter your post'\n# Overwrite $Company with the company name\nswitch ($Company) {\n '1' {$Company = $CompanyDefaults['Company1'].Name}\n '2' {$Company = $CompanyDefaults['Company2'].Name}\n default {$Company = $CompanyDefaults['ACME Inc.'].Name}\n}\n# Check $Address and $Postal are not empty\nif ($Address.Length -eq 0) {$Address = $CompanyDefaults|Where-Object Name -eq $Company|Select-Object -ExpandProperty Address}\nif ($Postal.Length -eq 0) {$Postal = $CompanyDefaults|Where-Object Name -eq $Company|Select-Object -ExpandProperty Postal}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-07-19T11:24:56.000Z","url":"https://community.spiceworks.com/t/powershell-switch-collecting-multiple-parameters/662862/4","author":{"@type":"Person","name":"tulioarends","url":"https://community.spiceworks.com/u/tulioarends"}},"suggestedAnswer":[{"@type":"Answer","text":"Hi,<\/p>\n
So Ive got my powershell switch working fine but what I was wondering is - can I collect multiple parameters from one switch result? So - below I have my switch<\/p>\n
$Company = Read-Host \" Please select the company\" \nswitch ($Company) \n{ \n‘1’ { $Company = “Company1” \n} \n‘2’ { $Company = “Company2” \n} \n}<\/p>\n
But if for example someone selects 1 I want to be able to collect additional parameters like $Address $ Postcode<\/p>\n
Is this possible?<\/p>\n
Thanks<\/p>","upvoteCount":2,"datePublished":"2018-07-19T09:31:46.000Z","url":"https://community.spiceworks.com/t/powershell-switch-collecting-multiple-parameters/662862/1","author":{"@type":"Person","name":"shabbaranksshabbaranks5809","url":"https://community.spiceworks.com/u/shabbaranksshabbaranks5809"}},{"@type":"Answer","text":"
Add this in the case<\/p>\n
$Address = Read-Host -Prompt ‘Enter your address’<\/p>\n
$Postal = Read-Host -Prompt ‘Enter your post’<\/p>","upvoteCount":0,"datePublished":"2018-07-19T09:56:43.000Z","url":"https://community.spiceworks.com/t/powershell-switch-collecting-multiple-parameters/662862/2","author":{"@type":"Person","name":"Scott-Gutauckis","url":"https://community.spiceworks.com/u/Scott-Gutauckis"}},{"@type":"Answer","text":"
Thanks - if thats the case, is there a way to use an if statement so that IF user selects 1 then the switch parameter for the company is used and the necessary post codes etc are used?<\/p>","upvoteCount":0,"datePublished":"2018-07-19T10:27:57.000Z","url":"https://community.spiceworks.com/t/powershell-switch-collecting-multiple-parameters/662862/3","author":{"@type":"Person","name":"shabbaranksshabbaranks5809","url":"https://community.spiceworks.com/u/shabbaranksshabbaranks5809"}},{"@type":"Answer","text":"
Thanks I used a IF statement rather than a switch statement to get the details into the user account which works fine simular to your option 2 example above. Thanks<\/p>","upvoteCount":0,"datePublished":"2018-07-19T11:53:54.000Z","url":"https://community.spiceworks.com/t/powershell-switch-collecting-multiple-parameters/662862/5","author":{"@type":"Person","name":"shabbaranksshabbaranks5809","url":"https://community.spiceworks.com/u/shabbaranksshabbaranks5809"}},{"@type":"Answer","text":"
If I wanted to add a user to multiple groups - is the only way to reference a text\\csv file with those group names in? Or is there a way to achieve in this with powershell?<\/p>\n
Thanks<\/p>","upvoteCount":0,"datePublished":"2018-07-19T12:48:24.000Z","url":"https://community.spiceworks.com/t/powershell-switch-collecting-multiple-parameters/662862/6","author":{"@type":"Person","name":"shabbaranksshabbaranks5809","url":"https://community.spiceworks.com/u/shabbaranksshabbaranks5809"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Shabbaranks.:<\/div>\n
\nIf I wanted to add a user to multiple groups - is the only way to reference a text\\csv file with those group names in? Or is there a way to achieve in this with powershell?<\/p>\n
Thanks<\/p>\n<\/blockquote>\n<\/aside>\n
This is a different question and should be asked in a different thread.<\/p>","upvoteCount":0,"datePublished":"2018-07-19T12:51:54.000Z","url":"https://community.spiceworks.com/t/powershell-switch-collecting-multiple-parameters/662862/7","author":{"@type":"Person","name":"tulioarends","url":"https://community.spiceworks.com/u/tulioarends"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Shabbaranks.:<\/div>\n
\nIf I wanted to add a user to multiple groups - is the only way to reference a text\\csv file with those group names in? Or is there a way to achieve in this with powershell?<\/p>\n
Thanks<\/p>\n<\/blockquote>\n<\/aside>\n
Well powershell has to know the names, so importing it from some source is required.<\/p>\n
Options are manual input, reading from a file (csv,txt,json,xml etc), or having another cmdlet produce the list (e.g. get-aduser $username -properies memberof) so all the goups a user is memberof will be added.<\/p>","upvoteCount":0,"datePublished":"2018-07-19T13:31:12.000Z","url":"https://community.spiceworks.com/t/powershell-switch-collecting-multiple-parameters/662862/8","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}}]}}
Hi,
So Ive got my powershell switch working fine but what I was wondering is - can I collect multiple parameters from one switch result? So - below I have my switch
$Company = Read-Host " Please select the company"
switch ($Company)
{
‘1’ { $Company = “Company1”
}
‘2’ { $Company = “Company2”
}
}
But if for example someone selects 1 I want to be able to collect additional parameters like $Address $ Postcode
Is this possible?
Thanks
2 Spice ups
Add this in the case
$Address = Read-Host -Prompt ‘Enter your address’
$Postal = Read-Host -Prompt ‘Enter your post’
Thanks - if thats the case, is there a way to use an if statement so that IF user selects 1 then the switch parameter for the company is used and the necessary post codes etc are used?
Please describe what it it you are actually trying to do.
Using read-host to get parameters is not really a good idea, you’re better off using -parameters and the established commandlet syntax. If this is a user facing script I recommend you have a look at How to add a Graphical User Interface to your PowerShell Functions using the .Net SystemWindowsForm class .
Having said that, try the following:
# Option 1
$Company = Read-Host -Prompt 'Please select the company'
$Address = Read-Host -Prompt 'Enter your address'
$Postal = Read-Host -Prompt 'Enter your post'
# Overwrite $Company with the company name
switch ($Company) {
'1' {$Company = "Company1"}
'2' {$Company = "Company2"}
default {$Company = "ACME Inc."} # Just kidding ;)
}
# Option 2
# Define company defaults
$CompanyDefaults['Company1'] = @{Name = 'Company 1'; Address = "Somewhere"; Postal = "99358"}
$CompanyDefaults['Company2'] = @{Name = 'Company 2'; Address = "Somewhere else"; Postal = "99359"}
$CompanyDefaults['ACME Inc.'] = @{Name = 'ACME Inc.'; Address = "42"; Postal = "42"}
$Company = Read-Host -Prompt 'Please select the company'
$Address = Read-Host -Prompt 'Enter your address'
$Postal = Read-Host -Prompt 'Enter your post'
# Overwrite $Company with the company name
switch ($Company) {
'1' {$Company = $CompanyDefaults['Company1'].Name}
'2' {$Company = $CompanyDefaults['Company2'].Name}
default {$Company = $CompanyDefaults['ACME Inc.'].Name}
}
# Check $Address and $Postal are not empty
if ($Address.Length -eq 0) {$Address = $CompanyDefaults|Where-Object Name -eq $Company|Select-Object -ExpandProperty Address}
if ($Postal.Length -eq 0) {$Postal = $CompanyDefaults|Where-Object Name -eq $Company|Select-Object -ExpandProperty Postal}
Thanks I used a IF statement rather than a switch statement to get the details into the user account which works fine simular to your option 2 example above. Thanks
If I wanted to add a user to multiple groups - is the only way to reference a text\csv file with those group names in? Or is there a way to achieve in this with powershell?
Thanks
This is a different question and should be asked in a different thread.
Neally
(Neally)
July 19, 2018, 1:31pm
8
Well powershell has to know the names, so importing it from some source is required.
Options are manual input, reading from a file (csv,txt,json,xml etc), or having another cmdlet produce the list (e.g. get-aduser $username -properies memberof) so all the goups a user is memberof will be added.