Author Posts

August 16, 2016 at 11:00 am

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

August 16, 2016 at 11:18 am

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.

August 16, 2016 at 11:32 am

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

August 16, 2016 at 12:16 pm

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

August 16, 2016 at 12:32 pm

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

August 16, 2016 at 1:10 pm

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)

August 16, 2016 at 1:29 pm

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

August 16, 2016 at 1:56 pm

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 1 year, 12 months ago by  Daniel Krebs.

August 16, 2016 at 1:57 pm

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.

August 16, 2016 at 2:07 pm

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,

August 16, 2016 at 2:37 pm

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

August 17, 2016 at 6:15 am

Sorry,

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

August 17, 2016 at 8:47 am

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.

August 17, 2016 at 9:41 am

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

August 17, 2016 at 9:53 am

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

August 17, 2016 at 9:55 am

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.

August 17, 2016 at 9:58 am

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.

August 17, 2016 at 10:40 am

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.

August 17, 2016 at 10:41 am

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

August 17, 2016 at 1:30 pm

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,