|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +<# |
| 5 | +.Synopsis |
| 6 | + Group Policy tools use administrative template files (.admx, .adml) to populate policy settings in the user interface. |
| 7 | + This allows administrators to manage registry-based policy settings. |
| 8 | + This script installs PSResourceGet Administrative Templates for Windows. |
| 9 | +.Notes |
| 10 | + The PSResourceRepository.admx and PSResourceRepository.adml files are |
| 11 | + expected to be at the location specified by the Path parameter with default value of the location of this script. |
| 12 | +#> |
| 13 | +[CmdletBinding()] |
| 14 | +param |
| 15 | +( |
| 16 | + [ValidateNotNullOrEmpty()] |
| 17 | + [string] $Path = $PSScriptRoot |
| 18 | +) |
| 19 | +Set-StrictMode -Version 3.0 |
| 20 | +$ErrorActionPreference = 'Stop' |
| 21 | + |
| 22 | +function Test-Elevated |
| 23 | +{ |
| 24 | + [CmdletBinding()] |
| 25 | + [OutputType([bool])] |
| 26 | + Param() |
| 27 | + |
| 28 | + # if the current Powershell session was called with administrator privileges, |
| 29 | + # the Administrator Group's well-known SID will show up in the Groups for the current identity. |
| 30 | + # Note that the SID won't show up unless the process is elevated. |
| 31 | + return (([Security.Principal.WindowsIdentity]::GetCurrent()).Groups -contains "S-1-5-32-544") |
| 32 | +} |
| 33 | +$IsWindowsOs = $PSHOME.EndsWith('\WindowsPowerShell\v1.0', [System.StringComparison]::OrdinalIgnoreCase) -or $IsWindows |
| 34 | + |
| 35 | +if (-not $IsWindowsOs) |
| 36 | +{ |
| 37 | + throw 'This script must be run on Windows.' |
| 38 | +} |
| 39 | + |
| 40 | +if (-not (Test-Elevated)) |
| 41 | +{ |
| 42 | + throw 'This script must be run from an elevated process.' |
| 43 | +} |
| 44 | + |
| 45 | +if ([System.Management.Automation.Platform]::IsNanoServer) |
| 46 | +{ |
| 47 | + throw 'Group policy definitions are not supported on Nano Server.' |
| 48 | +} |
| 49 | + |
| 50 | +$admxName = 'PSResourceRepository.admx' |
| 51 | +$admlName = 'PSResourceRepository.adml' |
| 52 | +$admx = Get-Item -Path (Join-Path -Path $Path -ChildPath $admxName) |
| 53 | +$adml = Get-Item -Path (Join-Path -Path $Path -ChildPath $admlName) |
| 54 | +$admxTargetPath = Join-Path -Path $env:WINDIR -ChildPath "PolicyDefinitions" |
| 55 | +$admlTargetPath = Join-Path -Path $admxTargetPath -ChildPath "en-US" |
| 56 | + |
| 57 | +$files = @($admx, $adml) |
| 58 | +foreach ($file in $files) |
| 59 | +{ |
| 60 | + if (-not (Test-Path -Path $file)) |
| 61 | + { |
| 62 | + throw "Could not find $($file.Name) at $Path" |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +Write-Verbose "Copying $admx to $admxTargetPath" |
| 67 | +Copy-Item -Path $admx -Destination $admxTargetPath -Force |
| 68 | +$admxTargetFullPath = Join-Path -Path $admxTargetPath -ChildPath $admxName |
| 69 | +if (Test-Path -Path $admxTargetFullPath) |
| 70 | +{ |
| 71 | + Write-Verbose "$admxName was installed successfully" |
| 72 | +} |
| 73 | +else |
| 74 | +{ |
| 75 | + Write-Error "Could not install $admxName" |
| 76 | +} |
| 77 | + |
| 78 | +Write-Verbose "Copying $adml to $admlTargetPath" |
| 79 | +Copy-Item -Path $adml -Destination $admlTargetPath -Force |
| 80 | +$admlTargetFullPath = Join-Path -Path $admlTargetPath -ChildPath $admlName |
| 81 | +if (Test-Path -Path $admlTargetFullPath) |
| 82 | +{ |
| 83 | + Write-Verbose "$admlName was installed successfully" |
| 84 | +} |
| 85 | +else |
| 86 | +{ |
| 87 | + Write-Error "Could not install $admlName" |
| 88 | +} |
0 commit comments