xs4eyes

DisableNetAdapter

Apr 8th, 2016
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#############################################################
  2. Disable the network adapter of the selected user
  3. #############################################################>
  4.  
  5.  
  6. # Set the username to disable
  7.  
  8. $user = "testuser"
  9.  
  10. #Get list of online computers
  11. Get-ADComputer -Filter * |
  12.  
  13. # Test each computer in the AD list for response and Store that list
  14. ForEach-Object {
  15.  
  16.   $rtn = Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet
  17.  
  18.   IF($rtn -match ‘True’) {$_.dnshostname | Out-File c:\onlinepc.txt -Append
  19.    write-host -ForegroundColor green $_.dnshostname}
  20.  
  21.   ELSE { Write-host -ForegroundColor red $_.dnshostname }
  22.  
  23. }
  24.  
  25. $opc = Get-Content C:\onlinepc.txt
  26.  
  27. #Find the users that are logged in on the online pc's
  28. Invoke-Command $opc {(quser) -replace '\s{2,}', ',' | ConvertFrom-Csv} | Export-Csv c:\qusr.csv
  29. #Get the computer name that the user is logged into
  30. $pc = Import-Csv C:\qusr.csv | where USERNAME -Like "$user" | select PSComputername
  31. $pc = $pc -replace "}"
  32. $pc = $pc -replace "@{PSComputerName="
  33.  
  34. #Popup message on the disabled pc
  35. $server = $pc;
  36. $message = "Attention $user!
  37. Your network connection has been disabled due to a possible virus infection!
  38. Do NOT attempt to re-enable it. Contact I.T immediately!";
  39. Invoke-WmiMethod -Class win32_process -ComputerName $server -Name create -ArgumentList `
  40.  "c:\windows\system32\msg.exe * $message"
  41.  
  42. #Disable any connected net adapter
  43. $test = Get-WmiObject -Class win32_networkadapter -computerName $pc -filter "NetConnectionStatus = 2"
  44. $test.disable()
  45.  
  46. # Cleanup files not needed
  47. Remove-Item c:\qusr.csv
  48. Remove-Item C:\onlinepc.txt
Advertisement
Add Comment
Please, Sign In to add comment