简体   繁体   English

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

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

Overview: I have a script which runs a query to extract all Audit Logs from the past day, from a SharePoint site.概述:我有一个脚本,它运行查询以从 SharePoint 站点提取过去一天的所有审核日志。 It then creates a.csv file, and uploads it to the "Shared Documents" folder on SharePoint.然后它会创建一个.csv 文件,并将其上传到 SharePoint 上的“共享文档”文件夹。 The script works perfectly when I manually run it from PowerShell console, regardless if it's run with admin rights, or not.当我从 PowerShell 控制台手动运行该脚本时,该脚本运行良好,无论它是否以管理员权限运行。

NOTE: This SharePoint solution is On-Premise, not Online.注意:此 SharePoint 解决方案是本地解决方案,而不是在线解决方案。

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.问题:当我从任务计划程序运行脚本时,它会生成 .csv 文件,并完成整个脚本,但该文件从未上传。 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: Transcript.txt 文件中的唯一区别是,当我手动运行它与任务计划程序时,这些行被添加到任务计划程序脚本中:

PS>$global:?
True

The Transcript prints the "Success" line from my Try-Catch成绩单从我的 Try-Catch 打印“成功”行

Problem was in SharePoint.问题出在 SharePoint 中。 Whenever I uploaded a file using Task Scheduler the file was marked as "Checked Out", so it was not viewable for other users.每当我使用任务计划程序上传文件时,该文件都被标记为“已签出”,因此其他用户无法查看。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows 任务计划程序中的 PowerShell 脚本无法正常工作 - PowerShell script doesn't work correctly from Windows Task Scheduler 当用户未登录时,修改 Word 文件的 PowerShell 脚本不适用于任务计划程序 - PowerShell script that modifies Word file doesn't work with Task Scheduler when user isn't logged on 从任务计划程序运行时,PowerShell脚本失败 - PowerShell script failing when run from the Task Scheduler 使用任务计划程序计划的Powershell脚本无法运行…脚本运行正常 - Powershell script scheduled using Task scheduler doesn't run…script runs fine 尝试从批处理脚本运行 Powershell 命令时,PowerShell 用文本文件中的另一行替换行无法正常工作 - PowerShell replace line with another line in text file doesn't work as expected when attemtping to run Powershell commands from Batch script 当编译或作为脚本运行时,简单的Powershell脚本不起作用 - Simple Powershell script doesn't work when compiled or run as a script 在任务计划程序上运行时,用于删除引号的 powershell 脚本代码不会删除它们 - powershell script code to remove quotation marks doesn't remove them when running on task scheduler Powershell 脚本无法使用任务计划程序正常运行 - Powershell Script Can't Run Properly With Task Scheduler 通过任务计划程序运行时,Azure Powershell脚本失败 - Azure Powershell script fails when run through task scheduler VBScript 不会从从 Windows 任务计划程序运行的 Powershell 脚本中运行 - VBScript will not run from Powershell script that runs from Windows Task Scheduler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM