Page 1 of 2

Update Computer Description

Posted: Mon Mar 11, 2019 1:20 pm
by TSMike
I am trying to update the Description field of a computer while running a script during a Task Sequence in SCCM. I have been searching and testing code and so far I cannot find anything that works. I do not want to use any snap-ins so I am trying to use OTB PowerShell. I found using ADSI to be a common theme, but like I said, nothing has worked for me. When the code runs, it is running in the native OS, not Win PE.

I have found the code as to establish the LDAP path to the computer object. But every time I use ADSI to try and connect to the object I get a null value. Is ADSI the right path to do this? I attached the code that I have been working with. When I run line 27 ($oComputer = [ADSI]$sLdapName) it returns a null value.

Product: PowerShell Studio 2019 (64 Bit)
Build: v5.6.160
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.15063.0

SCCM image is Windows 10 1709 (64-Bit)

Thank you in advance!

Mike

Re: Update Computer Description

Posted: Mon Mar 11, 2019 1:27 pm
by jvierra
Here is an example of how to get the computer object with ADSystemInfo:

Under SCCM you may have issues because the SCCM agent runs as system.

Re: Update Computer Description

Posted: Mon Mar 11, 2019 1:50 pm
by TSMike
I understand how it returns the DistinguishedName of the computer (CN=<computername>,OU=<ou1>,OU=<ou2>,DC=contoso,DC=com), and the next line gives me the parent OU in which the computer lives (OU=<ou1>,OU=<ou2>,DC=contoso,DC=com). But now I need to connect to the computer object to update any fields.

I believe I need to set a variable to the computer object and then update the Description field. I don't need the user information, just the computer information (sorry, the user portion was just in the code I was testing with. User information is not needed, just the computer information). Here is what I need the code to do yet:

1. Create a variable connected to the AD computer
2. Update the Description field in AD

Re: Update Computer Description

Posted: Mon Mar 11, 2019 1:59 pm
by jvierra
The code I posted shows you how to do this. Use the function and follow the examples.

Re: Update Computer Description

Posted: Mon Mar 11, 2019 2:08 pm
by jvierra
Here is how to update an ADSI object:

Code: Select all

$computerDN = Get-ADSystemInfo | select -expand ComputerName
$c = [adsi]"LDAP://$computerDN"
$c.Description = 'Domain Controller'
$c.CommitChanges()

Re: Update Computer Description

Posted: Mon Mar 11, 2019 2:16 pm
by jvierra
Sorry - I forgot part of the code:

Code: Select all

$computerDN = Get-ADSystemInfo | select -expand ComputerName
$c = [adsi]"LDAP://$computerDN"
$c.Description = 'Domain Controller'
$c.CommitChanges()

Re: Update Computer Description

Posted: Tue Mar 12, 2019 5:10 am
by TSMike
I tried to test the code in the PowerShell console and when trying to set $c to the adsi computer, it doesn't get the computer as intended. When I try to display what $c is set to I get an error. I attached the error message.

Re: Update Computer Description

Posted: Tue Mar 12, 2019 5:29 am
by jvierra
You have AD issues. It is not the code. Check the DN to be sure it looks correct. The code has been working for years.

Just do this part:

$computerDN = Get-ADSystemInfo | select -expand ComputerName
$computerDN # be sure this is correct
[adsi]"LDAP://$computerDN"

Re: Update Computer Description

Posted: Tue Mar 12, 2019 5:33 am
by jvierra
This is what it should look like:


PS C:\scripts> $computerDN = Get-ADSystemInfo | select -expand ComputerName
PS C:\scripts> $computerDN # be sure this is correct
CN=SBS01,OU=Domain Controllers,DC=TESTNET,DC=local
PS C:\scripts> [adsi]"LDAP://$computerDN"


distinguishedName : {CN=SBS01,OU=Domain Controllers,DC=TESTNET,DC=local}
Path : LDAP://CN=SBS01,OU=Domain Controllers,DC=TESTNET,DC=local



PS C:\scripts>

Re: Update Computer Description

Posted: Tue Mar 12, 2019 7:55 am
by mxtrinidad
First, JVierra has giving you an Awesome script that works. I would also try it using .NET which is easy to use and there's lots of documentation (COM Object is old technology).

https://devblogs.microsoft.com/scriptin ... directory/
https://blogs.msdn.microsoft.com/dsadsi ... owershell/

Or, you could use AD cmdlets: https://blogs.technet.microsoft.com/ash ... indows-10/

Now, the script will only update the system description on the Active Directory Services only. This will NOT update the "description" on the physical (or VM). There are two different object: one is AD and the other is CIM/WMI.

So, in order to update system description, use:

$SysDescription = get-wmiobject -Class "Win32_OperatingSystem" -ComputerName "DServer1"
$SysDescriptionn = "Domain Server1"
$SysDescription.Put()

Now, if one description variable you can update both AD and the CIM/WMI.

:)