从任务计划程序启动时,VBScript挂起

时间:2018-06-28 06:28:23

标签: windows vbscript scheduled-tasks

如果我手动运行VBScript或使用Task Scheduler从当前登录的用户运行它,我的VBScript都可以工作,但是如果我设置“无论是否登录用户都可以运行”,它就无法工作。

我正在从cmd文件运行vbs脚本。 Cmd和vbs都位于本地计算机上,我从那里在同一共享文件夹中运行任务(我不使用本地路径,因为它包含空格,而且我对vbs语法和Chr(34)不太熟悉,所以我决定使用共享文件夹(\\server\1$)的网络路径

这是批处理文件(1.cmd):

pushd \\server\1$\
wscript.exe "zISSsend.vbs"
popd

这是zISSsend.vbs(它执行程序cons.exe,存档文件并将其发送到电子邮件):

Option Explicit
Const SMTP_Subj = "tt"
Const Arch_Name = "tt"
Const Cons_Path = "\\server\1$\"

Dim fso, fn, f, ff(), cnt, WshShell
Set WshShell = CreateObject("WScript.Shell")
set fso=createobject("Scripting.FileSystemObject")
WshShell.CurrentDirectory = Cons_Path

WshShell.Run Cons_Path & "cons.exe /usr", 2, True

WshShell.Run "zISS.DAT\pkcb.exe a -y -r -ep -ibck -AG{-}YYYYMMDD{-}HH zISS.DAT\" & Arch_Name & ".zip " & Cons_Path & "\ADM\STS\*.stt", 2, true
WshShell.Run "zISS.DAT\pkcb.exe a -y -r -ep -ibck -AG{-}YYYYMMDD{-}HH zISS.DAT\" & Arch_Name & ".zip " & Cons_Path & "\RECEIVE\*.usr", 2, true

Const SMTP_Host = "smtp.gmail.com"
Const SMTP_Port = 465
Const SMTP_From = "xx@gmail.com"
Const SMTP_Text = "xx"
Const SSAP_INDX = "xx"
Const SMTP_ToPP = "xxx@gmail.com"
Const cdoSendUsingPort = 2

fn = fso.buildpath(Cons_Path, "zISS.DAT")
Set f = fso.GetFolder(fn).Files
For Each fn In f
    If LCase(fso.GetExtensionName(fn)) = "zip" Then
        AddToArray fn, ff, cnt
    End If
Next

SendMessage SMTP_ToPP, SMTP_Subj, SMTP_Text, ff

Sub SendMessage(Recipient, Subj, Text, Attachments)
    Dim msg,fn
    Set msg = WScript.CreateObject("CDO.Message")
    With msg
        .From = SMTP_From
        .To = Recipient
        .Subject = Subj
        .TextBody = text
        .Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
        .Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTP_Host
        .Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTP_Port
        .Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        .Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = SMTP_From
        .Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = SSAP_INDX
        .Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
        .Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 120
        .Configuration.Fields.Update
        .MimeFormatted = True
        For Each fn In attachments
            .addattachment fn
        Next
        .Send
    End With
End Sub

Sub AddToArray(Itm, Arr, Count)
    Count = Count + 1
    ReDim Preserve Arr(Count-1)
    Arr(Count-1) = Itm
End Sub

在“任务计划程序”中运行脚本的用户是服务器admin,并且当然具有共享文件夹的权限。我也以最高特权运行它。在“操作”栏中,指定cmd文件(\\server\1$\1.cmd)的路径。

重申一下,当我手动运行它时它可以工作,当我从当前登录的用户使用Task Scheduler运行它时它可以工作,但是当我检查“运行是否登录了用户”并使它运行时它不起作用与同一用户一起运行。 结果,我挂起了wscript.exe进程。

我决定从vbs文件中获取一些日志并添加

Dim fso1
Set fso1 = WScript.CreateObject("Scripting.Filesystemobject")
Set f = fso1.CreateTextFile("SCRIPTLOG.txt", 2)

在每个块之后加上f.WriteLine。有了这个,我发现脚本就挂了

WshShell.Run Cons_Path & "cons.exe /usr", 2, True

0 个答案:

没有答案