Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. $content = "H:democomputernames.txt" #please input where the list of servers is found (in a txt doc please)
  2. $input = "H:demosessionrun11.csv" #please input where you want the csv to be exported (this includes all the active and disconnected servers)
  3. $export ='H:demosessionopenservers.txt' #please input where you want to 'open servers' (no users) to be exported.
  4. #and that does it. cheers!
  5.  
  6. if (Test-Path $input) {
  7. Clear-Content $input
  8. }
  9.  
  10. $Servers = Get-Content "$content"
  11. $openservers = @()
  12. #THE LOOP THAT FILTERS THE SERVERS MENTIONED IN THE LIST ABOVE
  13. foreach ($Server in $Servers) {
  14. #tests the connection to the server (general test for the entire server)
  15. if (-not (Test-Connection $Server -Count 1 -Quiet)) { continue }
  16. #if their are no users logged in (hence the count of zero), push that server name onto a txt doc
  17. if (-not (Convert-QueryToObjects $Server -ErrorAction SilentlyContinue)) {
  18. $openservers += $server
  19. $openservers | Out-File $export #**TXT EXPORT**#
  20. }
  21. #if there are users logged onto the server, make sure they are either in disc or active state and push all the session details onto a seperate csv
  22. else {
  23. Convert-QueryToObjects -Name $Server | Where-Object {
  24. @('Disconnected','Active') -contains $_.SessionState
  25. }|Export-Csv $input -NoTypeInformation -Append
  26. # does the append so each time the content is kept (however, the issue of duplicate content arose (hence the fix above).
  27. }
  28. } #concludes the first foreach loop
  29. #<--- the folllowing function works with PsExec(external program)
  30. $openservers = Get-Content "$export" #**TXT IMPORT**#
  31. #for each server in the server list, it will check to make sure its online (Again).
  32. foreach($openserver in $openservers){
  33. if( Test-Connection -Cn $openserver -Quiet) {
  34. H:PsToolsPsExec.exe \$openserver change logon /enable # calls an external tool and passes it the
  35. }
  36. else {
  37. Write-Host("the server>> $openserver is NOT online") -ForegroundColor DarkYellow
  38. }
  39. }
  40.  
  41.  
  42.  
  43. #THIS IMPORTS FROM THE EXPORTED FILE PATH#
  44. Import-Csv $input | Where-Object {
  45. ($_.SessionState -eq 'Disconnected') -or #if its disconnected or
  46. (($_.IdleTime -like "*:*") -and ($_.IdleTime -gt "00:59")) #idle time is over an hour, kindly do this:
  47. } | ForEach-Object { #disconnect the session by using the server name and session ID
  48. Disconnect-LoggedOnUser -ComputerName $_.ComputerName -Id $_.ID -Verbose
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement