Understanding this script

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:

[pre]

$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()
}

[/pre]

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: <name of website>.

The script will then display the text “<name of website> - 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?

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()
}

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. :slight_smile:

Thank you for taking a look and helping me out.