0

我正在寻找构建一个 PowerShell 脚本,该脚本将查看本地用户帐户是否已在远程计算机上登录。如果是这样,它将弹出一条消息,说明用户已登录。如果用户未登录,它将打开 mstsc 以便用户可以登录。

我发现下面的代码效果很好,但它似乎只看到域帐户。从那里我不确定如何根据用户是否登录来传递结果并做出响应。

@(Get-WmiObject -ComputerName $machine -Namespace root\cimv2 -Class Win32_ComputerSystem)[0].UserName;

更新了代码。基本上我需要查看 RDP 会话是否正在使用本地用户帐户。那有意义吗?

$machine = "ServerNameHere"
$temp1 = "C:\temp\user.txt"
$Word = "JDoe"

# The below command will connect to the server and see if user bouair is currently logged in

@(Get-WmiObject -ComputerName $machine -Namespace root\cimv2 -Class Win32_ComputerSystem)[0].UserName | Out-File $temp1 -Append


If((Get-Content $temp1).Contains($Word))
{
    [system.windows.forms.messagebox]::Show("another user is already logged in!");
}
else {
    .\mstsc.exe -v $machine
}

Remove-Item $temp1
exit

Get-WmiObject 命令的问题之一是它没有在我的服务器上拉取 RDP 会话。然后我遇到了一个博客,用户使用 quser 来拉取所有用户,然后我修改了代码以在我的环境中工作。此代码非常适合我们的团队,其他人可能会从中受益。下一步是将空闲时间和状态拉到消息块中,但那是另一天。

param( $ComputerName = 'ServerNameNere' )

process {
    $File1 = "C:\temp\user.txt"
    $word = "UserNameHere"
    Remove-Item $File1
    foreach ($Computer in $ComputerName) {
        quser /server:$Computer | Select-Object -Skip 1 | ForEach-Object {
            $CurrentLine = $_.Trim() -Replace '\s+',' ' -Split '\s'
            $HashProps = @{
                UserName = $CurrentLine[0] | Out-File $file1 -Append
                ComputerName = $Computer | Out-File $file1 -Append
            }

            if ($CurrentLine[2] -eq 'Disc') {

                $HashProps.SessionName = $null | Out-File $file1 -Append
                $HashProps.Id = $CurrentLine[1] | Out-File $file1 -Append
                $HashProps.State = $CurrentLine[2] | Out-File $file1 -Append
                $HashProps.IdleTime = $CurrentLine[3] | Out-File $file1 -Append
                $HashProps.LogonTime = $CurrentLine[4..6] -join ' ' | Out-File $file1 -Append
            }
            else {

                $HashProps.SessionName = $CurrentLine[1] | Out-File $file1 -Append
                $HashProps.Id = $CurrentLine[2] | Out-File $file1 -Append
                $HashProps.State = $CurrentLine[3] | Out-File $file1 -Append
                $HashProps.IdleTime = $CurrentLine[4] | Out-File $file1 -Append
                $HashProps.LogonTime = $CurrentLine[5..7] -join ' ' | Out-File $file1 -Append
            }

            New-Object -TypeName PSCustomObject -Property $HashProps |
            Select-Object -Property UserName,ComputerName,SessionName,Id,State,IdleTime,LogonTime
        }
    }

    If((Get-Content $file1).Contains($Word))
    {
        [system.windows.forms.messagebox]::Show("another user is already logged in!");
    }
    else {
        mstsc.exe -v $machine
    }
}
4

2 回答 2

0
$machine = "mycomputer"

$user = @(Get-WmiObject -ComputerName $machine -Namespace root\cimv2 -Class Win32_ComputerSystem)

if ($user.Length -gt 0) {
    "Current user is $($user[0].UserName)"
    } else {
    .\mstsc.exe -v $machine
    }
于 2014-05-21T17:52:46.947 回答
0

所以我使用了你所拥有的东西,并删除了一些并不真正需要的东西。我展开 UserName 列表并将其分配给 $Users,因此我只有一个用户名数组。然后我检查所需的帐户是否与其中任何一个匹配,如果是,我们会弹出消息框,如果不是,我们启动 MSTSC。

$machine = "ServerNameHere"

# the below command will connect to the server and see all users currently logged in
$Users = Get-WmiObject -ComputerName $machine -Namespace root\cimv2 -Class Win32_ComputerSystem | Select -Expand Username

#This will check if bouair is one of those logged in users.
If($Users -Match "bouair"){
    [system.windows.forms.messagebox]::Show("another user is already logged in!")
}
else { #If it is not, start MSTSC to establish a remote session.
    .\mstsc.exe -v $machine
}

奖励功能:我喜欢手头有一个简单的功能,以防我需要弹出一个消息框:

Function Show-MsgBox ($Text=$(Throw "You must supply the text for the message box."),$Title,[Windows.Forms.MessageBoxButtons]$Button = "OK",[Windows.Forms.MessageBoxIcon]$Icon = "Information"){
[Windows.Forms.MessageBox]::Show("$Text", "$Title", [Windows.Forms.MessageBoxButtons]::$Button, [Windows.Forms.MessageBoxIcon]::Information) | ?{(!($_ -eq "OK"))}
}

然后你可以这样做Show-MsgBox "Another user is logged on!",它会弹出来。如果需要,您还可以指定窗口标题,并根据需要指定哪些按钮或图标(键入 -Button-Icon将 IntelliSense 您的选项,并且该参数可以使用选项卡完成)。

于 2014-05-21T19:17:00.213 回答