Display information at the start of a script

This topic contains 19 replies, has 3 voices, and was last updated by Profile photo of Marc Dryburgh Marc Dryburgh 3 months, 2 weeks ago.

  • Author
    Posts
  • #50575
    Profile photo of Marc Dryburgh
    Marc Dryburgh
    Participant

    Hi all, (new user here)

    I am trying to create a script that would Speed up the process of enabling users with Skype for Business. I would like the script to pose a series of "questions" using I think Mandatory Parameters. Then the Admin just has to type in the Name of the user, the diferrent policys and so on. My main Problem at the moment is that I would like a set of text to Pop on Screen when running the script that lists the available policys. This can be manually typed into the script as we don't have to many different policys and it is just to future proof to help future admins with the names. Is there a way to do this ? I have googled around but just can't seem to find what I am looking for or I don't know how to look 🙂

    Many thanks in advance

  • #50579
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    As you've probably discovered you can't output any message before the PowerShell engine asks for values for the mandatory parameters.

    However, you can use the ValidateSet attribute.

    [Parameter(Mandatory)]
    [ValidateSet('UserPolicy','ComputerPolicy')]
    [System.String]
    $PolicyName

    The neat thing about the attribute is that the values will show up in a Get-Help output.

    Get-Help .\PolicyScript.ps1 -Syntax

    Additionally you could add a HelpMessage attribute as well.

    I hope that helps.

    • #50581
      Profile photo of Marc Dryburgh
      Marc Dryburgh
      Participant

      Hi

      First of all thank you for your time,

      the ValidateSet Attribute would achieve exactly what I want I was just unaware that was possible. My knowledge of available Features is growing slowly everyday. (Day 4 of working with powershell)

      Thanks

  • #50583
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Hi Marc,

    You're very welcome. Which material are you using to learn PowerShell? May be we can suggest additional material for you to study over the coming days and weeks.

    Keep up the great work and welcome to the PowerShell community.

    Cheers
    Daniel

    • #50585
      Profile photo of Marc Dryburgh
      Marc Dryburgh
      Participant

      Hi Daniel,

      I have a copy of Windows Powershell Best Practices by Ed Wilson sitting next to me,I and have managed to watch the whole Jump Start Videos on MVA (with J.Snover and J.helmick) which is awesome by the way. I would like to pick up Don's book at some Point when I have the Chance.

      I am currently having a Problem getting the ValidateSet to work;

      Param(
      [parameter(

      mandatory=$true,
      HelpMessage="Please use Pool Name",
      Position=1)]
      [ValidateSet("gimmi","stuff")]
      [string[]]$pool,

      I could post the whole script butI like to try and figure stuff out rather than just be told 🙂

      Kind regards,

      Marc

  • #50590
    Profile photo of Paul Frankovich
    Paul Frankovich
    Participant

    Are you getting an error? Is this the entirety of the Param block? What you have here would fail if it is the entire block since it has the comma and no close parenthesis. I ran it through ISE as it is below with no issue.

    Param(
     [parameter(mandatory=$true,
     HelpMessage="Please use Pool Name",
     Position=1)]
     [ValidateSet("gimmi","stuff")]
     [string[]]$pool)
    • #50592
      Profile photo of Marc Dryburgh
      Marc Dryburgh
      Participant

      No that is not all 🙂 I was just wondering if I was putting it in the right place at all within the Param Block. I would post everything that I have but to be honest I'm a little unsure of myself at the Moment. I only started "working" with PS on Saturday

  • #50598
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Multiple parameter definitions in a param() block need to be separated by a comma.

    Example 1 (Simple without additional parameter attributes like Parameter, ValidateSet, String, etc.):

    param (
        $Pool,
        $Name
    )
    

    Example 2:

    param (
        [Parameter(
            Mandatory = $true,
            HelpMessage = "Please use Pool Name",
            Position = 1
        )]
        [ValidateSet("gimmi","stuff")]
        [String[]] $Pool, # Multiple parameter definitions need to be separated by a comma
    
        [Parameter(Mandatory=$true)]
        [String] $Name
    )
    
    $Pool
    $Name
    
    • This reply was modified 3 months, 2 weeks ago by Profile photo of Daniel Krebs Daniel Krebs.
  • #50601
    Profile photo of Paul Frankovich
    Paul Frankovich
    Participant

    A benefit to posting it all is it allows us to see where you are going with it and gives us a chance to spot other inconspicuous things they may be the actual error. Also, now is a good time to get into good habits and we can (mostly) assist with that as well.

  • #50605
    Profile photo of Marc Dryburgh
    Marc Dryburgh
    Participant

    function Enable-CsuserNAW{

    [cmdletbinding()]

    Param(

    [parameter(

    mandatory=$true,
    HelpMessage="Please use SAM name",
    Position=1)]
    [string]$SAMname,

    [parameter(

    mandatory=$true,
    HelpMessage="Please use Lineuri: tel:12341234ext=123(beispiel)",
    Position=4)]
    [string]$Linuri,

    [parameter(

    mandatory=$true,
    HelpMessage="Please use Dialplan: Dialplan ohne Amtsholung",
    Position=5)]
    [string]$Dial,

    [parameter(

    mandatory=$true,
    HelpMessage="Please use Voice Policy: Default Policy",
    Position=6)]
    [string]$Voice

    )

    Enable-csuser -identity $SAMname.Identity -registrarpool nawlync002.netatwork.de -sipaddresstype Emailaddress

    # Pause for 30 seconds for AD Replication
    write-host -foregroundcolor Green "Pausing for 2 Days to allow for AD Replication"

    Start-Sleep -s 30

    Set-CsUser -Identity $SAMname.Identity -enterprisevoiceenabled $True -lineuri $linuri

    Grant-CsDialPlan -Identity $SAMname.Identity -PolicyName $dial.DialPlan

    Grant-CsVoicePolicy -Identity $SAMname.Identity -PolicyName $voice.VoicePolicy

    Gran-CsClientPolicy -Identity $SAMname.Identity -policyname PaderbornClientPolicy

    }

    This is without Validate,

  • #50607
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    The param() block works fine on my machine. What happened to the parameters at position 2 and 3?

    • #50720
      Profile photo of Marc Dryburgh
      Marc Dryburgh
      Participant

      Sorry,

      I made changes to the script and forgot to Change the Postion numbers,

  • #50732
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    No problem. Have you had the chance to get your function parameter validation working?

    If not, please post the error message and/or screen output for us to help further.

    • #50738
      Profile photo of Marc Dryburgh
      Marc Dryburgh
      Participant

      Hi,

      I will be trying again this afternoon, I am currently on-site at a customer. I will let you know how it goes,

      Kind Regards,

      Marc

  • #50747
    Profile photo of Marc Dryburgh
    Marc Dryburgh
    Participant
    PS C:\Users\mdryburgh\Documents> Enable-CsuserNAW
    Cmdlet Enable-CsuserNAW an der Befehlspipelineposition 1
    Geben Sie Werte für die folgenden Parameter an:
    (Geben Sie zum Aufruf der Hilfe !? ein.)
    SAMname: TJones
    Linuri: 123456
    Dial: !?
    Please use Dialplan: Dialplan ohne Amtsholung or Default Policy
    Dial: Bannana
    Voice: Teddybear
    Enable-CsuserNAW : Das Argument für den Parameter "Dial" kann nicht überprüft werden. Das Argument "Bannana" gehört nicht 
    zu dem vom ValidateSet-Attribut angegebenen Satz "Default Policy;Dialplan ohne Amtsholung". Geben Sie ein Argument an, das 
    in dem Satz enthalten ist, und führen Sie dann den Befehl erneut aus.
    In Zeile:1 Zeichen:1
    + Enable-CsuserNAW
    + ~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Enable-CsuserNAW], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Enable-CsuserNAW
  • #50749
    Profile photo of Marc Dryburgh
    Marc Dryburgh
    Participant

    Ok, everyone has gone for lunch 🙂

    I think I have it working as when I am entering a value not included within the ValidateSet Argument I get and error that Shows what should be entered. This coupled with the help Messages makes it easy to use and see what Options are available.

  • #50751
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Looks like your parameter validation is working because you've provided "Banana" for the Dial parameter but only "Default Policy" and "Dialplan ohne Amtsholung" are allowed.

    • #50763
      Profile photo of Marc Dryburgh
      Marc Dryburgh
      Participant

      My browser did not refresh properly, thank you for the confirmation Daniel.

  • #50761
    Profile photo of Marc Dryburgh
    Marc Dryburgh
    Participant

    By the Looks of Things it is working. When I enter an incorrect value it Returns an error containing the allowed Arguments. That coupled with the Helpmessage Attribute should make it easier.

  • #50805
    Profile photo of Marc Dryburgh
    Marc Dryburgh
    Participant

    Hey guys,

    I have just tested the script and it functions exactly how I want it to, a 10 Minute process can now be completed in 2 using Powershell !! 🙂

    I just Need to tidy it up a bit and work out how to add a fixed area Code to the $lineuri Parameter to save a few button presses and it will be done.

    
        
    
    function Enable-CsuserNAW{
    
        
        [cmdletbinding()]
        
        Param(
                
            [parameter(
                
                mandatory=$true,
                HelpMessage="Please use Full name", 
                Position=1)]           
                [string]$SAMname, 
                            
                            
            [parameter(
                
                mandatory=$true,
                HelpMessage="Please use Format tel:12341234ext=123(beispiel)",
                Position=2)]
                [string]$Linuri,
            
            [parameter(
              
                mandatory=$true,
                HelpMessage="Please use Dialplan: Dialplan ohne Amtsholung or Default Policy",
                Position=3)]
                [ValidateSet("Default Policy","Dialplan ohne Amtsholung")]
                [string]$Dialplan
          
    )
    
    
    
    Enable-csuser -identity $SAMname -registrarpool nawlync002.netatwork.de -sipaddresstype Emailaddress 
    
    # Pause for 10 seconds for AD Replication
    write-host -foregroundcolor Green "Pausing for 10 Seconds to allow for AD Replication"
    
    Start-Sleep -s 10
     
    
    Set-CsUser -Identity $SAMname -enterprisevoiceenabled $True -lineuri $linuri
    
    
    Grant-CsVoicePolicy -Identity $SAMname -PolicyName "default policy"
    
    Grant-CsClientPolicy -Identity $SAMname -policyname PaderbornClientPolicy
    
    Grant-CsExternalAccessPolicy -Identity $SAMname -PolicyName "Allow Federation+Public+Outside Access"
    
    Grant-CsDialPlan -Identity $SAMname -PolicyName $dialplan
    }

    Thanks for all the help,

    Kind regards,

You must be logged in to reply to this topic.