Windows 7上的Powershell计划任务在管理员帐户未登录时运行

时间:2017-10-03 17:01:15

标签: powershell windows-7 schtasks.exe

我正在编写一个powershell函数,该函数在服务器上运行,以便使用schtasks.exe安排powershell脚本在每台Windows 7客户端计算机上运行。

如果我没有提供用户名和密码,则只有在客户端以管理员身份登录时才会运行脚本。

如果我确实提供了用户名和密码,我会收到两个错误: (1)系统找不到指定的文件 (2)指定的登录会话不存在。它可能已经被终止。

即使未记录管理员帐户,如何使预定作业正常运行?

谢谢!

function scheduleRemoteScript($computerList) {
    foreach ($computer in $computerList) {            
        $ts = New-TimeSpan -Minutes 1
        $currentDateTime = (get-date) + $ts
        $hour = $currentDateTime.Hour.ToString()
        if ($hour.Length -eq 1) {
            $hour = '0' + $hour
        }
        $minute = $currentDateTime.Minute.ToString()
        if ($minute.Length -eq 1) {
            $minute = '0' + $minute
        }
        $timeString = $hour + ":" + $minute
        schtasks.exe /Delete /TN LocalScript /S $computer /F #delete the task in case it aleady exists.

        #this runs when logged in with admin account, but not user account.
        #schtasks.exe /create /TN LocalScript /S $computer /ST $timeString /SC ONCE /TR "powershell.exe -file C:\localScript.ps1" 

        #this gives two errors: (1) system cannot find the file specified  (2) a specified logon session does not exist.  It may already have been terminated.
        schtasks.exe /create /TN LocalScript /S $computer /ST $timeString /SC ONCE /TR "powershell.exe -file C:\localScript.ps1" /RU 'admin-account' /RP 'somePassword'         
    }
}

0 个答案:

没有答案