当任务计划程序 运行 时,PowerShell 脚本将文件上传到 SharePoint 不起作用

PowerShell Script uploading file to SharePoint doesn't work when run from Task Scheduler

概述: 我有一个脚本,其中 运行 是一个查询,用于从 SharePoint 站点提取过去一天的所有审核日志。然后它会创建一个 .csv 文件,并将其上传到 SharePoint 上的“共享文档”文件夹。 当我从 PowerShell 控制台手动 运行 脚本时,该脚本完美运行,无论它是否 运行 具有管理员权限。

注意:此 SharePoint 解决方案是内部部署的,而不是在线的。

问题: 当我 运行 来自 Task Scheduler 的脚本时,它会生成 .csv 文件,并完成整个脚本,但文件永远不会上传。没有错误消息。

任务计划程序设置:我使用“运行用户是否登录”和“运行具有最高权限”

完整脚本如下:

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)

Transcript.txt 文件中的唯一区别,当我 运行 它手动与任务计划程序时,这些行是在任务计划程序 Tanscript 中添加的:

PS>$global:?
True

成绩单打印了我的 Try-Catch 中的“成功”行

问题出在 SharePoint 中。每当我使用 Task Scheduler 上传文件时,该文件都会被标记为“已签出”,因此其他用户无法查看。