ローカル ユーザー アカウントがリモート マシンにログインしているかどうかを確認する 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
}
}