通过任务计划程序

时间:2017-08-14 12:30:30

标签: powershell scheduled-tasks

我可以根据需要发布代码(它相当长),但缺点是我有一个Ps1脚本获取有关共享的信息(文件名,创建日期等)并创建一个CSV。文件在一个位置。然后它将所有csv更改为xlsx并删除csv。当手动运行时,这非常有效,当计划通过具有最高权限的任务调度程序运行时,它会创建csv,然后删除所有内容,似乎跳过转换为xlsx。

当手动运行和通过任务计划程序运行时,会导致相同脚本的行为方式有何不同?

以下代码:

    ###Make sure I can Access the Share
net use \\Share\Share /user:USER PASS /persisten:no

###Move the ones that exist to Back-Up Delete Backup
Remove-Item C:\Users\USER\Desktop\OutputBk\* -recurse
Get-ChildItem -Path "C:\Users\USER\Desktop\Output" -Recurse -File | Move-Item -Destination "C:\Users\USER\Desktop\OutputBk"

### Output1

Get-ChildItem -Path \\SHARE\Output1 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output1.csv -NoTypeInformation

### Output2

Get-ChildItem -Path \\SHARE\Output2 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output2.csv -NoTypeInformation

### Output3

Get-ChildItem -Path \\SHARE\ Output3-Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output3.csv -NoTypeInformation

### Output4

Get-ChildItem -Path \\SHARE\ Output4 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output4.csv -NoTypeInformation

### Output5

Get-ChildItem -Path \\SHARE\ Output5 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output5.csv -NoTypeInformation

### Output6

Get-ChildItem -Path \\SHARE\ Output6 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output6.csv -NoTypeInformation

### Output7

Get-ChildItem -Path \\SHARE\ Output7 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output7.csv -NoTypeInformation

### Output8

Get-ChildItem -Path \\SHARE\ Output8 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output8.csv -NoTypeInformation

### Output9

Get-ChildItem -Path \\SHARE\ Output9 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output9.csv -NoTypeInformation

### Output10

Get-ChildItem -Path \\SHARE\ Output10 -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output10.csv -NoTypeInformation

### Output11

Get-ChildItem -Path "\\SHARE\Recycle Bin - Output11" -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output11.csv -NoTypeInformation

### Output12

 Get-ChildItem -Path "\\SHARE\Recycle Bin - Output12" -Recurse | Select-Object CreationTime, LastAccessTime, LastWriteTime, Extension, Length, BaseName, PSParentPath, PSChildName, Directory, DirectoryName, DBDateAdded | Export-CSV C:\Users\USER\Desktop\Output\Output12.csv –NoTypeInformation

### Convert the CSV to XLSX

$workingdir = "C:\Users\USER\Desktop\Output\*.csv"
$csv = dir -path $workingdir
foreach($inputCSV in $csv){
$outputXLSX = $inputCSV.DirectoryName + "\" + $inputCSV.Basename + ".xlsx"

#### Create a new Excel Workbook with one empty sheet
$excel = New-Object -ComObject excel.application
$excel.DisplayAlerts = $False 
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)

### Build the QueryTables.Add command
### QueryTables does the same as when clicking "Data » From Text" in Excel
$TxtConnector = ("TEXT;" + $inputCSV)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)

### Set the delimiter (, or ;) according to your regional settings
$query.TextFileOtherDelimiter = $Excel.Application.International(5)

### Set the format to delimited and text for every column
### A trick to create an array of 2s is used with the preceding comma
$query.TextFileParseType  = 1
$query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count
$query.AdjustColumnWidth = 1

### Execute & delete the import query
$query.Refresh()
$query.Delete()

### Save & close the Workbook as XLSX. Change the output extension for Excel 2003
$Workbook.SaveAs($outputXLSX,51)
$excel.Quit()
}
### To exclude an item, use the '-exclude' parameter (wildcards if needed)
remove-item -path $workingdir -exclude *Crab4dq.csv

### Rename Output1

$xlspath = "C:\Users\USER\Desktop\Output\Output1.xlsx"
$xldoc = new-object -comobject Excel.application
$xldoc.Visible = $false
$xldoc.DisplayAlerts = $false
$workbook = $xldoc.Workbooks.Open($xlspath )
$worksheet = $workbook.worksheets.item(1)
$worksheet.name = "1"
$workbook.Save()
$workbook.Close()
$xldoc.Quit()

### Rename Output2

$xlspath = "C:\Users\USER\Desktop\Output\Output2.xlsx"
$xldoc = new-object -comobject Excel.application
$xldoc.Visible = $false
$xldoc.DisplayAlerts = $false
$workbook = $xldoc.Workbooks.Open($xlspath )
$worksheet = $workbook.worksheets.item(1)
$worksheet.name = "2"
$workbook.Save()
$workbook.Close()
$xldoc.Quit()

### Rename Output3

$xlspath = "C:\Users\USER\Desktop\Output\Output3.xlsx"
$xldoc = new-object -comobject Excel.application
$xldoc.Visible = $false
$xldoc.DisplayAlerts = $false
$workbook = $xldoc.Workbooks.Open($xlspath )
$worksheet = $workbook.worksheets.item(1)
$worksheet.name = "3"
$workbook.Save()
$workbook.Close()
$xldoc.Quit()

### Rename Output4

