powershell recursive function array values disappears

Welcome Forums General PowerShell Q&A powershell recursive function array values disappears

This topic contains 1 reply, has 1 voice, and was last updated by

 
Participant
1 year, 9 months ago.

  • Author
    Posts
  • #70963

    Participant
    Points: 0
    Rank: Member

    Before calling recursive function – RecursiveWebs, $webinfo.count is 1. During the execution of recursive function, count is increased but as soon is recursive function is completed and control return to main function, $webinfo.count again decreases to 1.

    function RecursiveWebs($web){
        $Properties = @{
            Title = $web.Title
            URL = $web.Url
        }
    
        $webinfo += New-Object psobject -Property $properties       
        foreach($w in $web.Webs)
        {
            if ($w.Webs.Count -gt 0){
             RecursiveWebs $w
            }
        }
    }
    
    $global:webinfo = @() 
    $sites = Get-SPOSite -Limit All
    foreach ($site in $sites) {
        Write-Host $site.Url
        try {
            $Properties = @{
                Title = $site.Title
                URL = $site.Url            
                NoOfSubsites = $site.Webs.Count        
            }    
    
            $webinfo += New-Object psobject -Property $properties
    
            if ($site.Webs.Count -gt 0)
            {            
                RecursiveWebs $site
            }        
        }
        catch 
        {       
            Write-Host $_ -ForegroundColor Red
        }
    }
    
  • #71063

    Participant
    Points: 0
    Rank: Member

    Replaces $webinfo with $Global:webinfo everywhere in the code and it did the trick

The topic ‘powershell recursive function array values disappears’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort