簡體   English   中英

從任務計划程序運行時,PowerShell 腳本將文件上傳到 SharePoint 不起作用

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

概述:我有一個腳本,它運行查詢以從 SharePoint 站點提取過去一天的所有審核日志。 然后它會創建一個.csv 文件,並將其上傳到 SharePoint 上的“共享文檔”文件夾。 當我從 PowerShell 控制台手動運行該腳本時,該腳本運行良好,無論它是否以管理員權限運行。

注意:此 SharePoint 解決方案是本地解決方案,而不是在線解決方案。

問題:當我從任務計划程序運行腳本時,它會生成 .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 文件中的唯一區別是,當我手動運行它與任務計划程序時,這些行被添加到任務計划程序腳本中:

PS>$global:?
True

成績單從我的 Try-Catch 打印“成功”行

問題出在 SharePoint 中。 每當我使用任務計划程序上傳文件時,該文件都被標記為“已簽出”,因此其他用戶無法查看。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM