script to check member in RDP and Admin group

  • Thread starter Thread starter sks198117
  • Start date Start date
S

sks198117

Guest
$errorlogFile = "E:\DBA\error.log"
$outputFile = "E:\DBA\Report.csv"
$serverList ="E:\DBA\servers.txt"

$memberName = "dosman"

if(Test-Path $errorlogFile){Remove-Item $errorlogFile}

function is-Member {
[OutputType([bool])]
param ([parameter(Mandatory=$true)][string]$strcomputer,
[parameter(Mandatory=$true)][string]$memberName,
[parameter(Mandatory=$true)][string]$groupName)
$group= [ADSI]"WinNT://$strcomputer/$groupName"

$alladmins=@($group.Invoke("Members")) |%{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
if($alladmins -contains $memberName)
{return $true}
else
{return $false}

}
$result = @()

foreach($server in GC $serverList)
{
if (Test-Connection -ComputerName $server -Count 2 -ErrorAction SilentlyContinue)
{
$isAdminMember = $false
$isRDPMember = $false
$isRDPPortEnabled = $false

if(is-Member $server $memberName "Administrators") {$isAdminMember = $true}
if(is-Member $server $memberName "Remote Desktop Users") {$isRDPMember = $true}
$socket = New-Object Net.Sockets.TcpClient($server, 3389);
if($socket.Connected)
{
$isRDPPortEnabled = $true
$socket.Close()
}

$result += New-Object psObject -Property @{
Server = $server
MemberName = $memberName
isAdminMember = $isAdminMember
isRDPMember=$isRDPMember
isRDPPortEnabled=$isRDPPortEnabled -join ";"
}


}
else {Add-Content $errorlogFile "$server is not accessible"}
}

$result |Export-Csv $outputFile -NoTypeInformation

Continue reading...
 
Back
Top