Integer Variable Not Increasing When Provided with Values

Welcome Forums General PowerShell Q&A Integer Variable Not Increasing When Provided with Values

  • This topic has 3 replies, 2 voices, and was last updated 2 months ago by
    J
    Participant
    .
Viewing 3 reply threads
  • Author
    Posts
    • #200594
      J
      Participant
      Topics: 3
      Replies: 6
      Points: 42
      Rank: Member

      Please can anyone help with this issue.

      I’m trying to calculate the size of the homedrive for each user in an AD Group and then add it to a variable to keep a running total  of the homedrive sizes. I want the size of the each users homedrive and the running total size of all mailboxes to be displayed as the search progresses. I have written the following:

      [int]$TotalSizeofAllHomeDrives = 0 
      
      $ADUser = (get-aduser -SearchBase "ExampleADGroup" -filter *).samaccountname
      
      Foreach ($User in $ADUser){
      
      $SizeofUserHomeDrive = ((get-aduser -id "$User" -properties homedirectory).homedirectory |
      Get-ChildItem -recurse | measure-object -property length -sum).sum
      
      $SizeofUserHomeDrive += $TotalSizeofAllHomeDrives
      
      Write-Host "The size of the Home Drive for $User is $SizeofUserHomeDrive" 
      Write-Host "Current total of all Home Drives is $TotalSizeofAllHomeDrives"
      
       }

      As the search runs, the size of each users home drive is being displayed but the ‘Current total of all Home Drives (the $TotalSizeofAllHomeDrives figure) always displays as 0.

    • #200600
      Participant
      Topics: 11
      Replies: 462
      Points: 911
      Helping Hand
      Rank: Major Contributor

      line 10 should read

      $TotalSizeofAllHomeDrives += $SizeofUserHomeDrive
      

      🙂

    • #200630
      J
      Participant
      Topics: 3
      Replies: 6
      Points: 42
      Rank: Member

      Thanks for the input. I’ve changed the line as suggested.

      It now seems to update the $TotalSizeofAllHomeDrives based on the first users $SizeofUserHomeDrive but then doesn’t update it any further for subsequent users, even though their $SizeofUserHomeDrive are being returned.

    • #200678
      J
      Participant
      Topics: 3
      Replies: 6
      Points: 42
      Rank: Member

      I’ve changed the output to GB and it’s started to work – maybe didn’t like the large numbers?

      I’ve encountered another issue where, for some user folders, I get ‘An unexpected network error has occured’.  The results are being output for each user without issue until I get one of these error messages but then I get no more outputs for any users even though the script continues to run.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.