opexxx
2/10/2017 - 11:47 PM

Email list of logged in users

Email list of logged in users

#get usernames and email
#from http://www.reddit.com/r/PowerShell/comments/2acnqh/computerlist_filter_by_user/
$MachineList = Get-Content -Path H:\ListOfMachines.txt; # One system name per line
foreach ($machine in $MachineList){
    $params = @{'ComputerName'=$machine;
                'Namespace'='root\cimv2';
                'Class'='Win32_ComputerSystem';
                'ErrorAction'='SilentlyContinue'
               }
    $user = (Get-WmiObject @params).UserName
    if ($user -notlike 'domain\*')
    {
        $machine + ": " + $user | Out-File loggedinusers.txt -Append
    }    
}
$mail = @{'to'='recipient@email.com';
          'from'='from@email.com';
          'smtpserver'='mail.email.com';
          'subject'='Logged in users';
          'body'=(Get-Content -Path loggedinusers.txt | Out-String)
         }
Send-MailMessage @mail