Solved

Powershell - create module - how to specify empty argument

Posted on 2011-03-25
11
603 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…

751 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