MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4<#5.Synopsis6 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 installes PowerShell Core Administrative Templates for Windows.9.Notes10 The PowerShellCoreExecutionPolicy.admx and PowerShellCoreExecutionPolicy.adml files are11 expected to be at the location specified by the Path parameter with default value of the location of this script.12#>13[CmdletBinding()]14param15(16 [ValidateNotNullOrEmpty()]17 [string] $Path = $PSScriptRoot18)19Set-StrictMode -Version 3.020$ErrorActionPreference = 'Stop'21 22function Test-Elevated23{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 $IsWindows34 35if (-not $IsWindowsOs)36{37 throw 'This script must be run on Windows.'38}39 40if (-not (Test-Elevated))41{42 throw 'This script must be run from an elevated process.'43}44 45if ([System.Management.Automation.Platform]::IsNanoServer)46{47 throw 'Group policy definitions are not supported on Nano Server.'48}49 50$admxName = 'PowerShellCoreExecutionPolicy.admx'51$admlName = 'PowerShellCoreExecutionPolicy.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)58foreach ($file in $files)59{60 if (-not (Test-Path -Path $file))61 {62 throw "Could not find $($file.Name) at $Path"63 }64}65 66Write-Verbose "Copying $admx to $admxTargetPath"67Copy-Item -Path $admx -Destination $admxTargetPath -Force68$admxTargetFullPath = Join-Path -Path $admxTargetPath -ChildPath $admxName69if (Test-Path -Path $admxTargetFullPath)70{71 Write-Verbose "$admxName was installed successfully"72}73else74{75 Write-Error "Could not install $admxName"76}77 78Write-Verbose "Copying $adml to $admlTargetPath"79Copy-Item -Path $adml -Destination $admlTargetPath -Force80$admlTargetFullPath = Join-Path -Path $admlTargetPath -ChildPath $admlName81if (Test-Path -Path $admlTargetFullPath)82{83 Write-Verbose "$admlName was installed successfully"84}85else86{87 Write-Error "Could not install $admlName"88}89 