Link to home
Start Free TrialLog in
Avatar of Ben Hart
Ben HartFlag for United States of America

asked on

Powershell syntax help

I have the below script:

$computers = Get-ADComputer -Properties  OperatingSystem -Filter "OperatingSystem -Like 'Windows 7*'" -erroraction SilentlyContinue

foreach ($computer in $computers) {


$username = (get-wmiobject Win32_ComputerSystem -computername $computer.Name | select username)
$descr = $username
set-adcomputer -identity $computer -Description $descr

    }
    

Open in new window


What happens is the computer object description is populated with: @{username=domain\CN}  

I'd like to change the formatting to only display the CN.  But then I'd also like it to not overwrite, for example if I run this on Monday, I want to run it on Wed to populate the objects that were not logged on when it ran on monday but not change the results from Monday... I hope that makes sense.
SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ben Hart

ASKER

Ok I made a change:  

$computers = Get-ADComputer -Properties  OperatingSystem -Filter "OperatingSystem -Like 'Windows 7*'" -erroraction SilentlyContinue

foreach ($computer in $computers) {


$username = (get-wmiobject Win32_ComputerSystem -computername $computer | select -expandProperty username)
#$descr = $username
set-adcomputer -identity $computer -Description $username

    }
    

Open in new window


Now however the entire script fails with RPC unavailable errors on each $computer

However if I run:


PS C:\> $username = (get-wmiobject win32_computersystem -computername "jak00676" | select -expandproperty username)
set-adcomputer -identity "jak00676" -description $username

On any one specific host it works.. it sets teh computer object description as domain\username

Exactly what I want.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice, nice... thanks.

So what about being able to not overwrite if the Description field is populated already?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well not ever but like this now if I am polling currently logged on users.. well if a user is out today then his computers desc field will be empty. If I re-run the script tomorrow when he is here but his co-worker who was here today but gone tomorrow then I will have inconsistencies.
Make sense?
Im not trying to design this to be ran automatically, but maybe for a few subsequent days every quarter so keep a relatively accurate list of users to pcs.
Then you would either have to update it every time, or keep a record of the date that the field was updated, then build in logic that checks the date against current date and decide in what timeframe the field gets updated or not.