I’ve got a pretty simple query to pull back the last logon date for every account that has logged into a server. It works great on any server that I remote to and run it on but when I try to feed a list of servers to it, it comes back with empty values for almost all of the properties under the Wmi_NetworkLoginProfile class. I have domain admin access on the account I’m using and remote is enabled on all the servers. DCOM looks correct as well. If I remote to any of the servers in my server list and run it locally, it returns all the values with no issue. It just won’t return them when I run it for a list of all the servers. I have over 6000 servers that are a variety of Windows versions but all are at least 2008 or later. Help! I am relatively new to PowerShell but this seems like it would be a permissions issue but I don’t know what other permissions I would need to pull this. Thanks for the help!!!
#Set workstation list file (get list of workstations from file)
$list = Get-Content "C:\Users\A-BLBURKHA\scripts\ServerList.txt"
#Set Output file
$OUTPUT = "C:\Users\A-BLBURKHA\scripts\Server_Logged_On_Users.csv"
#set headers in CSV output file
echo "ServerName,User,LogonTime" | Out-File -FilePath $OUTPUT
#begin script logic
$list | ForEach-Object {
echo "Current computer $_"
#Test if system is online
Invoke-Expression "ping -n 1 -w 500 $_" | Out-Null
$ping = $LASTEXITCODE
#If Pingable perform the following collection actions
if (!$ping){
#Retrieve listings of ALL users that have logged on to the server and send results to Output File
$NetLogs = Get-WmiObject Win32_NetworkLoginProfile -ComputerName $_
foreach ($NetLog in $NetLogs) {
if ($NetLog.UserType -eq "Normal Account" -AND $NetLog.LastLogon -match "(\d{14})") {
$row = "" | Select Name,LogonTime
$row.Name = $NetLog.Name
$row.LogonTime=[datetime]::ParseExact($matches[0], "yyyyMMddHHmmss", $null)
foreach ($RName in $row) {
$Name = $row.Name
$LTime = $row.LogonTime
#Write "ServerName, Name, and TimeStamp" results to Output File
echo "$_,$Name,$LTime" >> $OUTPUT }
}
}
} else {
#If System is not pingable, the System Name is written to the NotPingable.csv file
echo "$_ not pingable"
echo "$_" >> NotPingable.csv
}
}