2011-05-18 33 views
3

我一直在試圖讓自己在遠程計算機上工作,這是可能的嗎?如果是這樣,有人能指出我的方向嗎?這可以在遠程計算機上工作嗎?

下面的代碼:

Function Lock-WorkStation { 
     #Requires -Version 2.0 
     $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 
    } 

回答

1

我無法測試在這裏,但我不能因爲工作,因爲你可以在Microsoft documentation讀取時,LockWorkStation功能僅通過在交互運行的進程調用桌面。另外,用戶必須登錄。

因此,當您使用PSSession連接到遠程計算機時,據我所知您不在交互式會話中。

+0

是的,PSremoting隔離遠程會話。 – JoeG 2011-05-18 22:16:17

1

這是可能的。但是您需要解決方法才能連接到交互式會話。

下載PowerShellPack並進行安裝。您只需要一個名爲「TaskScheduler」的模塊。

我測試過下面的代碼:

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 
} 

您可以使用它像這樣:

Lock-Workstation "NameOfTheComputer" (Get-Credential) 

或像這樣:

Lock-Workstation "NameOfTheComputer" 

如果您在收到錯誤Connect-ToTaskScheduler指定憑證時,這是因爲模塊中存在拼寫錯誤(編輯Connect-ToTaskScheduler.ps1和repl王牌「$ NetworkCredentail.Domain,」「$ NetworkCredential.Domain,」

+0

有兩個問題:1)您是否在運行Vista或高端系統的遠程計算機上測試它? 2)你真的用一個不同的用戶登錄遠程機器來測試它嗎?這意味着管理員可以使用taskscheduler在交互式桌面中注入代碼。 – JPBlanc 2011-05-18 16:27:02

+0

1)我將它從Windows 7工作站運行到Windows 2008 R2服務器。 2)兩臺電腦都有相同的用戶登錄。 – Winfred 2011-05-18 17:10:04

+0

要使用不同的憑證遠程運行它,您可以指定-creatential to new-task和register-scheduledtask。 – Winfred 2011-05-18 17:17:59

1

沒有做這一點,但它可以在Windows Vista幫助/ 7 2008/R2,您可以使用命令tsdiscon.exe到鎖定遠程桌面會話或工作站。

下面是一個示例,其中,我的計算機上記錄爲adminstrator域,我首先列出,然後鎖定我的服務器上的控制檯會話。

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 
+0

這甚至適用於Windows 10:] – mousio 2015-11-08 22:57:12