Update Computer Description

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
TSMike
Posts: 4
Joined: Fri Feb 15, 2019 1:55 pm

Update Computer Description

Post by TSMike » Mon Mar 11, 2019 1:20 pm

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
Attachments
UpdateComputerDescription.ps1
(828 Bytes) Downloaded 21 times

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Update Computer Description

Post by jvierra » Mon Mar 11, 2019 1:27 pm

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.
Attachments
Get-ADSystemInfo.ps1
(880 Bytes) Downloaded 20 times

TSMike
Posts: 4
Joined: Fri Feb 15, 2019 1:55 pm

Re: Update Computer Description

Post by TSMike » Mon Mar 11, 2019 1:50 pm

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

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Update Computer Description

Post by jvierra » Mon Mar 11, 2019 1:59 pm

The code I posted shows you how to do this. Use the function and follow the examples.

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Update Computer Description

Post by jvierra » Mon Mar 11, 2019 2:08 pm

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()

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Update Computer Description

Post by jvierra » Mon Mar 11, 2019 2:16 pm

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()

TSMike
Posts: 4
Joined: Fri Feb 15, 2019 1:55 pm

Re: Update Computer Description

Post by TSMike » Tue Mar 12, 2019 5:10 am

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.
Attachments
Error_01.png
Error_01.png (28.24 KiB) Viewed 990 times

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Update Computer Description

Post by jvierra » Tue Mar 12, 2019 5:29 am

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"

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Update Computer Description

Post by jvierra » Tue Mar 12, 2019 5:33 am

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>

User avatar
mxtrinidad
Site Admin
Posts: 328
Joined: Sun Mar 03, 2013 12:42 pm

Re: Update Computer Description

Post by mxtrinidad » Tue Mar 12, 2019 7:55 am

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.

:)

Locked