{"id":4868,"date":"2023-09-12T13:01:44","date_gmt":"2023-09-12T11:01:44","guid":{"rendered":"https:\/\/leeryanrs.com\/?p=4868"},"modified":"2023-09-12T13:01:44","modified_gmt":"2023-09-12T11:01:44","slug":"list-user-logon-sessions-on-all-servers-in-ad-domain","status":"publish","type":"post","link":"https:\/\/leeryanrs.com\/?p=4868","title":{"rendered":"List user logon sessions on all servers in AD domain"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># Define the OU path where your servers are located\n$ouPath = \"OU=servers,DC=domain,DC=local\"  # Replace with your OU path\n\n# Get a list of servers in the specified OU\n$servers = Get-ADComputer -Filter {OperatingSystem -like \"Windows Server*\"} -SearchBase $ouPath | Select-Object -ExpandProperty Name\n\n# Loop through each server and query logged-on users\nforeach ($server in $servers) {\n    try {\n        # Use WMI to query logged-on user information\n        $loggedOnUsers = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $server | Select-Object -ExpandProperty UserName\n\n        if ($loggedOnUsers) {\n            Write-Host \"Logged-on users on $server\"\n            $loggedOnUsers | ForEach-Object {\n                Write-Host \"  $_\"\n            }\n        } else {\n            Write-Host \"No users logged on to $server\"\n        }\n    } catch {\n        Write-Host \"Failed to query $server\"\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>If you want to search by specific names, you can adjust the filter to this &#8211; <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-ADComputer -Filter {Name -like \"AZ-EUW-*\"} -SearchBase $ouPath<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want to search by specific names, you can adjust the filter to this &#8211;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[21,38,51],"class_list":["post-4868","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-active-directory","tag-powershell","tag-windows"],"_links":{"self":[{"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/posts\/4868","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4868"}],"version-history":[{"count":0,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/posts\/4868\/revisions"}],"wp:attachment":[{"href":"https:\/\/leeryanrs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}