[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Powershell script to prompt for input

Posted on 2011-02-16
4
Medium Priority
?
1,073 Views
Last Modified: 2012-05-11
what would I need for  the script to prompt me for a workgroup name.

param(
              [Parameter(Position=0, Mandatory=$true,ParameterSetName="Workgroup")]
              [string]$Workgroup,
              
              [Switch]$AutoReboot
          )
0
Comment
Question by:chshrmc
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
Dale Harris earned 1000 total points
ID: 34910663
param(
              [Parameter(Position=0, Mandatory=$true,ParameterSetName="Workgroup")]
              [string]$Workgroup,
               
              [Switch]$AutoReboot
          )
if ($Workgroup -eq $null){$Workgroup = read-host "Enter in the workgroup name"}

HTH,

Dale Harris
0
 
LVL 71

Assisted Solution

by:Chris Dent
Chris Dent earned 1000 total points
ID: 34911040

me again :)

If you set Mandatory = $True it will never permit a $Null value for $Workgroup, and therefore never prompt.

You'd have to rip out Mandatory as well, and, since you're casting to a string, test for "" instead of $Null :)

Chris
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 34911123
So what you're saying Chris, is that I should just leave the answering to you? :)

My thought on the $null was that the variable was never used, but since it's cast in the parameters, it makes sense that past that point, it will need to be "".

Thanks again for the help.

It never ceases to amaze me how much I don't know about Powershell.

Dale
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 34911153
Never :) I just like joining in :)

Chris
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question