Powershell script creates empty zip file when run automatically through task scheduler, but works when run manually

五迷三道 提交于 2019-12-25 03:47:20

问题


Here is the full text of my script, with the business sensitive data filed off and replaced with generic names for things.

function Add-Zip
{
    param([string]$zipfilename)

    if(-not (test-path($zipfilename)))
    {
        set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
        (dir $zipfilename).IsReadOnly = $false  
    }

    $shellApplication = new-object -com shell.application
    $zipPackage = $shellApplication.NameSpace($zipfilename)

    foreach($file in $input) 
    { 
            $zipPackage.CopyHere($file.FullName)
            Start-sleep -milliseconds 500
    }
}

$emailFrom = "server@server.com"
$emailTo = "supportgroup@server.com"
$emailSubject = "Title of script"
$smtpServer = "smtpserver"
$emailBody = "Script: C:\pathtoscript\script.ps1`r`n`r`n"

Try {
    $sourcepath = '\\remotepath1\'
    $destpath = '\\remotepath2\'
    $sourcefiles = $($sourcepath + '*.txt')
    $es = ""
    [regex]$regex = '(Rr)egex\.txt'
    $gci = (Get-ChildItem $sourcefiles | sort LastWriteTime | select -last 1)
    $txtfile = $regex.Matches($gci) | foreach-object {$_.Value}
    $oldtxtfile = Get-Content 'C:\pathtoscript\script.cfg'
    if ($txtfile -eq $oldtxtfile) {
        throw "No new file found."
    }
    echo $txtfile > 'C:\pathtoscript\script.cfg'
    $txt2file = $txtfile.Replace('.txt', '.txt2')
    $zipname = $txtfile.Replace('.txt', '.zip')
    $txtpath = $($sourcepath + $txtfile)
    $txt2path = $($destpath + $txt2file)
    $zippath = $($sourcepath + $zipname)
    get-childitem $txtpath | Add-Zip $zippath
    if ((get-item $zippath).length -lt 5kb) {
        throw $zippath + " is empty."
    }
    $emailBody = "Successfully zipped:`r`n" + $txtpath + " into " + $zippath + "`r`n`r`n"
    move-item $zippath $destpath
    $emailBody = $emailBody + "Successfully moved:`r`n" + $zippath + " to: " + $destpath + "`r`n`r`n"
    echo $es > $txt2path
    $emailSubject = $emailSubject + ' [Success]'
    $emailBody = $emailBody + "Successfully created:`r`n" + $cmppath
}
Catch [system.exception]{
    $emailSubject = $emailSubject + " [Failure]"
    $emailBody = "`r`n*****Exception Caught*****`r`n" + $_.Exception.ToString()
    $emailTo = "ticket@server.com"
}
Finally {
    $emailBody = $emailBody + "`r`n`r`nEnd: " + (Get-Date -format G)

    Send-MailMessage -from $emailFrom -to $emailTo -subject $emailSubject -body $emailBody -smtpserver $smtpServer
    exit
}

the idea here is that this script runs every monday, to pick up a file that is uploaded to a server via ftp on sunday, zip it, and create an empty companion file to get dropped in a different location where they are picked up by a different automated process. The script runs flawlessly when it is run manually, however, I currently have it set to run every monday in the am hours in the Windows Task Scheduler--the script runs, but will throw the empty zip exception located on line 47. If I run the script manually (which requires deleting the contents of the cfg files) it works with no problem.

I am not sure what information about the task scheduler is important, so here is some basic information, if you need more, please ask me.

In the General tab of the properties: it is set to Run whether user is logged on or not. It is set to Run with highest privileges.

For triggers, it runs weekly every monday during am

the action is to launch powershell.exe with the full script path as the argument

for conditions, it is set to: Stop if the computer ceases to be idle, Start the task only if the computer is on AC power, Stop if the computer switches to battery power

settings say: allow task to be run on demand, stop the task if it runs longer than 1 hour, if the running task does not end when requested, force it to stop

history shows the task has been executing properly.

来源:https://stackoverflow.com/questions/25243915/powershell-script-creates-empty-zip-file-when-run-automatically-through-task-sch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!