[英]How to properly run a powershell script using VBA in a task scheduler?
我无法通过任务调度程序运行使用 vba 的 ps1 脚本。 我有几个脚本,当我手动执行它们时,一切正常。 通过任务调度程序运行它们,我收到一条消息,表明脚本已成功执行(0x0)。 然而,实际上,什么都没有发生。 我按如下方式运行脚本:
powershell.exe -命令“。C:\scripts\script.ps1;退出$LASTEXITCODE”
示例脚本:
$files = Get-ChildItem -Recurse -Directory "Z:\teamdata\icpl.qlt\Encoder Reports\ENCODER_LAM"
$fileToCheck = "raport.xlsx"
$xlsxToCheck = "*.xlsx"
foreach ($f in $files){
$DirToTest = Join-Path $f.fullname $fileToCheck
$xlsxToTest = Join-Path $f.fullname $xlsxToCheck
if (Test-Path $DirToTest -PathType leaf)
{
#Write-Output "exist"
}
else
{
if(Test-Path $xlsxToTest -PathType leaf){
$ExcelObject=New-Object -ComObject excel.application
$ExcelObject.visible=$true
$ExcelFiles=Get-ChildItem -Path $f.fullname
$Workbook=$ExcelObject.Workbooks.add()
$Worksheet=$Workbook.Sheets.Item("Arkusz1")
foreach($ExcelFile in $ExcelFiles){
$Everyexcel=$ExcelObject.Workbooks.Open($ExcelFile.FullName)
$Everysheet=$Everyexcel.sheets.item(1)
$Everysheet.Copy($Worksheet)
$Everyexcel.Close()
}
$savedir = $f.fullname + '\raport.xlsx'
$Workbook.SaveAs($savedir)
$ExcelObject.Quit()
taskkill /f /im excel.exe
}
}
}
感谢您的建议,我设法从任务调度程序运行脚本。 我将映射驱动器更改为 UNC 路径。
我在任务调度程序中运行 powershell
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
与以下 arguments
-NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "C:\script.ps1"
但现在我对脚本的 vba 部分有疑问:
$data = @(
[PSCustomObject]@{
Name = "User_A"
id = 1198
},
[PSCustomObject]@{
Name = "User_B"
id = 1156
},
)
$Path = "\\xyz\xyz\xyz\xyz\test_encoder_reports\ENCODER_2"
$files = Get-ChildItem -Path "\\xyz\xyz\xyz\xyz\test_encoder_reports\ENCODER_2" -Filter '*.xlsx' -ErrorAction 'SilentlyContinue'
foreach ($f in $files){
$var = $f.Name
#getting user id's and kit number of process
$ids = $var.substring(0, 4)
$kitnbr = $var.substring(4, 4)
$sum = 0
$extn = [IO.Path]::GetExtension($f)
if ($extn -eq ".xlsx")
{
$cr_date = $var.substring(24, 10);
$manuf_data=$cr_date
$create new directory
$new_dir = New-Item $Path\$manuf_data -ItemType Directory -Force
$ExcelPath = $f.FullName
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook = $excel.Workbooks.Open($ExcelPath)
#insert data into table header
$workbook.ActiveSheet.Cells.Item(1,13) = 'DATA UTWORZENIA'
$workbook.ActiveSheet.Cells.Item(1,14) = 'Operator'
$workbook.ActiveSheet.Cells.Item(1,15) = 'Nr Kita'
#insert date of manufacture
$workbook.ActiveSheet.Cells.Item(2,13) = $manuf_data
foreach($i in $data){
if($ids -eq $data[$sum].id){
#insert username
$workbook.ActiveSheet.Cells.Item(2,14) = $data[$sum].name
#insert kit number
$workbook.ActiveSheet.Cells.Item(2,15) = $kitnbr
}
$sum++
}
$workbook = $Excel.ActiveWorkbook.Save()
$workbook = $excel.Workbooks.Close()
taskkill /f /im excel.exe
#move excel file to datacatalog
Move-Item -Path "\\xyz\xyz\xyz\xyz\test_encoder_reports\ENCODER_2\$f" -Destination "\\xyz\xyz\xyz\xyz\test_encoder_reports\ENCODER_2\$manuf_data\$f"
}
}
我移动文件的代码的最后一行正确执行,但是前面初始化 excel 的行不起作用,当然当我手动启用脚本时一切正常。
我尝试在任务调度程序中使用登录到 o365 的域用户进行授权,但 excel 仍然无法打开。
我应该将代码的 excel 部分移动到一个单独的.vbs 脚本并在任务调度程序中运行它吗?
"C:\Windows\System32\cscript.exe"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.