Lee_Dailey

function Get-QUserInfo

Oct 15th, 2018
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-QUserInfo
  2.     {
  3.     [CmdletBinding()]
  4.     Param (
  5.         [Parameter ()]
  6.             [string[]]
  7.             $ComputerName = $env:COMPUTERNAME
  8.         )
  9.  
  10.     begin
  11.         {
  12.         $Header = 'UserName','SessionName','ID','State','IdleTime','LogonTime'
  13.         $No_Connection = '-- No Connection --'
  14.         }
  15.  
  16.     process
  17.         {
  18.         foreach ($CN_Item in $ComputerName)
  19.             {
  20.             if (Test-Connection -ComputerName $CN_Item -Count 1 -Quiet)
  21.                 {
  22.                 quser /server:$CN_Item |
  23.                     Select-Object -Skip 1 |
  24.                     ForEach-Object {($_ -replace '\s{2,}', ',').Trim()} |
  25.                     ConvertFrom-Csv -Header $Header |
  26.                     ForEach-Object {
  27.                         if ($_.IdleTime -eq 'none')
  28.                             {
  29.                             $IdleTime = $Null
  30.                             }
  31.                             else
  32.                             {
  33.                             $IdleTime = [timespan]$_.IdleTime
  34.                             }
  35.                         [PSCustomObject]@{
  36.                             ComputerName = $CN_Item
  37.                             UserName = $_.UserName
  38.                             SessionName = $_.SessionName
  39.                             ID = $_.ID
  40.                             State = $_.State
  41.                             IdleTime = $IdleTime
  42.                             LogonTime = [datetime]$_.LogonTime
  43.                             }
  44.                         }                
  45.                 }
  46.                 else
  47.                 {
  48.                 [PSCustomObject]@{
  49.                     ComputerName = $CN_Item
  50.                     UserName = $No_Connection
  51.                     SessionName = $No_Connection
  52.                     ID = $No_Connection
  53.                     State = $No_Connection
  54.                     IdleTime = $No_Connection
  55.                     LogonTime = $No_Connection
  56.                     }                
  57.                 }
  58.             } # end >> foreach ($CN_Item in $ComputerName)
  59.         } # end >> process {}
  60.  
  61.     end {}
  62.  
  63.     } # end >> function Get-QUserInfo
  64.  
  65.  
  66.  
  67. Get-QUserInfo -ComputerName LocalHost
  68. ''
  69. Get-QUserInfo -ComputerName '10.0.0.1'
  70. ''
  71. Get-QUserInfo -ComputerName '127.0.0.1', BetterNotBeThere, LocalHost
  72. ''
Add Comment
Please, Sign In to add comment