I’m working a script that looks at a remote machine and tells if if there is a user logged in. The script runs however the output gives me both conditions the syntax is posted below:
[pre]
$machinenames = ‘randomcomputername’
$users = get-wmiobject -class win32_computersystem -computername $machinenames | select -expandproperty username
$users
$result = write-host “no users logged in to $machinename”
if ($users -eq 0) {write-host " $results logged into $machines"} else {write-host “$users logged into $machinenames”}
What you are doing is not correct to give you what you are after. Please be sure to read the help file entirely, especially the parameters and examples.
Not all properties are available by default. So, you must specifically ask for them, and even with that, some things change over time. What you are doing no longer returns anything in the user name.
You need to use the built-in Windows ‘Qwinsta’
or, there’s the built-in Windows command, “query”, located at %SystemRoot%\system32\query.exe
Find the logged on users on a remote system/s
DescriptionThis script should be useful for Helpdesk or other IT Admins to query remote machines to see who is logged on.This could also be useful for Remote Desktop Services or Citrix Admins * Note this scripts also returns non system accounts that are running services.
https://gallery.technet.microsoft.com/scriptcenter/d46b1f3b-36a4-4a56-951b-e37815a2df0c
Get-UserSession
tagging in on Joel’s pointer… just a heads up…
Agreed, Write-Host, outside of specific needs should not be used, …
Sending things to the screen in general does not require Write anything. It’s a default action.
but… as to the normal pointer go to in order back up such statements.