我有一个PowerShell脚本,我试图在注销时在远程桌面上工作。到目前为止,我已经能够在本地计算机上成功运行此脚本,并将任务计划程序设置为以下两种安全设置:“仅在用户登录时运行”和“运行用户是否已登录或不“。
一旦我开始工作,我就去了远程桌面并在那里创建了一个预定的任务。当设置为“仅在用户登录时运行”时,脚本按预期运行 - 就像在本地计算机上一样。当我切换到“运行是否登录用户”(选择“以最高权限运行”)时,脚本不再运行。
我没有收到任何错误或消息,表明我的远程桌面上的安全级别是一个问题,所以我不确定这是不是原因,或者如果它是如何更改它。
有谁知道如何解决这个问题?
# collect excel process ids before and after new excel background process is started
$priorExcelProcesses = Get-Process -name "*Excel*" | % { $_.Id }
$Excel = New-Object -ComObject Excel.Application
$postExcelProcesses = Get-Process -name "*Excel*" | % { $_.Id }
#run program
#manually map network drive
if (-Not (Test-Path 'I:\')) {New-PSDrive -Name "I" -PSProvider FileSystem -Root "\\MyServer\MyShare"}
$folderPath = "\\MyServer\MyShare\rest of file path"
$filePath = "MyServer\MyShare\rest of file path\filename.xlsm"
$tempPath = "C:\Users\Public"
$tempFile = "C:\Users\Public\filename.xlsm"
#copy file from I drive to remote desktop
Copy-Item -Path $filePath -Destination $tempPath
#create Excel variables
$excel = new-object -comobject excel.application
$excel.visible = $False
$excelFiles = Get-ChildItem -Path $tempPath -Include *.xls, *xlsm -Recurse
#open excel application and run routine
Foreach($file in $excelFiles)
{
$workbook = $excel.workbooks.open($tempFile)
$worksheet = $workbook.worksheets.item(1)
$excel.Run("getAccessData")
$workbook.save()
$workbook.close()
}
#copy file from remote desktop back onto I drive
Copy-Item -Path $tempFile -Destination $folderPath
# try to gently quit
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
# otherwise allow stop-process to clean up
$postExcelProcesses | ? { $priorExcelProcesses -eq $null -or $priorExcelProcesses -notcontains $_ } | % { Stop-Process -Id $_ }