我一直试图实现一个PowerShell脚本,它将访问一个Excel工作簿,签出它,刷新工作簿中的数据集,最后再签回它。
我将此与中的一个任务结合在一起,每天从具有访问SharePoint在线站点的用户帐户的服务器上运行脚本。
我的问题是脚本不会运行。当我查看Windows事件日志时,我可以看到它得到了一个403错误
这个脚本是从这里找到的文档中提取的:链接到下载文档
任务从任务操作配置中的参数获取以下脚本和Excel工作簿的位置(详见上面的文档)
try
{
# Creating the excel COM Object
$xl = New-Object -ComObject Excel.Application;
# Setting up Excel to run without UI and without alerts
$xl.DisplayAlerts = $false;
$xl.Visible = $false;
}
Catch
{
Write-EventLog -EventId "5001" -LogName "Application" -Message "Failed to start Excel" -Source "Application"
Exit
}
foreach ($i in $args)
{
write-host "handling $i"
try
{
# Allow update only if we can perform check out
If ($xl.workbooks.CanCheckOut($i))
{
# Opening the workbook, can be local path or SharePoint URL
$wb = $xl.workbooks.open($i);
# Perform the check out
$xl.workbooks.checkout($i)
# Calling the refresh
$wb.RefreshAll();
# Saving and closing the workbook
$wb.CheckInWithVersion();
# in case you are not using checkout/checkin perform a save and close
#$wb.Save();
#$wb.Close();
#Release Workbook
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb)
}
else
{
write-host "Check out failed for: $i"
Write-EventLog -EventId "5001" -LogName "Application" -Message "Workbook can't be checked out $i" -Source "Application"
}
}
catch
{
Write-EventLog -EventId "5001" -LogName "Application" -Message "Failed refreshing the workbook $i $_" -Source "Application"
}
}
#Quiting Excel
$xl.quit();
#Release Excel
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)我是不是漏掉了什么?
谢谢,如果需要更多的信息,请告诉我。
编辑:如果用正确的参数从cmd手动运行脚本,脚本就能工作。问题似乎在于任务调度程序无法访问PowerShell。
发布于 2015-08-28 05:57:54
所以我终于开始工作了。问题是,当选择任务的常规设置中的“用户登录与否”选项时,它没有运行脚本。当选择“用户登录”时工作正常。
这里是我必须采取的步骤,使它正常运行。
首先,脚本需要从System32文件夹中运行(还可以在任务“开始”框中指定该目录。此外,请确保您所指向的是32位版本的PowerShell,因为Excel不能使用64位。
其次,在Excel中有一个错误,您必须在\SysWOW64 64\config\systemprofile\和\System32 32\config\systemprofile\目录中创建一个名为“Desktop”的文件夹(如果运行Excel 64位,需要创建这两个文件夹)。
这是我最后使用的参数字符串: C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -nologo -noprofile -noninteractive -executionpolicy绕开路径\to\script‘path\to\\excelworkbook’
https://stackoverflow.com/questions/32096755
复制相似问题