Solved

Powershell - create module - how to specify empty argument

Posted on 2011-03-25
11
598 Views
Last Modified: 2012-06-21
Hello,

Please advice, I am trying to create easy module, which will give me members of the group.
And I am not sure how to work with parameters. How to specify when parameter is empty to throw for instance "please enter the group" and when group doesnt exist "group doesnt exist" etc..

And I am also not sure how 'Param' works.



Many thanks
function Get-RMGroupMembers

{
$empty = Get-QADGroup -Identity $args[0]
#$empty
if ($empty -eq $null){
 write "group doesnt exist"
}

try{
Get-QADGroup -Identity $args[0] | select -ExpandProperty members | Get-QADUser | select name
}

catch
{
write "group doesnt exist"
}
}

Open in new window

0
Comment
Question by:Radim88
  • 4
  • 4
  • 3
11 Comments
 
LVL 13

Expert Comment

by:soostibi
ID: 35213748
Try this:
function Get-RMGroupMembers  
{
	[cmdletbinding()]
        
    param(
    [Parameter(
        Mandatory = $true,
        Position = 0,
        HelpMessage = "Name of the group"
    )]
    [string]
    $name
    )
    if(!(Get-QADGroup $name)){Write-Error "Group cannot be found!"}
    else{
	   Get-QADGroup $name | Get-QADGroupMember | Select-Object -Property name
    }
}

Open in new window

0
 

Author Comment

by:Radim88
ID: 35214185
Isnt there possibility to use it without cmdletbinding I am actually not sure how does it work, when I run it It takes really long time and stops and I get this error:  "WARNING: This search was configured to retrieve only the first 1000 results"  I know its possible to extend. Wouldnt be possible to use the normal function?

Many thanks
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 35214201
If you validate the value entered into the parameter you can stop people passing empty values ("") which will lead to it returning all groups.

If you want it to explicitly check the group name, include the -Name parameter on Get-QADGroup. If you want more control, shift to LDAP filter.
function Get-RMGroupMembers  {
  Param(
    [Parameter(Mandatory = $true, Position = 0,  HelpMessage = "Name of the group")]
    [ValidateNotNullOrEmpty()]
    [string]$Name
  )

  if(!(Get-QADGroup $name)) { 
    Write-Error "Group cannot be found!"
  } else {
    Get-QADGroup $name | Get-QADGroupMember | Select-Object -Property name
  }
}

Open in new window

Chris
0
 

Author Comment

by:Radim88
ID: 35214287
Thanx, and I dont understand one thing, if I import-module etc.. and paste there 'Get-RMGroupMembers'  what calls that inputbox.

I easily dont understand this part.


Param(
    [Parameter(Mandatory = $true, Position = 0,  HelpMessage = "Name of the group")]
    #[ValidateNotNullOrEmpty()]
    [string]$Name
  )

Open in new window

0
 
LVL 13

Expert Comment

by:soostibi
ID: 35214312
The cmdletbinding version will not let you give more parameters than expected. Without cmdletbinding extra parameter values will go into the $args array. This is basically the only difference.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 70

Expert Comment

by:Chris Dent
ID: 35214320
This one?
Supply values for the following parameters:
(Type !? for Help.)

Open in new window

It appears because Mandatory is set to $True.

Chris
0
 
LVL 13

Accepted Solution

by:
soostibi earned 63 total points
ID: 35214329
This advanced function of PowerShell V2 hooks into the logic of cmdlet parameter handling. So the code that does the inputbox is inside the PowerShell engine, you just leverage this functionality by specifying the requested paramenter handling mode by the [Parameter(Mandatory = $true] part.
0
 

Author Comment

by:Radim88
ID: 35214528
Thanks a lot guys,

Please and other parameters?

 Position = 0,  HelpMessage = "Name of the group"  ,

And how would you do that without without that parameters?

Something like, if I play only (without any group name )"Get-RMGroupMembers" then it will write-host -- please enter the parameter. I am just curious.

And then let me accept solution for both of you.

BR

0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 35214775
You can do all of it without the Param block, whether you do or not depends on how you want the application to run. e.g.
Function Test-Param {
  Param(
    $Name
  )

  While (!$Name) {
    $Name = Read-Host -Prompt "You must tell me a name"
  }

  Write-Host "I have a value for name: $Name"
}

Open in new window

Chris
0
 
LVL 70

Assisted Solution

by:Chris Dent
Chris Dent earned 62 total points
ID: 35214795
Just to be clear, I meant without validation within the param block. The param block itself remains useful even if you don't use the more advanced features.

In the example above, each of the following will work:
Test-Param "Some Name"
Test-Param -Name "Some Name"

Open in new window

But the following will start it prompting until it gets a name:
Test-Param

Open in new window

Chris
0
 

Author Closing Comment

by:Radim88
ID: 35215218
Thx
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now