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 コマンドに関する私の問題の 1 つは、サーバーで 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

だから私はあなたが持っていたものを使って作業し、本当に必要のないものをいくつか削除しました. 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-IconIntelliSense を使用すると、そのパラメーターでタブ補完が使用可能になります)。

于 2014-05-21T19:17:00.213 に答える
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 に答える