Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell - create module - how to specify empty argument

Posted on 2011-03-25
11
Medium Priority
?
608 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 71

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
LVL 71

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 252 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 71

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 71

Assisted Solution

by:Chris Dent
Chris Dent earned 248 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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
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 antispam), the admini…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

650 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