可以使其在远程计算机上工作吗?

发布于 2024-11-08 03:08:26 字数 504 浏览 0 评论 0 原文

我一直在试图让它在远程计算机上工作,这可能吗?如果是这样,有人可以指出我正确的方向吗?

这是代码:

    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 
    }

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 LockWorkStation(); 
        "@ 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江湖正好 2024-11-15 03:08:27

与此无关,但它可以在 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

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
阳光的暖冬 2024-11-15 03:08:27

这是有可能的。但您需要一种解决方法来连接到交互式会话。

下载 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,"

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,"

同尘 2024-11-15 03:08:26

我无法在这里进行测试,但对我来说它无法工作,因为正如您可以在 Microsoft 文档中,LockWorkStation 函数只能由运行在交互式桌面上的进程调用。此外,用户必须登录。

因此,据我所知,当您使用 PSSession 连接到远程计算机时,您并不处于交互式会话中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文