Radim88
asked on
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
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"
}
}
ASKER
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
Many thanks
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.
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
}
}
Chris
ASKER
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.
I easily dont understand this part.
Param(
[Parameter(Mandatory = $true, Position = 0, HelpMessage = "Name of the group")]
#[ValidateNotNullOrEmpty()]
[string]$Name
)
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.
This one?
Chris
Supply values for the following parameters:
(Type !? for Help.)
It appears because Mandatory is set to $True.Chris
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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"
}
Chris
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thx
Open in new window