在vbScript

时间:2018-06-06 12:33:32

标签: vbscript scheduled-tasks scheduling

好吧所以我一直在寻找答案,虽然我发现了这个问题的相似例子,但它们似乎对我没用。我正在尝试安排一个任务,当用户登录时杀死Skype,以便人们不必每次登录时都关闭。我必须通过编写代码而不是手动执行此操作,因为它将是用于设置新计算机的脚本。这是我编写任务的代码:

请注意,评论的行来自我尝试的解决方案,但未按照我希望的方式工作

Option Explicit

Dim wShell, outFile, objFSO, ret

Set wShell = CreateObject ("Wscript.Shell") 
wShell.Run "N:\Internfolder\INCA_Scripts\SkypeKill.vbs"
'wShell.Run "cmd start ""N:\Internfolder\INCA_Scripts\Schedule_SkypeKill.bat"""
'wShell.Run "N:\Internfolder\INCA_Scripts\Schedule_SkypeKill.bat"
wShell.Run "SCHTASKS /create  /tn ""Schedule_Kill_Skype""  /tr ""N:\Internfolder\INCA_Scripts\SkypeKill.vbs"" /sc onlogon", 0   

'set wShell = Nothing
'WScript.sleep(15000)
WScript.echo "completed"

这是SkypeKill的.vbs文件:

Dim oShell : Set oShell = CreateObject("WScript.Shell")
dim timer
timer = 0

Do
    'attempt to kill skype (lync.exe) 
    r = oShell.Run("taskkill /F /IM lync.exe",0, True)

    'if r = 0, then skyp was opened, then killed
    if r= 0 then
        WScript.echo "Skype was opened, but has been termiated. Hit 'Enter' to exit."
        WScript.quit

    'else, we wait until it is opened for 35 seconds, and kill it if it appears
    else
        WScript.sleep(3000)
        timer = timer + 3
        if timer = 35 Then Exit Do
    end if
Loop

WScript.echo "Skype kill has been attempted"

最后一点重要信息是,当我安排任务运行此脚本时,它表示系统无法找到指定的文件,但SkypeKill.vbs文件位于指定位置(N:\ Internfolder \ INCA_Scripts) \ SkypeKill.vbs),这有点奇怪。

所以我有几个问题。我假设我试图安排任务的方式有问题,因为当我运行第一个代码块时它不会出现在任务调度程序中。但是,两个代码都没有抛出任何错误。如何在用户登录并启动后实际写入任务调度程序并终止skype?在尝试完成此任务时,我在Windows 7和Windows 10上运行的事实是否重要?或者通过我忽略的代码有更简单的方法来做到这一点。请尽快帮忙!谢谢!

1 个答案:

答案 0 :(得分:0)

谢谢,但是我已经通过在执行shell函数之前包含以下代码解决了这个问题:

If WScript.Arguments.Count = 0 Then Set objShell = CreateObject("Shell.Application") objShell.ShellExecute "wscript.exe", "c:\Users\admin\Documents\selfConfigure.vbs -1", "", runas", 1 End If

这提升了我的权限,以便访问我需要访问的驱动器/文件路径,因为我收到了一个"访问被拒绝"命令提示符中的错误。问题已解决,任务已写入调度程序。