我一直在努力让自己在远程计算机上工作,甚至可能吗?如果是这样,有人能指出我正确的方向吗?
以下是代码:
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
}
答案 0 :(得分:1)
我无法在此测试,但对我来说它无法正常工作,因为您可以在Microsoft documentation中阅读,LockWorkStation
函数只能由交互式桌面上运行的进程调用。此外,用户必须登录。
因此,据我所知,当您使用PSSession连接到远程计算机时,您不在交互式会话中。
答案 1 :(得分: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并替换“$ NetworkCredentail.Domain”, “$ NetworkCredential.Domain”
答案 2 :(得分:1)
与此无关,但它可以在Windows Vista / 7 2008 / R2中提供帮助,您可以使用命令tsdiscon.exe
锁定远程桌面会话或工作站。
这是一个示例,在我的计算机上记录为管理员域,我首先列出,然后在我的服务器上锁定控制台会话。
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