Update Computer Description

Ask your 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.
This topic is 5 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
TSMike777
Posts: 5
Last visit: Wed Mar 20, 2024 1:17 pm

Update Computer Description

Post by TSMike777 »

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 130 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Update Computer Description

Post 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.
Attachments
Get-ADSystemInfo.ps1
(880 Bytes) Downloaded 140 times
TSMike777
Posts: 5
Last visit: Wed Mar 20, 2024 1:17 pm

Re: Update Computer Description

Post by TSMike777 »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Update Computer Description

Post by jvierra »

The code I posted shows you how to do this. Use the function and follow the examples.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Update Computer Description

Post 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()
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Update Computer Description

Post 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()
TSMike777
Posts: 5
Last visit: Wed Mar 20, 2024 1:17 pm

Re: Update Computer Description

Post by TSMike777 »

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 3442 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Update Computer Description

Post 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"
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Update Computer Description

Post 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>
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Update Computer Description

Post 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.

:)
This topic is 5 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked