开发者

Can this be made to work on a remote computer?

I've been killing myself trying to get this to work on a remote computer, is it even possible? If so, can someone point me in the right direction?

Here's the code:

    Function Lock-WorkStation { 
        #Requires -Version 2.0 
        $signature = @" 
            [DllImport("user32.dll", SetLastError = true)] 
            public static extern bool LockW开发者_运维技巧orkStation(); 
        "@ 

        $LockWorkStation = Add-Type -memberDefinition $signature -name "Win32LockWorkStation" -namespace Win32Functions -passthru 
        $LockWorkStation::LockWorkStation() | Out-Null 
    }


I can't test here, but for me it can NOT work because, as you can read in Microsoft documentation, the LockWorkStation function is callable only by processes running on the interactive desktop. In addition, the user must be logged on.

So when you connect to a remote computer using PSSession as far as I understand you are not in the interactive session.


Nothing to do with this, but it can help in Windows Vista/7 2008/R2, you can use the command tsdiscon.exe to lock a Remote Desktop session or your workstation.

Here is a sample where, logged as adminstrator domain on my computer, I first list, then lock the console session on my server.

PS> query session /server:WM2008R2ENT
 SESSION           UTILISATEUR              ID  ÉTAT    TYPE        PÉRIPHÉRIQUE
 services                                    0  Déco
 console           jpb                       2  Actif

PS> tsdiscon 2 /server:WM2008R2ENT


It's possible. But you need a workaround to connect to the interactive session.

Download the PowerShellPack and install it. You only need one module called "TaskScheduler".

I've tested the following code:

Function Lock-Workstation
{
param(
$Computername,
$Credential
)
    if(!(get-module taskscheduler)){Import-Module TaskScheduler}
    New-task -ComputerName $Computername -credential:$Credential | 
    Add-TaskTrigger -In (New-TimeSpan -Seconds 30) |
    Add-TaskAction -Script `
    { 
    $signature = @" 
    [DllImport("user32.dll", SetLastError = true)] 
    public static extern bool LockWorkStation(); 
"@ 
    $LockWorkStation = Add-Type -memberDefinition $signature `
                                -name "Win32LockWorkStation" `
                                -namespace Win32Functions `
                                -passthru 
    $LockWorkStation::LockWorkStation() | Out-Null
    } | Register-ScheduledTask TestTask -ComputerName $Computername `
                                        -credential:$Credential
}

You can use it like this:

Lock-Workstation "NameOfTheComputer" (Get-Credential)

or like this:

Lock-Workstation "NameOfTheComputer"

If you receive an error in Connect-ToTaskScheduler when specifying a credential, it's because there is a typo in the module (edit Connect-ToTaskScheduler.ps1 and replace "$NetworkCredentail.Domain," with "$NetworkCredential.Domain,"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