Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. $username = Read-Host "Enter the Users ID"
  2. $dcs = Get-ADDomainController -Filter {Name -like "*"} | Select -expandproperty name
  3. $Code = {
  4. Param($username,$dc)
  5. Get-ADUser $username | Get-ADObject -Server $dc -Properties lastLogon |
  6.  
  7. Select -Expandproperty lastLogon
  8. }
  9. $rsPool = [runspacefactory]::CreateRunspacePool(1,8)
  10. $rsPool.Open()
  11. foreach($dc in $dcs)
  12. {
  13. $PSinstance = [powershell]::Create().AddScript($Code).AddArgument($username).AddArgument($dc)
  14. $PSinstance.RunspacePool = $RunspacePool
  15. $PSinstance.BeginInvoke()
  16. }
  17.  
  18. $userName = Read-Host "Enter NTID: "
  19.  
  20. $time = 0
  21.  
  22.  
  23. $dcs = Get-ADDomainController -Filter {Name -like "*"} | Select -expandproperty name
  24.  
  25.  
  26. $scriptbox = {
  27.  
  28. Param($username,$dc)
  29.  
  30. Get-ADUser $username | Get-ADObject -Server $dc -Properties lastLogon | Select -Expandproperty lastLogon
  31.  
  32. }
  33.  
  34. foreach($dc in $dcs){start-Job -ScriptBlock $scriptbox -ArgumentList $username,$dc}
  35.  
  36.  
  37. Get-Job | Wait-Job
  38.  
  39.  
  40. Get-Job
  41.  
  42. $Data = ForEach ($Job in (Get-Job)) {
  43.  
  44. Receive-Job $Job
  45.  
  46. Remove-Job $Job
  47.  
  48. }
  49.  
  50.  
  51. Foreach ($date in $Data){if($date -gt $time){$time = $date}}
  52.  
  53.  
  54. $dt = [DateTime]::FromFileTime($time)
  55.  
  56. write-Host $username "last logged on at:" $dt
  57.  
  58. # Script to run in each thread.
  59. [System.Management.Automation.ScriptBlock]$ScriptBlock = {
  60.  
  61. $adUser = Get-ADUser -Identity $args[0] -Server $args[1] -Properties lastLogon
  62.  
  63. $result = New-Object PSObject -Property @{ 'User' = $args[0];
  64. 'DC' = $args[1];
  65. 'LastLogon' = $adUser.LastLogon; }
  66.  
  67. return $result
  68.  
  69. } # End Scriptblock
  70.  
  71. function Invoke-AsyncJob
  72. {
  73. [CmdletBinding()]
  74. param(
  75. [parameter(Mandatory=$true)]
  76. [System.String]
  77. # User ID
  78. $Username
  79. )
  80.  
  81. Import-Module -Name ActiveDirectory
  82.  
  83. $Results = @()
  84.  
  85. $AllJobs = New-Object System.Collections.ArrayList
  86.  
  87. $AllDomainControllers = Get-ADDomainController -Filter "*"
  88.  
  89. $HostRunspacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(2,10,$Host)
  90.  
  91. $HostRunspacePool.Open()
  92.  
  93. foreach($DomainController in $AllDomainControllers)
  94. {
  95. $asyncJob = [System.Management.Automation.PowerShell]::Create().AddScript($ScriptBlock).AddParameters($($Username,$($DomainController.Name)))
  96.  
  97. $asyncJob.RunspacePool = $HostRunspacePool
  98.  
  99. $asyncJobObj = @{ JobHandle = $asyncJob;
  100. AsyncHandle = $asyncJob.BeginInvoke() }
  101.  
  102. $AllJobs.Add($asyncJobObj) | Out-Null
  103. }
  104.  
  105. $ProcessingJobs = $true
  106.  
  107. Do {
  108.  
  109. $CompletedJobs = $AllJobs | Where-Object { $_.AsyncHandle.IsCompleted }
  110.  
  111. if($null -ne $CompletedJobs)
  112. {
  113. foreach($job in $CompletedJobs)
  114. {
  115. $result = $job.JobHandle.EndInvoke($job.AsyncHandle)
  116.  
  117. if($null -ne $result)
  118. {
  119. $Results += $result
  120. }
  121.  
  122. $job.JobHandle.Dispose()
  123.  
  124. $AllJobs.Remove($job)
  125. }
  126.  
  127. } else {
  128.  
  129. if($AllJobs.Count -eq 0)
  130. {
  131. $ProcessingJobs = $false
  132.  
  133. } else {
  134.  
  135. Start-Sleep -Milliseconds 1000
  136. }
  137. }
  138.  
  139. } While ($ProcessingJobs)
  140.  
  141. $HostRunspacePool.Close()
  142. $HostRunspacePool.Dispose()
  143.  
  144. return $Results
  145.  
  146. } # End function Invoke-AsyncJob
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement