Share via


Powershell to Disable "Protect from Accidental Deletion" on List of computers

Question

Wednesday, July 15, 2020 3:04 PM

Hello, I need some help please

I used this script in a LAB environment with ADDS on WinServer 2012 R2 successfully.

$Computers = get-content C:\Users\Administrator\Desktop\Servers.txt
ForEach ($Computer In $Computers) {
    $DN = (Get-ADComputer -LDAPFilter "(Name=*$Computer*)").distinguishedName
    If ($DN) {Set-ADObject -Identity $DN -ProtectedFromAccidentalDeletion $True}
}

When running the same script in the production environment there was a conversion failure stating that the Method is not supported

Set-ADObject : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADObject' required by parameter 'Identity'. Specified method is not supported.
At line:4 char:38
+     If ($DN) {Set-ADObject -Identity $DN -ProtectedFromAccidentalDele ...

All replies (5)

Wednesday, July 15, 2020 4:06 PM âś…Answered | 1 vote

Name=*$Computer* for some reasone you have asterisks in your filter so if your $computer will be a part of more then 1 ad computer object (names), then command will return more than 1 records that not allowed by identity parameter.

i do not know what storred in your server.txt and do you need asterisks or not. If you have full server names - just remove *'s from your filter, otherwise use $dn | foreach {set-adobject -Identity $_ ...} construction

The opinion expressed by me is not an official position of Microsoft


Wednesday, July 15, 2020 3:12 PM

1 dn variable may contain more then 1 objects inside

2 identity param does not accept array an an input

The opinion expressed by me is not an official position of Microsoft


Wednesday, July 15, 2020 3:45 PM

sorry, but I didn't understand
Do I need to remove a $ DN variable?


Wednesday, July 15, 2020 5:37 PM | 1 vote

my .txt file has the full name of the servers.
example:
server1
server2
server3

I did as your guidance removes the * and the script worked both in the LAB and Production environments.

Thank you for your attention!
I want to improve myself more in powershell


Wednesday, July 15, 2020 5:47 PM

ok, thanks for your feedback

in your case when $computername was eq "server1" your filter with asterisks also matches with "notaserver1", "server11" etc.

The opinion expressed by me is not an official position of Microsoft