$xlspath = "C:\Users\USER\Desktop\Output\Output4.xlsx"
$xldoc = new-object -comobject Excel.application
$xldoc.Visible = $false
$xldoc.DisplayAlerts = $false
$workbook = $xldoc.Workbooks.Open($xlspath )
$worksheet = $workbook.worksheets.item(1)
$worksheet.name = "4"
$workbook.Save()
$workbook.Close()
$xldoc.Quit()

### Rename Output5

$xlspath = "C:\Users\USER\Desktop\Output\Output5.xlsx"
$xldoc = new-object -comobject Excel.application
$xldoc.Visible = $false
$xldoc.DisplayAlerts = $false
$workbook = $xldoc.Workbooks.Open($xlspath )
$worksheet = $workbook.worksheets.item(1)
$worksheet.name = “5"
$workbook.Save()
$workbook.Close()
$xldoc.Quit()

### Rename Output6

$xlspath = "C:\Users\USER\Desktop\Output\Output6.xlsx"
$xldoc = new-object -comobject Excel.application
$xldoc.Visible = $false
$xldoc.DisplayAlerts = $false
$workbook = $xldoc.Workbooks.Open($xlspath )
$worksheet = $workbook.worksheets.item(1)
$worksheet.name = "6"
$workbook.Save()
$workbook.Close()
$xldoc.Quit()

### Rename Output7

$xlspath = "C:\Users\USER\Desktop\Output\Output7.xlsx"
$xldoc = new-object -comobject Excel.application
$xldoc.Visible = $false
$xldoc.DisplayAlerts = $false
$workbook = $xldoc.Workbooks.Open($xlspath )
$worksheet = $workbook.worksheets.item(1)
$worksheet.name = "7"
$workbook.Save()
$workbook.Close()
$xldoc.Quit()

### Rename Output8

$xlspath = "C:\Users\USER\Desktop\Output\Output8.xlsx"
$xldoc = new-object -comobject Excel.application
$xldoc.Visible = $false
$xldoc.DisplayAlerts = $false
$workbook = $xldoc.Workbooks.Open($xlspath )
$worksheet = $workbook.worksheets.item(1)
$worksheet.name = "8"
$workbook.Save()
$workbook.Close()
$xldoc.Quit()

### Rename Output9

$xlspath = "C:\Users\USER\Desktop\Output\Output9.xlsx"
$xldoc = new-object -comobject Excel.application
$xldoc.Visible = $false
$xldoc.DisplayAlerts = $false
$workbook = $xldoc.Workbooks.Open($xlspath )
$worksheet = $workbook.worksheets.item(1)
$worksheet.name = "9"
$workbook.Save()
$workbook.Close()
$xldoc.Quit()

### Rename Output10

$xlspath = "C:\Users\USER\Desktop\Output\Output10.xlsx"
$xldoc = new-object -comobject Excel.application
$xldoc.Visible = $false
$xldoc.DisplayAlerts = $false
$workbook = $xldoc.Workbooks.Open($xlspath )
$worksheet = $workbook.worksheets.item(1)
$worksheet.name = "10"
$workbook.Save()
$workbook.Close()
$xldoc.Quit()

### Rename Output11

$xlspath = "C:\Users\USER\Desktop\Output\Output11.xlsx"
$xldoc = new-object -comobject Excel.application
$xldoc.Visible = $false
$xldoc.DisplayAlerts = $false
$workbook = $xldoc.Workbooks.Open($xlspath )
$worksheet = $workbook.worksheets.item(1)
$worksheet.name = "11"
$workbook.Save()
$workbook.Close()
$xldoc.Quit()

### Rename Output12

$xlspath = "C:\Users\USER\Desktop\Output\Output12.xlsx"
$xldoc = new-object -comobject Excel.application
$xldoc.Visible = $false
$xldoc.DisplayAlerts = $false
$workbook = $xldoc.Workbooks.Open($xlspath )
$worksheet = $workbook.worksheets.item(1)
$worksheet.name = "12"
$workbook.Save()
$workbook.Close()
$xldoc.Quit()

谢谢,

2 个答案:

答案 0 :(得分:0)

Task Properties - Mail Trigger

希望您已经提供了一个有权修改文件的帐户,某些帐户可能只有写入权限才能修改共享文件夹中的文件。 在为帐户提供所需权限后,如果问题仍然存在,请尝试在删除CSV文件的命令行开关之前添加“Start-Sleep -s 60”。有些时候删除任务可能会覆盖以前的任务,假设更改了文件扩展名。如果以上都不奏效,请发布脚本。

答案 1 :(得分:0)

ty gms0ulman - 感谢您的链接,我找到了答案。

这是DCOM权限问题。

我发现此类问题的唯一方法是将Excel设置为通过DCOM权限以特定用户身份运行。

打开组件服务(开始 - >运行,输入dcomcnfg) 深入研究组件服务 - >计算机 - >我的电脑,然后单击DCOM配置 右键单击Microsoft Excel Application,然后选择“属性” 在“标识”选项卡中,选择“此用户”并输入交互式用户帐户(域或本地)的ID和密码,然后单击“确定” 不幸的是,将其保留为交互式用户或启动并不能与任务调度程序一起工作,即使将任务设置为在具有管理员访问权限的帐户下运行

谢谢,