My powershell script determines the current user of a remote Windows 7 computer and will output
userId=DOMAIN\username
If there is no user currently logged on, the script will output
userId=No One Currently Logged In
And if the script cannot access the WMI of the remote computer, the script will output
userId=CannotConnectToWMI
I ran the script along with running WBEMTEST to confirm whether or not WMI can be accessed on the remote machine.
I am really puzzled because yesterday afternoon, I was able to access WMI on several remote machines, and this morning, I cannot. Below is a chart:
Why is this happening? How to make sure that WMI is always accessible? I posted another question yesterday about WMI, https://stackoverflow.com/questions/19409747/wbemtest-to-windows-7-says-the-rpc-server-is-unavailable
Please help
@vonPryz
The script has Test-Connection. Below is the entire script
$line_array = @()
$multi_array = @()
[hashtable]$my_hash = @{}
$Sender_IP = $NULL
$bios = $NULL
$wmi = $NULL
foreach ($i in $args){
$line_array+= $i.split(" ")
}
foreach ($j in $line_array){
$multi_array += ,@($j.split("="))
}
foreach ($k in $multi_array){
$my_hash.add($k[0],$k[1])
}
$Sender_IP = $my_hash.Get_Item("sender-ip")
try{
Test-Connection $Sender_IP -count 1 -ErrorAction Stop | out-null
}
catch [Exception]
{
$userId = "userId=CannotPing"
return $userId
}
try{
$wmi = gwmi -class win32_computerSystem -computer $Sender_IP -ErrorAction Stop
}
catch [Exception]{
$userId = "userId=CannotConnectToWMI"
return $userId
}
try{
$userId = ($wmi).username
}
catch [Exception]{
$userId = "userId=CannotFindLastUserLoggedOn"
return $userId
}
if ($userId -ne $NULL){
$userID = "userId="+$userId
return $userId
}
elseif ($userID -eq $NULL)
{
$userId = "userId=No One Currently Logged In"
return $userId
}
EDIT
I was remoting into these computers to check DCOM permissions, and then I realized that one of them turned into Windows XP. It seems that the IP addresses are getting switched to different computers. I will compare according to Fully Qualified Domain Name.