0

Ok peeps, say I've got something like:

Function Get-Reports {
 # Get managers direct report, calls function above
 $DirectReports = Get-DirectReport $Manager -norecurse  | Select-Object samAccountName
 if ($null -ne $DirectReports) {        
     #$LogLine = "Gathering direct reports for " + $Manager
     #Log-Write -LogPath $sLogFile -LineValue $LogLine
 } else {
     $LogLine = $Manager + " has no reports."
     #Log-Write -LogPath $sLogFile -LineValue $LogLine
     #Continue
 }   
}
Function Set-RTGmembers {
    # Get manager's 'report to <manager>' group again to update members
    $managerReportToGroup = Get-ADGroup -SearchBase $ou -Filter "Name -like 'Report to $Manager'"
    if ($managerReportToGroup) {
        Add-ADGroupMember -identity $managerReportToGroup.Name -members $DirectReports
        Add-ADGroupMember -identity $managerReportToGroup.name -members $Manager
        $LogLine = "Report to " + $Manager + " updated."
        #Log-Write -LogPath $sLogFile -LineValue $LogLine
    } else {
        $LogLine = "Could not find group for " + $Manager
        #Log-Write -LogPath $sLogFile -LineValue $LogLine
    }
}

Foreach ($manager in $managers) { 
    Get-Reports
    Set-RTGmembers
 }

I get an error saying the Add-ADGroupMember's -members $DirectReports value is empty. I've debug it and it's indeed empty. How do I pass a variable created in a function to another function? Should I combine them maybe?

1
  • 1
    Your functions need to define some parameters which you pass object in by. Additionally you'll probably want your functions to output some objects learn.microsoft.com/en-us/powershell/module/… Commented Feb 26, 2021 at 4:46

1 Answer 1

1

What you need to do is to return something from the function and pass it as a parameter for the other one. But in your case, the first function is not needed Something like this

Function Set-RTGmembers {
    Param(
        $Reports,
        [string]$Manager,
        [string]$OU
    )
    # Get manager's 'report to <manager>' group again to update members
    $managerReportToGroup = Get-ADGroup -SearchBase $OU -Filter "Name -like 'Report to $Manager'"
    if ($managerReportToGroup) {
        Add-ADGroupMember -identity $managerReportToGroup.Name -members $Reports
        Add-ADGroupMember -identity $managerReportToGroup.name -members $Manager
        $LogLine = "Report to " + $Manager + " updated."
        #Log-Write -LogPath $sLogFile -LineValue $LogLine
    } else {
        $LogLine = "Could not find group for " + $Manager
        #Log-Write -LogPath $sLogFile -LineValue $LogLine
    }
}

$ou = 'SomeOUName'
Foreach ($manager in $managers) {
    $DirectReports = $null #This is to make sure if the Get-DirectReport fails on the last loop, it won't use the last set value
    $DirectReports = Get-DirectReport $manager -norecurse  | Select-Object samAccountName
    if ($null -ne $DirectReports) {        
         #$LogLine = "Gathering direct reports for " + $Manager
        #Log-Write -LogPath $sLogFile -LineValue $LogLine
    } else {
         $LogLine = $manager + " has no reports."
         #Log-Write -LogPath $sLogFile -LineValue $LogLine
         Continue
     }   
    Set-RTGmembers -Reports $DirectReports -Manager $manager -OU $ou
}

Remember that each function has its own scope and the variables declared inside them gets cleared when the function finishes

Sign up to request clarification or add additional context in comments.

3 Comments

Question though, in my original code I'm passing (or trying to pass) $DirectReports to the next function Set-RTGmembers. In your example there's no source for $Reports. Now $Manager I define up at the beginning of the script but $Reports or $DirectReports was originally a loop that I was trying to turn into a function.
Inside the function, you can declare the param you're passing using Param() at the beginning of the function so those variables belong to the function scope and when you passe them using -ParamName (When ParamName means the Variable declared inside the Params() section) you set those variables values, therefore, you can use them inside the function
In case you need a bit more information, you could check here (learn.microsoft.com/en-us/powershell/module/…) and here (learn.microsoft.com/en-us/powershell/module/…) or here (tutorialspoint.com/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.