即使系统被锁定,也会在后台运行powershell脚本

时间:2017-07-21 05:27:27

标签: powershell scripting

当系统被锁定时,powershell脚本会暂停其执行。一旦我们解锁系统,脚本就会继续执行。

有没有办法可以继续执行powershell脚本,即使系统被锁定了?

打开资源管理器后,脚本会单击本地系统中的特定点。然后它打开远程桌面连接,然后再次单击远程系统中的特定点。此行为处于循环中,因此它模拟了与本地系统中远程系统的人工交互。

我们的最终目标是避免我们在远程桌面连接中的15分钟超时限制,并且由于不活动,服务器会在几小时后重新启动。

Powershell脚本:

 $cSource = @'
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class Clicker
{
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646270(v=vs.85).aspx
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{ 
    public int        type; // 0 = INPUT_MOUSE,
                            // 1 = INPUT_KEYBOARD
                            // 2 = INPUT_HARDWARE
    public MOUSEINPUT mi;
}

//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646273(v=vs.85).aspx
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
    public int    dx ;
    public int    dy ;
    public int    mouseData ;
    public int    dwFlags;
    public int    time;
    public IntPtr dwExtraInfo;
}

//This covers most use cases although complex mice may have additional buttons
//There are additional constants you can use for those cases, see the msdn page
const int MOUSEEVENTF_MOVED      = 0x0001 ;
const int MOUSEEVENTF_LEFTDOWN   = 0x0002 ;
const int MOUSEEVENTF_LEFTUP     = 0x0004 ;
const int MOUSEEVENTF_RIGHTDOWN  = 0x0008 ;
const int MOUSEEVENTF_RIGHTUP    = 0x0010 ;
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020 ;
const int MOUSEEVENTF_MIDDLEUP   = 0x0040 ;
const int MOUSEEVENTF_WHEEL      = 0x0080 ;
const int MOUSEEVENTF_XDOWN      = 0x0100 ;
const int MOUSEEVENTF_XUP        = 0x0200 ;
const int MOUSEEVENTF_ABSOLUTE   = 0x8000 ;

const int screen_length = 0x10000 ;

//https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

public static void LeftClickAtPoint(int x, int y)
{
    //Move the mouse
    INPUT[] input = new INPUT[3];
    input[0].mi.dx = x*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width);
    input[0].mi.dy = y*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
    input[0].mi.dwFlags = MOUSEEVENTF_MOVED | MOUSEEVENTF_ABSOLUTE;
    //Left mouse button down
    input[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    //Left mouse button up
    input[2].mi.dwFlags = MOUSEEVENTF_LEFTUP;
    SendInput(3, input, Marshal.SizeOf(input[0]));
}
}
'@
Add-Type -TypeDefinition $cSource -ReferencedAssemblies System.Windows.Forms,System.Drawing
Add-Type -AssemblyName microsoft.VisualBasic
#Send a click at a specified point
$current_date = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$LogFile = 'I:\'+'click_activity_'+$current_date+".txt"
for($i=1; $i -lt 1440; $i = $i + 1)
        {
        Write-Host "the current minute number is $i `n " 
        Write-Host "press ctrl+ C to stop execution `n "
        explorer .
        Start-Sleep -Seconds 2
        [System.Windows.Forms.SendKeys]::SendWait("% x");
        [Clicker]::LeftClickAtPoint(64,233)
        Start-Sleep -Seconds 3
        $current_time = Get-Date        
        Add-Content $LogFile "left click on 64,233 by $current_time and minute number is $i"
        [Clicker]::LeftClickAtPoint(64,258)     
        explorer .
        Start-Sleep -Seconds 2
        [System.Windows.Forms.SendKeys]::SendWait("% x");
        Start-Sleep -Seconds 3
        $current_time = Get-Date        
        Add-Content $LogFile  "left click on 64,433 by $current_time and minute number is $i"
        $close_explorer = (New-Object -comObject Shell.Application).Windows() |
         ? { $_.FullName -ne $null} |
         ? { $_.FullName.toLower().Endswith('\explorer.exe') } 

         $close_explorer | % {  $_.Quit() }
         Start-Sleep -Seconds 2
         [Microsoft.VisualBasic.Interaction]::AppActivate("Remote Desktop Connection")
         Start-Sleep -Seconds 2
         [Clicker]::LeftClickAtPoint(64,233)
         Start-Sleep -Seconds 3
         [Clicker]::LeftClickAtPoint(64,258)    
         Start-Sleep -Seconds 2
         #[system.diagnostics.process]::Start()
        }

0 个答案:

没有答案