Set-GPOState.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID dae372ad-6807-4b7f-ba13-09d0356a9cd1
 
.AUTHOR ThierryBT
 
.COMPANYNAME
 
.COPYRIGHT ThierryBT
 
.TAGS GPO, Group Policy Object, GPO Tools
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
V1.0 First Version
 
#>


<#
 
.DESCRIPTION
 Enable / Disable your GPO users or computers settings
 
#>
 
Param()

Function Set-GPOState
{
<#
 .SYNOPSIS
 
 .DESCRIPTION
 Set / Unset GPO Configuration
   
 .NOTES
 V1.0
    
 .OUTPUTS
    
 .EXAMPLE
 
 PS C:\Windows\system32> Set-GPOState -GPONAME test -STATE $true -TYPE User
 SetUserEnabledGPO value for GPO test is True
   
 .EXAMPLE
 
 PS C:\Windows\system32> Set-GPOState -GPONAME test -STATE $false -TYPE Computer
 SetComputerEnabled value for GPO test is False
   
 .LINK
 https://msdn.microsoft.com/en-us/powershell
 
 #>


Param 
([Parameter(Mandatory=$true,HelpMessage="Insert GPO Name : ")][String]$GPONAME,
[Parameter(Mandatory=$true,HelpMessage="Insert GPO State : ")][bool]$STATE,
[Parameter(Mandatory=$true,HelpMessage="Insert type State (User, Computer) : ")][ValidateSet("User","Computer")][String]$TYPE
)

$Gpmc = New-Object -ComObject GPMgmt.GPM
$GPMC2 = $gpmc.GetConstants()
$Domain = $gpmc.GetDomain((([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).name),$null,0)

try
        {
        $GPOid=(Get-GPO -Name $GPONAME -Server ([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().PdcRoleOwner.Name)).id
        switch ($TYPE)
        {
        {$TYPE -eq "User"}{$Domain.GetGPO("{$GPOid}").SetUserEnabled($STATE);write-host "SetUserEnabledGPO value for GPO $GPONAME is $STATE";break}
        {$TYPE -eq "Computer"}{$Domain.GetGPO("{$GPOid}").SetComputerEnabled($STATE);write-host "SetComputerEnabled value for GPO $GPONAME is $STATE"}
        }}
catch
        {Write-Warning $error[0]}}