6
\$\begingroup\$

I'm creating a script to turn into a commandlet we can reuse as part of our custom PowerShell module. This works, but is very slow. I don't suspect it is anything to do with my code. I have to individually load each site to retrieve the title even though this property is visible off the $site object, it is always empty/null.

Anything else I can do to improve the efficiency?

$path = "C:\Users\$env:USERNAME\Desktop\SPOExport.csv"
$csv = "Title,URL,Sharing Capability,Storage Quota (MB),Template`r`n"
$sites = Get-SPOSite -Limit all | Select Url,SharingCapability,StorageQuota,Template
foreach($site in $sites){
    try{
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($site.Url)  
        $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)
        $web = $context.Web 
        $context.Load($web) 
        $context.ExecuteQuery() 
        $title = $web.Title
    }
    catch {
        $title = "Error fetching title"
    }
    finally{
        $csv +=$title + "," + $site.Url + "," + $site.SharingCapability + "," + $site.StorageQuota + "," + $site.Template
    }
}
$fso = new-object -comobject scripting.filesystemobject
$file = $fso.CreateTextFile($path,$true)
$file.write($csv)
$file.close()

Note that I have not fully adapted it into a commandlet yet, which will have all the commenting and help text available, I'm more worried about the code and the time it takes for this operation presently.

\$\endgroup\$
  • 1
    \$\begingroup\$ Which part is slow? I'm not sure how to do proper profiling in PowerShell, but for a poor-man's profiler, you could just single-step through the script in the PowerShell ISE and see if any lines stand out as being slow. \$\endgroup\$ – Dangph Jan 20 '16 at 10:59
  • \$\begingroup\$ The try block is very slow, I don't know if it is us being throttled by Microsoft or what, but it was taking upwards of 20 minutes to get through 20% of our SharePoint Online sites. \$\endgroup\$ – Eric Alexander Jan 20 '16 at 12:00
  • \$\begingroup\$ There are 6 lines in the try block. Is any one of them particularly slow? If you want to speed something up, it's helpful to know exactly where the slowness is. How many sites are there? \$\endgroup\$ – Dangph Jan 20 '16 at 17:07
  • \$\begingroup\$ We have about 100 SPO sites, and doing a simple CSOM fetch for the title shouldn't be as time consuming as it is. \$\endgroup\$ – Eric Alexander Jan 20 '16 at 17:47
2
\$\begingroup\$

Well, I cannot tell why it's so slow, but this should solve your problem:

$sites = Get-SPOSite -Limit all -Detailed

-Detailed switch will cause Get-SPOSite to load all properties. It makes iterating all sites to get title unnecessary.

\$\endgroup\$
  • \$\begingroup\$ Unlike some other Stack Exchange sites, we like to know why some code you be better versus what the owner have. We are here to help the OP code better and explaining things are a good way to help the OP. \$\endgroup\$ – Marc-Andre Feb 11 '16 at 20:20
  • \$\begingroup\$ I totally did not see the -Detailed switch, that gets me everything I need plus allows me to pull other fields that normally aren't calculated like StorageUsageCurrent. \$\endgroup\$ – Eric Alexander Feb 11 '16 at 21:42
  • \$\begingroup\$ @Marc-Andre This one seemed quite self explanatory, but you're right. I've added short explanation and I'll keep that in mind next time. \$\endgroup\$ – dstarkowski Feb 12 '16 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.