Advertisement
Guest User

Untitled

a guest
May 18th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Sophos Endpoint Cloud Installation Script v0.1.10
  2. # Ross Sutherland 2017
  3. # Hardies Property & Construction Consultants
  4.  
  5. Clear-Host
  6.  
  7. $ErrorActionPreference = “SilentlyContinue”
  8.  
  9. # Get AD credentials
  10. if ($credential -eq $null)
  11. {
  12.     $credential = Get-Credential
  13. }
  14. else
  15. { }
  16.  
  17. # Null some variables from previous or failed runs that would break stuff
  18. $office = $null
  19. $server = $null
  20. $hostname = $null
  21. $computer = $null
  22. $computers = $null
  23. $desktops = $null
  24. $laptops = $null
  25. $installFail = $null
  26. $installPass = $null
  27.  
  28. # Define a couple of arrays
  29. $installPass = New-Object System.Collections.ArrayList
  30. $installFail = New-Object System.Collections.ArrayList
  31.  
  32.  
  33. Write-Host "Which office would you like to deploy Sophos Endpoint Cloud to?`n" -ForegroundColor Magenta
  34. [int]$xMenuChoiceA = 0
  35. While ($xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 11){
  36.     "`t 1. Aberdeen"
  37.     "`t 2. Dumfries"
  38.     "`t 3. Dundee"
  39.     "`t 4. Dunfermline"
  40.     "`t 5. Edinburgh"
  41.     "`t 6. Glasgow St Vincent Street"
  42.     "`t 7. Inverness"
  43.     "`t 8. Perth"
  44.     "`t 9. Stirling"
  45.     "`t 10. St Andrews"
  46.     "`t 11. Or target a specific machine"
  47. Write-Host "`n Please enter an option 1 to 11... " -ForegroundColor Magenta -NoNewLine
  48. [Int]$xMenuChoiceA = Read-Host }
  49. Switch( $xMenuChoiceA ){
  50.   1{ $office = "aberdeen" ; $server = "hcsaberdeen" }
  51.   2{ $office = "dumfries" ; $server = "hcsdunfermline" }
  52.   3{ $office = "dundee" ; $server = "hcsdundee" }
  53.   4{ $office = "dunfermline" ; $server = "hcsdunfermline" }
  54.   5{ $office = "edinburgh" ; $server = "hcsedinburgh" }
  55.   6{ $office = "glasgow" ; $server = "hcsglasgow" }
  56.   7{ $office = "inverness" ; $server = "hcsdunfermline" }
  57.   8{ $office = "perth" ; $server = "hcsperth" }
  58.   9{ $office = "stirling" ; $server = "hcsstirling" }
  59.   10{ $office = "st andrews" ; $server = "eternium" }
  60.   11{ $hostname = Read-Host "Target hostname" ; $server = Read-Host "Target's server" }
  61. }
  62.  
  63. if ($hostname)
  64. {
  65.     $computer = $hostname
  66.     $computers = $hostname
  67. }
  68. else
  69. {
  70.     try
  71.     {
  72.         $desktops = Get-ADComputer -SearchBase "OU=Computers,OU=$office,DC=hardies,DC=local" -Filter * | Select -ExpandProperty Name
  73.         $laptops = Get-ADComputer -SearchBase "OU=Laptops,OU=$office,DC=hardies,DC=local" -Filter * | Select -ExpandProperty Name
  74.         $computers = $desktops+$laptops
  75.     }
  76.     catch
  77.     {
  78.         Write-Host "Something went wrong collecting the computer objects from Active Directory. Do you have the appropriate permissions?" -ForegroundColor Red
  79.         Exit
  80.     }
  81. }
  82.  
  83. # Display number of computers found
  84. Write-Host `nFound $computers.Count computers in $office. $desktops.Count desktops and $laptops.Count laptops.
  85.  
  86. # Start doing all the stuff
  87. foreach ($computer in $computers)
  88. {
  89.     Write-Host "`n--------------------------------------"
  90.     Write-Host "Starting installation on $computer...`n"
  91.  
  92.     # Check the computer is available
  93.     $computerTest = Test-Connection $computer -Count 1 -ErrorAction SilentlyContinue
  94.  
  95.         if ($computerTest)
  96.         {
  97.             # Enable CredSSP locally, delegating to the computer in the current loop
  98.             Enable-WSManCredSSP -Role Client -DelegateComputer $computer -Force | Out-Null
  99.  
  100.             # Show the current user logged onto the remote computer
  101.             $currentUser = gwmi win32_computersystem -ComputerName $computer -Credential $credential -ErrorAction SilentlyContinue | Select -ExpandProperty Username
  102.  
  103.             if ($currentUser)
  104.             {
  105.                 Write-Host "Current logged on user: $currentUser"
  106.             }
  107.             else
  108.             {}
  109.  
  110.             # Create a PS session, enable CredSSP on the remote computer, then exit the session
  111.             $session = New-PSSession -ComputerName $computer -Credential $credential -ErrorAction SilentlyContinue
  112.  
  113.                 if ($session)
  114.                 {
  115.                     Enter-PSSession -Session $session
  116.                     Enable-WSManCredSSP –Role Server -Force | Out-Null
  117.                     # Start-Sleep -Seconds 5
  118.                     Exit-PSSession
  119.  
  120.                     # Create a PS session with CredSSP
  121.                     $session = New-PSSession -ComputerName $computer -Credential $credential -Authentication Credssp
  122.  
  123.                         if ($session)
  124.                         {        
  125.                             # Start the installation
  126.                             Invoke-Command -Session $session -ScriptBlock { New-Item -Path c:\temp -ItemType Directory -Force } | Out-Null
  127.                             Invoke-Command -Session $session -ScriptBlock { Copy-Item -Path \\$using:server\ClientApps\Sophos\SophosInstall.exe -Destination c:\temp }
  128.                             Invoke-Command -Session $session -ScriptBlock { & cmd.exe /c "c:\temp\SophosInstall.exe -tps remove -q" }
  129.                             Invoke-Command -Session $session -ScriptBlock { Remove-Item c:\temp\SophosInstall.exe -Force } | Out-Null
  130.  
  131.                             # Disable CredSSP on the remote computer, then exit and remove the PS session
  132.                             Disable-WSManCredSSP -Role Server
  133.                             Exit-PSSession
  134.                             Remove-PSSession -Session $session
  135.                         }
  136.                         else
  137.                         {
  138.                             Write-Host "`nCould not establish a PS Session to $computer with CredSSP." -ForegroundColor Red
  139.                             $installFail.Add($computer) | Out-Null
  140.                         }
  141.                 }
  142.                 else
  143.                 {
  144.                     Write-Host "Could not establish a PS Session to $computer." -ForegroundColor Red
  145.                     $installFail.Add($computer) | Out-Null
  146.                 }
  147.         }
  148.         else
  149.         {
  150.             Write-Host "Could not contact $computer." -ForegroundColor Red
  151.             $installFail.Add($computer) | Out-Null
  152.         }
  153.     $installPass.Add($computer) | Out-Null
  154. }
  155.  
  156. Write-Host "`n-------RESULTS--------" -BackgroundColor DarkGray
  157. Write-Host "`nSuccessful:"
  158. Write-Host "$installPass" -ForegroundColor Green
  159. Write-Host "`nFailed:"
  160. Write-Host "$installFail" -ForegroundColor Red
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement