Advertisement
ultros77

Untitled

Jun 6th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. $CurrentState = $null
  2. $NewState = $null
  3.  
  4. while($true) {
  5. if(-not($CurrentState)) {
  6. $CurrentState = Get-PnpDevice -Status OK
  7. } else {
  8. $NewState = Get-PnpDevice -Status OK
  9. $Changes = $null
  10. $Changes = Compare-Object -ReferenceObject $CurrentState -DifferenceObject $NewState
  11. if($Changes) {
  12. $Additions = @()
  13. $Removals = @()
  14. foreach($Change in $Changes) {
  15. if($Change.SideIndicator -eq "=>") {
  16. $Additions += @($Change.InputObject)
  17. } elseif ($Change.SideIndicator -eq "<=") {
  18. $Removals += @($Change.InputObject)
  19. }
  20. }
  21. if($Additions) {
  22. Write-Host "`n`nNew devices detected..." -ForegroundColor Green
  23. Write-Output $("="*50)
  24. $Additions
  25. Write-Output $("="*50)
  26. }
  27.  
  28. if($Removals) {
  29. Write-Host "`nDevices removed since last check..." - -ForegroundColor Red
  30. Write-Output $("="*50)
  31. $Removals
  32. Write-Output $("="*50)
  33. }
  34.  
  35. }
  36. $CurrentState = $NewState
  37. }
  38. Start-Sleep -Seconds 5
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement