Understanding this script

Welcome Forums General PowerShell Q&A Understanding this script

Viewing 3 reply threads
  • Author
    Posts
    • #176440
      Participant
      Topics: 1
      Replies: 1
      Points: -7
      Rank: Member

      Hello all,

      I am new to reading powershell scripts so I wanted to ask you if my reading of this short script is corrrect.

      Here is the script:

      
      $user = "someuser"
      
      foreach ($site in gets spsite -Limit ALL)
      {
      if (($site.Owner.UserLogin -eq $user) -OR ($site.SecondaryContact.UserLogin -eq $user))
      {
      Write-host 'Deleting audit data for site: ' $sc.URL
      $i = -50
      do {
      Write-Host $site.URL ' – Delete day ' $i ':
      ' ([System.DateTime]::Now.ToLocalTime().AddDays($i))
      $site.audit.deleteentries([System.DateTime]::NowToLocalTime().AddDays($i))
      $site.audit.update()
      $i++
      }
      while ($i -le 1)
      }
      $site.Dispose()
      }
      
      

      My reading of this script is:

      For each site on the server, if the value of UserLogin OR the value of SecondaryContact.UserLogin IS 'someuser', write a string of text that reads, "Deleting audit data for: .

      The script will then display the text " – Delete day 50"

      The script will delete audit log entries for files in the name format of date and time followed by the number 50 in the filename.

      The script will then change $i to 49 and repeat until it gets to 1, deleting each audit log that matches the name format specified.

      Is that a correct reading?

    • #176473
      Participant
      Topics: 10
      Replies: 1283
      Points: 1,079
      Helping Hand
      Rank: Community Hero

      Seeing is believing, right. For a script this simple, I would start by remarking lines that are actually making changes and run it to see the output. Generally, your logic breakdown seems correct, the only difference is it appears to be searching for entries fifty days in the past, then 49 and so forth.

      $user = "someuser"
      
      foreach ($site in Get-SPSite -Limit ALL) {
          if (($site.Owner.UserLogin -eq $user) -OR ($site.SecondaryContact.UserLogin -eq $user)) {
              Write-host 'Deleting audit data for site: ' $sc.URL
              $i = -50
              do {
                  Write-Host $site.URL ' – Delete day ' $i ':
                  ' ([System.DateTime]::Now.ToLocalTime().AddDays($i))
                  #$site.audit.deleteentries([System.DateTime]::NowToLocalTime().AddDays($i))
                  #$site.audit.update()
                  $i++
              }
              while ($i -le 1)
          }
      
          $site.Dispose()
      }
      
    • #176476
      Participant
      Topics: 2
      Replies: 511
      Points: 1,306
      Helping Hand
      Rank: Community Hero

      That sounds about right from what I can see there. Script could probably use a bit of a tidying up, but that's the gist of it. 🙂

    • #176479
      Participant
      Topics: 1
      Replies: 1
      Points: -7
      Rank: Member

      Thank you for taking a look and helping me out.

Viewing 3 reply threads
  • The topic ‘Understanding this script’ is closed to new replies.