Powershell - create module - how to specify empty argument

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

Radim88Asked:
Who is Participating?
 
soostibiConnect With a Mentor Commented:
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
 
soostibiCommented:
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
 
Radim88Author Commented:
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
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
Chris DentPowerShell DeveloperCommented:
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
 
Radim88Author Commented:
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
 
soostibiCommented:
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
 
Chris DentPowerShell DeveloperCommented:
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
 
Radim88Author Commented:
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
 
Chris DentPowerShell DeveloperCommented:
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
 
Chris DentConnect With a Mentor PowerShell DeveloperCommented:
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
 
Radim88Author Commented:
Thx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.