Understanding this script

Welcome Forums General PowerShell Q&A Understanding this script

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
1 week, 2 days ago.

  • 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: 8
    Replies: 1203
    Points: 706
    Helping Hand
    Rank: Major Contributor

    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: 483
    Points: 1,152
    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.

You must be logged in to reply to this topic.