Variables are not able

Function Group-List{
$groupList = Get-MsolGroup
$groupName = Read-Host “Enter group name to add an user”
Write-Host “group name selecte is $groupName”
$result = $groupList | Where-Object DisplayName -eq $groupName
$result
if ( $result ){
Write-Host “Group found”
}
else{
Write-Host “Group not found” -ForegroundColor Red
}
$groupList
Write-Host “Group List”
Write-Host “group Object id bellow”
$groupID = $result.objectID
$groupID
}
Function AddIn-Group{

$userAdd = Add-MsolGroupMember -GroupObjectId $groupID -GroupMemberObjectId $userID -GroupMemberType User

}
Group-List
AddIn-Group
##############################OUTPUT###################################
Add-MsolGroupMember : Cannot bind argument to parameter ‘GroupObjectId’ because it is null.
At line:21 char:51

  • $userAdd = Add-MsolGroupMember -GroupObjectId $groupID -GroupMemb ...
    
  •                                               ~~~~~~~~
    
    • CategoryInfo : InvalidData: (:slight_smile: [Add-MsolGroupMember], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Online.Administration.Automation.AddGroupMember

##############################Question####################################
I want to pass $groupID from groupList function to AddIn-Group function. But value of $groupID is not passed to the function and gives above error. This is crysis. Please help me guys…

Tip, please use the pre and /pre tags around your code.
Just follow the text above were you type the post.

To begin, by calling the first function the output will not be used when you call the second function.
You would need to use the pipe from the first to the second.
which in turn would need you to use the ”param” settings of the functions.

See the functions as two completely seperate pieces of code that don’t know of eachother.

If it’s a crisis then remove the function statements and just use it as a top-down script. Otherwise read up on how parameters work with functions.

Scope problem. Variables in a function are only available in the function.
This is a short example how to get around that.

Function Group-List($somegroup){
#whatever
 }

Group-List $group

I’ve not tested this, so beware, but I’d be wanting to do something more like this, see below.
But, “Get-MsolGroup” requires some parameter and values input or potentially it’ll return a lot of superfluous data.
Again please test this before using it, but you should be able to call the “AddIn-Group” by passing either the “$GroupID” or “$groupName”.

eg:
AddIn-Group -groupname $groupname

AddIn-Group -groupid $groupid
Function Group-List
{
    [cmdletbinding()]
    param (
    [Parameter(Mandatory = $true, HelpMessage="Enter group name to add an user")]
    [ValidateNotNullorEmpty()]
    [string]$groupName 
    )

$groupList = Get-MsolGroup

Write-Host "group name selected is $groupName"
$result = $groupList | Where-Object DisplayName -eq $groupName

    if ( $result )
    {
        Write-Host "Group found"
    }
    else
    {
        Write-Host "Group not found" -ForegroundColor Red
    }

    $groupID = $result.objectID
    Return $groupID
}

Function AddIn-Group
{
    [cmdletbinding()]
    param (
    [Parameter(Mandatory = $false,ParameterSetName = "id")]
    [Parameter(ParameterSetName = "name")]
    [string]$userID ,
    [Parameter(Mandatory = $false,ParameterSetName = "id")]
    [string]$groupid ,
    [Parameter(Mandatory = $false,ParameterSetName = "name")]
    [string]$groupName
    )

    if ( $groupid )
    {
        $userAdd = Add-MsolGroupMember -GroupObjectId $groupid -GroupMemberObjectId $userID -GroupMemberType User
    }
    elseif ( $groupName )
    {
        $userAdd = Add-MsolGroupMember -GroupObjectId $(Group-List -groupName $groupName) -GroupMemberObjectId $userID -GroupMemberType User
    }
    else { "Please provide a GroupID or a Group Name" }

}

Thank you @iain Barnetson, That’s really helpfull