autoFill description AD

Hi all,

I would like to create a script that modifies the description field of the computers in my domain.

I want the ps1 file to query the AD in order to retrieve the last user (win2000) connected and the IP address and then register it in the description field of the computer object.

for exemple :

Script :

CHECK COMPUTER 1 => user “TATA” found, ip adresse : “10.0.0.1” found , copy the information and paste in description field of “Computer 1”

CHECK COMPUTER 2 => user “TOTO” found, ip …

I don’t want a script that runs on host machines that don’t have RSAT tools, I’ll run the script every 20 minutes

For moment i have this :

Import-Module ActiveDirectory

REMOTE COMPUTER

$ComputerName = ‘P5113931’

OR

LOCAL COMPUTER

#ComputerName = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name

$a = Get-Date
$b = $a.ToShortDateString()
$c = $a.ToShortTimeString()
$d = Get-WmiObject -computername $ComputerName Win32_Computersystem | Select -Expand model

if ($d -eq $null) {$d = ‘0000’}
#$e = $d -replace ‘\D+(\d+)\D+’,‘$1’
$Description = “Damien script lounes * $b $c * $d”

Set-ADComputer $ComputerName -description $Description

i can modify one per one but is not my objectif.

Thank :slight_smile: a lot !

Lounès,
Welcome to the forum. :wave:t4:

The AD does not have this information. You would need to query the eventlog of a computer to retrieve last logged on user.

What does that mean? Are you really talking about Windows 2000? The support for Windows 2000 ended 12 years ago.

Depending on your infrastructure that might not be the best idea for client computers. Ideally you already have a software deployment and metering solution in place? Or you could use GPOs to trigger certain tasks on the local computers. They don’t need to access the AD - they just should collect the needed information and send it to a central location.

Import-Module ActiveDirectory

Since PowerShell version 3 properly installed modules will be loaded automatically by PowerShell if needed. So no need to do this explicitly. :wink:

$ComputerName = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name

That could be replaced by

$ComputerName = $ENV:COMPUTERNAME

And you should not use Get-WmiObject anymore. Instead use Get-CimInstance

If you want to run commands on more than one remote computer you can use loops. Either with

or

or you could even pass an array of computernames to

Please always read the help for the cmdlets you’re about to use completely including the examples to learn how to use them.

In addition, the task you’re asking about is quite common and has been posted already several times throughout the internet. So you don’t need to re-invent the wheel again. You can just use what others already shared with hte world. :wink:

And last but not least … When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

thank you for welcoming to this forum.

we already have tools that allow you to report information (GLPI in particular) but we want this script in addition (launched every two hours via scheduled task)

only administrator accounts can increment the AD, I want everything to be centralized on a server

my end goal would be to
Uploading: image.png…
Uploading: 1.png…

I trie this but is doesn’t work :

# Define the OU where the script will be applied
$NewComputers = Get-ADComputer -Filter * -SearchBase "OU=Computers,OU=xStaging OU,DC=company,DC=com"

# Define AD description
$ComputerDescription = (Get-WmiObject -Class Win32_OperatingSystem).Description

# Define attributes for computer description
foreach ($NewComputer in $NewComputers)
{
    # Update properties
    $NewComputer.Description = $ComputerDescription

    # Update the computer in AD
    $ADComputerName = Get-WmiObject -Class Win32_OperatingSystem -Property CSName
    if ($ADComputerName -eq $env:COMPUTERNAME) {Set-ADComputer -Instance $NewComputer}
}

You did not read my answer and you did not read the help topics I posted. :face_with_diagonal_mouth:

That’s querying the local computer for the description what’s in my experiences almost never filled in. Why do you want to use this property?

I’d recommend to choose variable names that are more distinct. Like $ComputerName in $ComputerList. But that’s just cosmetics. :wink:

Here you fill in a variable for a computer from your list of computers and assign the description from the local computer you populated before OUTSIDE the loop!!

Here you query agan the LOCAL computer where the script runs.

You compare the computername from the local computer determined by WMI with the computername from the local computer determined by environment variable. That’s in 99.9999 % of all cases true.

To use the instance method for Set-AdComputer you have to query the particular computer from the AD first and change some of its properties. Please read the help including the examples - especially example 4!

… and please do not post images of code or error messages or console output. Instead post the plain text and format it as code.

Thanks in advance

1 Like