I'm trying to assign permissions to AD OU's using powershell script that is supposed to create a new object of type System.Security.Principal.NTAccount
and System.DirectoryServices.ActiveDirectoryAccessRule
, The code I have right now is working without alternate credentials but now I need to use the same code with alternate credentials.
Working Code without Alternate Credentials:
$ADSI = [ADSI]"LDAP://$OUPath"
$NTAccount = New-Object System.Security.Principal.NTAccount($ClientGroupED)
$IdentityReference = $NTAccount.Translate([System.Security.Principal.SecurityIdentifier])
$ActiveDirectoryRights = [System.DirectoryServices.ActiveDirectoryRights] "GenericAll"
$AccessControlType = [System.Security.AccessControl.AccessControlType] "Deny"
$Inherit = [System.DirectoryServices.ActiveDirectorySecurityInheritance] "All" #All, Children, Descendents, None, SelfAndChildren
$ACE = New-Object System.DirectoryServices.ActiveDirectoryAccessRule($IdentityReference,$ActiveDirectoryRights,$AccessControlType,$Inherit)
$ADSI.psbase.ObjectSecurity.SetAccessRule($ACE)
$ADSI.psbase.commitchanges()
I tried passing the alternate credentials using -Credential $Cred
and also passed the -ArgumentList $Cred
while calling New-Object neither works. Need some help in this issue.