0

Overview: I have a script which runs a query to extract all Audit Logs from the past day, from a SharePoint site. It then creates a .csv file, and uploads it to the "Shared Documents" folder on SharePoint. The script works perfectly when I manually run it from PowerShell console, regardless if it's run with admin rights, or not.

NOTE: This SharePoint solution is On-Premise, not Online.

The Issue: When I run the script from Task Scheduler it generates the .csv file, and completes the whole script, but the file is never uploaded. There's no error messages.

Task Scheduler Settings: I use "Run whether user is logged on or not" and "Run with highest privileges"

Here's the full script:

Start-Transcript -Path "C:\Timer Jobs\exampleDomain.Audit.CreateAndTrimLog\transcript17.txt" -NoClobber

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

#Get date, and format it. Used for file name, and for deleting old Audit files
$today = Get-Date
$startDate = $today.AddDays(-1)
$todayFormatted = $today.ToString('dd-MM/yyyy_HH-mm-ss')

#Get site and save siteURL
$siteUrl = "https://example.com"
$docLibName = "Shared Documents"
#site is needed for Audit Query
$site = Get-SPSite -Identity $siteUrl
#web is needed to upload the file
$web = Get-SPWeb -Identity $siteUrl
$list = $web.Lists[$docLibName]
$folder = $list.RootFolder
$files = $folder.Files


#Creaty query for Audits
$wssQuery = New-Object -TypeName Microsoft.SharePoint.SPAuditQuery($site) 
$wssQuery.SetRangeStart($startDate)
$wssQuery.SetRangeEnd($today)

#Create Audit Collection object
$auditCol = $site.Audit.GetEntries($wssQuery)

#Get all users on site
$users = $site.RootWeb.SiteUsers

#Get and add $userName to each $audit
#Ignore when it says "User cannot be found" in log
$CachedUsers = @{};
foreach($audit in $auditCol){
    $curUserId = $audit.UserId;
    $user = $null;
    if($CachedUsers.$curUserId -eq $null){
        $user = $users.GetById($curUserId);
        $CachedUsers.$curUserUI = $user;
    } else {
        $user = $CachedUsers.$curUserId;
    }

    $userName = ($user.DisplayName + " <" + $user.LoginName) + ">";
    $audit | Add-Member NoteProperty -Name "UserName" -Value $userName -Force;
}

#Export CSV-file. Save fileName and filePath for when uploading to SharePoint
$fileName = ("domain_Audit_Log_" + $todayFormatted + ".csv")
$filePath = "C:\Users\xml\" + ($fileName)
$auditCol | Export-Csv -Append -path ($filePath) -NoTypeInformation -Delimiter ";" -Encoding UTF8


$file = Get-ChildItem $filePath
[Microsoft.SharePoint.SPFile]$spFile = $web.GetFile("/" + $folder.Url + "/" + $file.Name)


if($spFile.Exists -eq $false) {
    #Open FileStream
    $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()
    #Add File
    Write-Host "Uploading file" $file.Name "to" $folder.ServerRelativeUrl "..."
    try {
        [Microsoft.SharePoint.SPFile]$spFile = $files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
        Write-Host "Success"
    } catch {
        Write-Host "Error"
    }

    #Close FileStream
    $fileStream.Close()
}

$web.Dispose()
#$site.Audit.DeleteEntries($startDate)

Only difference in the Transcript.txt file, when I run it Manually vs. Task Scheduler is these lines, that are added in the Task Scheduler Tanscript:

PS>$global:?
True

The Transcript prints the "Success" line from my Try-Catch

1 Answer 1

0

Problem was in SharePoint. Whenever I uploaded a file using Task Scheduler the file was marked as "Checked Out", so it was not viewable for other users.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.