首页 > 解决方案 > 在 vbScript 中安排要在(任何用户)登录时执行的任务

问题描述

好吧,所以我一直在寻找这个问题的答案,虽然我找到了与这个问题类似的例子,但它们似乎对我不起作用。我正在尝试安排一个任务来在用户登录时终止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 上运行这一事实是否重要?或者有没有更简单的方法可以通过我忽略的代码来做到这一点。请尽快帮忙!谢谢!

标签: vbscriptscheduled-tasksscheduling

解决方案


谢谢,但是我已经解决了这个问题,只需在执行 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

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


推荐阅读