SHARE
TWEET

rsat1809

a guest Feb 20th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.     Powershell script to install/uninstall RSAT (Remote Server Administration Tools) on Windows 10 v1809 and higher
  4.    
  5. .DESCRIPTION
  6.     RSAT (Remote Server Administration Tools) in Windows 10 v1809 are no longer a downloadable add-on to Windows. Instead its included as a set of  "Features on Demand" directly in Windows.
  7.     The script requires adminitrative rights to run.
  8.     The script will only run on Windows 10 v1809 or higher.
  9.     The script can be run from SCCM and as of such I have added a registry key for you to use as detection method in the application model
  10.    
  11. .EXAMPLES
  12.     .\Install-RSATv1809.ps1 -Uninstall (Removes all RSAT features from the local PC)
  13.     .\Install-RSATv1809.ps1 -All (Install all the RSAT features on the local PC)
  14.     .\Install-RSATv1809.ps1 -Basic (Installs AD, DHCP, DNS, GP Management and Server Manager on the local PC)
  15.    
  16. .NOTES
  17.     Filename: Install-RSATv1809.ps1
  18.     Version: 1.0
  19.     Author: Martin Bengtsson
  20.     Blog: www.imab.dk
  21.     Twitter: @mwbengtsson
  22.  
  23. .LINKS
  24.    
  25. #>
  26.  
  27. [CmdletBinding()]
  28. param(
  29.     [parameter(Mandatory=$false)]
  30.     [ValidateNotNullOrEmpty()]
  31.     [switch]$All,
  32.  
  33.     [parameter(Mandatory=$false)]
  34.     [ValidateNotNullOrEmpty()]
  35.     [switch]$Basic,
  36.  
  37.     [parameter(Mandatory=$false)]
  38.     [ValidateNotNullOrEmpty()]
  39.     [switch]$ServerManager,
  40.  
  41.     [parameter(Mandatory=$false)]
  42.     [ValidateNotNullOrEmpty()]
  43.     [switch]$Uninstall
  44. )
  45.  
  46. # Check for administrative rights
  47. if (-NOT([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
  48.    
  49.     Write-Warning -Message "The script requires elevation"
  50.     break
  51.    
  52. }
  53.  
  54. # Variables
  55. $1809Build = "17763"
  56. $Windows = Get-WmiObject -Class Win32_OperatingSystem | Select-Object BuildNumber
  57. # Registrypath - change to fit your need
  58. $RegistryPath = "HKLM:\Software\imab.dk"
  59.  
  60. # Check if running supported version of Windows 10
  61. if ($Windows.BuildNumber -lt "$1809Build") {
  62.    
  63.     Write-Error -Message "Oops - not running the proper Windows build. Windows 10 v1809 is required as a minimum."
  64.     break
  65.  
  66. }
  67.  
  68. # Continuing if running on supported version of Windows 10
  69. else {
  70.    
  71.     if ($PSBoundParameters["All"]) {
  72.        
  73.         # Query for all RSAT features that's not installed already
  74.         $Install = Get-WindowsCapability -Online | Where-Object {$_.Name -like "Rsat*" -AND $_.State -eq "NotPresent"}
  75.  
  76.         if ($Install -ne $null) {
  77.  
  78.             foreach ($Item in $Install) {
  79.            
  80.                 try {
  81.                     Write-Host -ForegroundColor Yellow "Adding" $Item.Name "to Windows"
  82.                     Add-WindowsCapability -Online -Name $Item.Name
  83.                     }
  84.            
  85.                 catch [System.Exception]
  86.                     {
  87.                     Write-Warning -Message $_.Exception.Message ; break
  88.                     }
  89.             }
  90.            
  91.             # Adding registry key for detection method in SCCM
  92.             if (-NOT(Test-Path -Path $RegistryPath)) {
  93.                 New-Item -Path $RegistryPath –Force
  94.                 }
  95.            
  96.             New-ItemProperty -Path $RegistryPath -Name "1809RSATInstalled" -Value 1 -PropertyType "String" -Force
  97.            
  98.         }
  99.        
  100.         else {
  101.             Write-Host -ForegroundColor Red "RSAT seems to be installed already"
  102.         }
  103.     }
  104.  
  105.     if ($PSBoundParameters["Basic"]) {
  106.        
  107.         $Install = Get-WindowsCapability -Online | Where-Object {$_.Name -like "Rsat.ActiveDirectory*" -OR $_.Name -like "Rsat.DHCP.Tools*" -OR $_.Name -like "Rsat.Dns.Tools*" -OR $_.Name -like "Rsat.GroupPolicy*" -AND $_.State -eq "NotPresent" }
  108.  
  109.         if ($Install -ne $null) {
  110.        
  111.             foreach ($Item in $Install) {
  112.            
  113.                 try {
  114.                     Write-Host -ForegroundColor Yellow "Adding" $Item.Name "to Windows"
  115.                     Add-WindowsCapability -Online -Name $Item.Name
  116.                     }
  117.            
  118.                 catch [System.Exception]
  119.                     {
  120.                     Write-Warning -Message $_.Exception.Message ; break
  121.                     }
  122.             }
  123.            
  124.             # Adding registry key for detection method in SCCM
  125.             if (-NOT(Test-Path -Path $RegistryPath)) {
  126.                 New-Item -Path $RegistryPath –Force
  127.                 }
  128.            
  129.             New-ItemProperty -Path $RegistryPath -Name "1809RSATInstalled" -Value 1 -PropertyType "String" -Force
  130.         }
  131.  
  132.         else {
  133.             $Install = (Get-WindowsCapability -Online | Where-Object {$_.Name -like "Rsat.ServerManager*"}).Name
  134.             Write-Host -ForegroundColor Red $Install "seems to be installed already"
  135.         }
  136.        
  137.  
  138.     }
  139.  
  140.     if ($PSBoundParameters["ServerManager"]) {
  141.        
  142.         $Install = Get-WindowsCapability -Online | Where-Object {$_.Name -like "Rsat.ServerManager*" -AND $_.State -eq "NotPresent"}
  143.          
  144.         if ($Install -ne $null) {
  145.            
  146.             try {
  147.                 Write-Host -ForegroundColor Yellow "Adding" $Install.Name "to Windows"
  148.                 Add-WindowsCapability -Online -Name $Install.Name
  149.                 }
  150.            
  151.             catch [System.Exception]
  152.                 {
  153.                 Write-Warning -Message $_.Exception.Message ; break
  154.                 }
  155.            
  156.             if (-NOT(Test-Path -Path $RegistryPath)) {
  157.                 New-Item -Path $RegistryPath –Force
  158.                 }
  159.            
  160.             New-ItemProperty -Path $RegistryPath -Name "1809RSATInstalled" -Value 1 -PropertyType "String" -Force        
  161.         }
  162.        
  163.         else {
  164.             $Install = (Get-WindowsCapability -Online | Where-Object {$_.Name -like "Rsat.ServerManager*"}).Name
  165.             Write-Host -ForegroundColor Red $Install "seems to be installed already"
  166.         }
  167.     }
  168.  
  169.     if ($PSBoundParameters["Uninstall"]) {
  170.        
  171.         # Querying for installed RSAT features first time
  172.         $Installed = Get-WindowsCapability -Online | Where-Object {$_.Name -like "Rsat*" -AND $_.State -eq "Installed" -AND $_.Name -notlike "Rsat.ServerManager*" -AND $_.Name -notlike "Rsat.GroupPolicy*" -AND $_.Name -notlike "Rsat.ActiveDirectory*"}
  173.  
  174.         if ($Installed -ne $null) {
  175.            
  176.             # Removing first round of RSAT features - some features seems to be locked until others are removed first
  177.             foreach ($Item in $Installed) {
  178.            
  179.                 try {
  180.                     Write-Host -ForegroundColor Yellow "Removing" $Item.Name "from Windows"
  181.                     Remove-WindowsCapability -Name $Item.Name -Online
  182.                     }
  183.            
  184.                 catch [System.Exception]
  185.                     {
  186.                     Write-Warning -Message $_.Exception.Message ; break
  187.                     }
  188.             }      
  189.                          
  190.         }
  191.  
  192.         # Querying for installed RSAT features second time
  193.         $Installed = Get-WindowsCapability -Online | Where-Object {$_.Name -like "Rsat*" -AND $_.State -eq "Installed"}
  194.        
  195.         if ($Installed -ne $null) {
  196.            
  197.             # Removing second round of RSAT features
  198.             foreach ($Item in $Installed) {
  199.            
  200.                 try {
  201.                     Write-Host -ForegroundColor Yellow "Removing" $Item.Name "from Windows"
  202.                     Remove-WindowsCapability -Name $Item.Name -Online
  203.                     }
  204.            
  205.                 catch [System.Exception]
  206.                     {
  207.                     Write-Warning -Message $_.Exception.Message ; break
  208.                     }
  209.             }
  210.            
  211.             # Removing registry key used for detection method in SCCM
  212.             Remove-ItemProperty -Path $RegistryPath -Name "1809RSATInstalled" -Force
  213.         }
  214.  
  215.         else {
  216.             Write-Host -ForegroundColor Red "Nothing to uninstall"
  217.         }
  218.     }
  219.  
  220. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top