Hello all,

A little help here pls. I have my below script which returns 80% info that I want but can’t seem to get it to work exactly how I want. I would like it to achieve all of the 3.

  1. The script checks if the computer is online or not.

  2. Checks if the computer is online AND if a user is logged on or not.

  3. If computer is in use, it should retrieve “name”, “city” and “department” from Get-ADUser of the logged in user.

So far, I only manage to achieve 1 & 2 but struggling with to achieve number 3. I would appreciate any guidance here. Thanks in advance.

$Computer = ‘PC2335’
If (Test-Connection -BufferSize 32 -Count 2 -ComputerName $Computer -Quiet) {
try{
$User = $null
$User=get-wmiobject win32_computersystem -comp $computer | Select -ExpandProperty UserName -ErrorAction Stop
$Domain,$UserID=$User -split "\\" 
Get-ADUser -Identity $UserID -Properties Name,Title,LastLogon | 
Select-Object Name,City,Department |
Format-List
}
catch {"$Computer is online. No user is logged in"; return}
    
} Else {

Write-Host "$Computer is offline"

}
3 Spice ups

check this out

$computers="computername"

  ForEach ($COMPUTER in $computers)
  {if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
  {write-host "cannot reach $computer" -f red}
  Else {
  TRY{
  $ErrorActionPreference = "Stop"

   $userid=(get-wmiobject Win32_ComputerSystem -ComputerName $computer).UserName.Split('\')[1]
    
    }

   Catch
   {
   Write-Host "$($computer) " -BackgroundColor red -NoNewline
   Write-Warning $Error[0] 
    }
    }
    }
    
    
    If ( ! (Get-module ActiveDirectory )) {
  Import-Module ActiveDirectory -verbose
  Cls
  }
  Foreach($user in $userid)
  Get-ADUser -Identity $User -Properties Department,city,Title,LastLogondate | 
Select-Object Name,City,Department,LastLogondate |
Format-List
3 Spice ups

Try something like this:

$names = Get-WmiObject win32_useraccount -Property name,sid | select name,sid

$hello = Get-CimInstance win32_userprofile  -Property special,loaded,sid | select special,loaded,sid | ?{($_.special -eq $false) -and ($_.loaded -eq $true)}

foreach ($name in $names){

if($name.sid -match $hello.sid){

write-host $name.name

}

}
2 Spice ups

Missed Curly brackets here, If Multiple users

Foreach($user in $userid){
  Get-ADUser -Identity $User -Properties Department,city,Title,LastLogondate | 
Select-Object Name,City,Department,LastLogondate |
Format-List
}
1 Spice up

Thanks JitenSh. Works perfectly. Is there anyway to incorporate get-childitem of c\users\USER_ID in this script so it could also retrieve “lastwritetime” of the user logged in by looking at $userid?

you should have opened a new topic actually, mark answer as best and close this

 $users = import-csv c:\users.csv |select -exp samaccountname
 $computers=""
ForEach ($COMPUTER in $computers)
        {if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
        {write-host "Cannot reach $computer its offline" -f red}
          
else    {
        if ((Test-Path -Path \\$computer\c$)){
        TRY{
$data=foreach ($user in $users){
Get-ChildItem "\\$computer\c$\users\$($user)\ntuser.dat" -force |select LastWriteTime,@{n="User";e={$user}}
   }
        Write-Host "Sucess on $computer" -BackgroundColor Green
        }
        Catch {$error[0].exception.message}
        }
        }
        }
        
        $data |export-csv c:\data.csv -NoTypeInformation
2 Spice ups

This is working as expected I wrote it without testing make sure csv has header as samaccount name or any name but should match this samccount or any header

|select -exp samaccountname

and $computers=list of computers.either text or csv

I wrote One more for computers and all its users

ForEach ($COMPUTER in ($computers))
        {if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
        {write-host "Cannot reach $computer its offline" -f red}
          
else    {
        if ((Test-Path -Path \\$computer\c$)){
        TRY{
        $users = Get-ChildItem \\$computer\c$\users |?{$_.name -notlike '*Public*' -and $_.name -notlike '*Default*' }
$data=foreach ($user in $users){
GCI "$($user.fullname)\ntuser.dat" -force |select LastWriteTime,@{n="User";e={$user}}
   }
        Write-Host "Sucess on $computer" -BackgroundColor Green
        }
        Catch {$error[0].exception.message}
        }
        }
        }
        
        $data |export-csv c:\data.csv -NoTypeInformation

I’ve modified your earlier script and it’s working. I’ll take a look at the last one too. Thanks.

1 Spice up