简体   繁体   English

PowerShell计划任务不将CSV转换为EXCEL

[英]PowerShell scheduled task not converting CSV to EXCEL

I have a server health check script which i'm trying to get working by scheduled task. 我有一个服务器运行状况检查脚本,我正在尝试按计划任务工作。

The scheduled task has the following set for 'AddArguments" 计划任务具有以下针对“AddArguments”的设置

Add Arguments: -NoLogo -ExecutionPolicy Bypass -File "C:\HealthCheck.ps1"

Everything in the server health portion of full script works fine to create the .csv report, except the last part, which does the CSV to excel conversion/save/close - I've not included the preceding code as it includes some confidential stuff, and i don't believe it's relevant. 完整脚本的服务器运行状况部分中的所有内容都可以正常创建.csv报告,除了最后一部分,它将CSV转换为excel转换/保存/关闭 - 我没有包含前面的代码,因为它包含一些机密内容,我不相信它是相关的。

When I run the script with the same ID, but from the GUI (not as a scheduled task) it works fine. 当我使用相同的ID运行脚本,但是从GUI(而不是计划任务)运行时,它可以正常工作。

Note: The last part of the script definitely does launch excel briefly and performs the functions, and saves/closes it - i'm thinking the scheduled task isn't doing this because it's not supported by Microsoft? 注意:脚本的最后一部分肯定会简单地启动excel并执行这些功能,然后保存/关闭它 - 我认为计划任务不是这样做的,因为微软不支持它?

I did find the following SpiceWorks post but the solution noted didn't resolve the issue for me in this case. 我确实找到了以下SpiceWorks帖子,但是在这种情况下,我所说的解决方案没有为我解决问题。 That's where you create a DESKTOP folder under these paths depending on your version of Office (i'm using Office 2010 32-bit on Windows 7 x64 Pro) 这是您在这些路径下创建DESKTOP文件夹的位置,具体取决于您的Office版本(我在Windows 7 x64 Pro上使用Office 2010 32位)

C:\windows\system32\config\systemprofile 
C:\windows\syswow64\config\systemprofile 

Anyway, here's the code - Any help appreciated! 无论如何,这是代码 - 任何帮助赞赏!

#Convert CSV to EXCEL, format, and save
#Create excel object
$xl = new-object -comobject excel.application
$xl.visible = $true

#Input
$Workbook = $xl.workbooks.open(“$Dir\Reports\SeverHealth-Results-    $CurrentDate.csv”)
$worksheet = $workbook.worksheets.Item(1)
$xl.Rows.Item("2:2").Select()
$xl.ActiveWindow.FreezePanes = $true
$HeaderRow = $Worksheet.Range("A1:L1")
$HeaderRow.Font.Bold = $True
$HeaderRow.Font.Underline = $True

$range = $worksheet.UsedRange
$range.AutoFilter() | Out-Null
$range.EntireColumn.AutoFit() | Out-Null
$rowc = $WorkSheet.UsedRange.Rows.Count
$colc = $WorkSheet.UsedRange.Columns.Count

#Coloring
for ($z = 1; $z -le $rowc; $z++) {
$ActionReqCol = $worksheet.cells.item($z,7)
$ServerCol= $worksheet.cells.item($z,1)
if ($ActionReqCol.text -eq "YES") {
    $ActionReqCol.interior.colorindex=3
    $ACtionReqCol.font.colorindex=2
$ServerCol.interior.colorindex=3
$ServerCol.font.colorindex=2}}

#Save and close!
$EndDate = Get-Date
$EndDate = $EndDate.ToString('MM-dd-yyyy_hhmm')
$Worksheets = $Workbooks.worksheets
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
$Workbook.SaveAs($Dir + "\Reports\SeverHealth-Results-$EndDate.xls”,     $XLFixedFormat)
$Workbook.Saved = $True
$xl.Quit()

Write the command to invoke the PowerShell with arguments in a batch file. 编写命令以在批处理文件中使用参数调用PowerShell。 I believe from the comments that you are already able to do this successfully. 我相信你已经能够成功地做到这一点。 Configure the Task Scheduler to execute the batch file. 配置任务计划程序以执行批处理文件。

Other advantage of this is, you have reduced dependency. 这样做的另一个好处是,您减少了依赖性。 If later you want to make modifications to your command or alter arguments, then you will be able to do so without altering or even opening the Task Scheduler. 如果以后要修改命令或更改参数,则可以在不更改甚至打开任务计划程序的情况下执行此操作。

Update: @Kenny reported that running task scheduler's task with highest privilege resolved this. 更新: @Kenny报告说,运行任务调度程序的具有最高权限的任务解决了这个问题。 The script required elevated access and the same was provided by checking the check box in Task Scheduler to run the task with highest privilege. 该脚本需要提升访问权限,并通过选中任务计划程序中的复选框以运行具有最高权限的任务来提供相同的访问权限。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM