Guest User

Untitled

a guest
May 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. param(
  2. [CmdletBinding(DefaultParameterSetName="OU")]
  3. [Parameter(
  4. ParameterSetName="Computer",
  5. ValueFromPipeline=$true,
  6. ValueFromPipelineByPropertyName=$true)]
  7. [string[]]$ComputerName,
  8.  
  9. [Parameter(ParameterSetName="OU")]
  10. [string]$SearchBase = "OU=Target,OU=Stations,DC=hagatorn,DC=local",
  11.  
  12. [Parameter(ParameterSetName="Computer")]
  13. [Parameter(ParameterSetName="OU")]
  14. [string]$Idletime = 1440, #1 Day
  15.  
  16. [Parameter(ParameterSetName="Computer")]
  17. [Parameter(ParameterSetName="OU")]
  18. [switch]$disconnected = $true,
  19.  
  20. [Parameter(ParameterSetName="Computer")]
  21. [Parameter(ParameterSetName="OU")]
  22. [switch]$idle = $true,
  23.  
  24. [Parameter(ParameterSetName="OU")]
  25. [switch]$All
  26. )
  27.  
  28. Write-verbose ("Param Set: "+($psCmdlet.ParameterSetName))
  29. if($psCmdlet.ParameterSetName -eq "OU"){
  30. $targetComputers = Get-ADComputer -filter '*' -SearchBase $SearchBase
  31. }
  32. else{
  33. $targetComputers = New-Object psobject -Property @{Name = $ComputerName}
  34. }
  35.  
  36.  
  37. $command = {
  38. param(
  39. [CmdletBinding()]
  40. [string]$Idletime,
  41. [switch]$disconnected,
  42. [switch]$idle
  43. )
  44. function Get-LoggedOnUser{
  45. param(
  46. [CmdletBinding()]
  47. [Parameter(ValueFromPipeline=$true,
  48. ValueFromPipelineByPropertyName=$true)]
  49. [string[]]$ComputerName = 'localhost'
  50. )
  51. begin {
  52. $ErrorActionPreference = 'Stop'
  53. }
  54.  
  55. process {
  56. foreach ($Computer in $ComputerName) {
  57. try {
  58. quser /server:$Computer 2>&1 | Select-Object -Skip 1 | ForEach-Object {
  59. $CurrentLine = $_.Trim() -Replace '\s+',' ' -Split '\s'
  60. $HashProps = @{
  61. UserName = $CurrentLine[0]
  62. ComputerName = $Computer
  63. }
  64.  
  65. # If session is disconnected different fields will be selected
  66. if ($CurrentLine[2] -eq 'Disc') {
  67. $HashProps.SessionName = $null
  68. $HashProps.Id = $CurrentLine[1]
  69. $HashProps.State = $CurrentLine[2]
  70. $HashProps.IdleTime = $CurrentLine[3]
  71. $HashProps.LogonTime = $CurrentLine[4..6] -join ' '
  72. $HashProps.LogonTime = $CurrentLine[4..($CurrentLine.GetUpperBound(0))] -join ' '
  73. } else {
  74. $HashProps.SessionName = $CurrentLine[1]
  75. $HashProps.Id = $CurrentLine[2]
  76. $HashProps.State = $CurrentLine[3]
  77. $HashProps.IdleTime = $CurrentLine[4]
  78. $HashProps.LogonTime = $CurrentLine[5..($CurrentLine.GetUpperBound(0))] -join ' '
  79. }
  80.  
  81. New-Object -TypeName PSCustomObject -Property $HashProps |
  82. Select-Object -Property UserName,ComputerName,SessionName,Id,State,IdleTime,LogonTime,Error
  83. }
  84. } catch {
  85. New-Object -TypeName PSCustomObject -Property @{
  86. ComputerName = $Computer
  87. Error = $_.Exception.Message
  88. } | Select-Object -Property UserName,ComputerName,SessionName,Id,State,IdleTime,LogonTime,Error
  89. }
  90. }
  91. }
  92. }
  93.  
  94. function Parse-Time{
  95. param(
  96. [CmdletBinding()]
  97.  
  98. [ValidatePattern('^(((?<days>[0-9]+)\+)?((?<hours>[0-9]{1,2}):))?(?<mins>[0-9]{1,2})$')]
  99. [string]$quserTimeString
  100.  
  101. )
  102.  
  103. $quserTimeString -match '^(((?<days>[0-9]+)\+)?((?<hours>[0-9]{1,2}):))?(?<mins>[0-9]{1,2})$' | Out-Null
  104.  
  105. [Timespan]::new($Matches.days, $Matches.hours, $Matches.mins, 0)
  106.  
  107.  
  108. }
  109.  
  110. $idlemins = @{Name = "TotalIdleMinutes" ; Expression = {(Parse-Time $_.IdleTime).TotalMinutes} }
  111.  
  112. try{
  113. Get-LoggedOnUser |
  114. ?{$_.Error -ne "No User exists for *"} |
  115. select *, $idlemins |
  116. %{ if( $_.State -eq "Disc"){
  117.  
  118. Write-Output ($env:computername+": "+$_.Username+" disconnected and idle for "+$_.TotalIdleMinutes+" Mins")
  119. if($disconnected){Write-Output ( $_.id+" | % {Logoff `$_}")}
  120.  
  121. }elseif($_.IdleTime -ne "none" -and $_.TotalIdleMinutes -gt $Idletime)
  122. {
  123.  
  124. Write-Output ($env:computername+": "+$_.Username+" idle for "+$_.TotalIdleMinutes+" Mins")
  125.  
  126. if($idle){ Write-Output ( $_.id+" | % {Logoff `$_}")}
  127.  
  128. }
  129. else{
  130. Write-Output ($env:computername+": "+$_.Username+" is active")
  131. }
  132. }
  133. }
  134. catch [System.Management.Automation.RuntimeException]{
  135. Write-warning "Error Executing Get-LoggedOnUser on $env:computername"
  136. $_
  137. }
  138.  
  139. }
  140.  
  141. $targetComputers.name |
  142. %{
  143.  
  144. try{
  145.  
  146. invoke-command -computer $_ -ErrorAction Stop -command $command -ArgumentList $Idletime,$disconnected,$idle
  147.  
  148. }
  149.  
  150. catch [System.Management.Automation.RuntimeException]{
  151.  
  152. Write-warning ("Error Connecting to "+($_.TargetObject))
  153.  
  154. }
  155.  
  156. }
Add Comment
Please, Sign In to add comment