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.
LVL 14
Ben HartAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
I don't see any reason for line 7.
The reason you're getting the description as you see now is that $username contains an object with a property called "username".  You either need to change the contents of the variable so that it is only string, or only reference the property of the variable when calling the Set-ADComputer command.

For the first case, you could use either reference the property using dot notation (as Toni Uranjek has shown)
$username = (get-wmiobject Win32_ComputerSystem -computername $computer.Name).username

Open in new window

or using the -expandProperty parameter of Select-Object
$username = (get-wmiobject Win32_ComputerSystem -computername $computer.Name | select -expandProperty username

Open in new window


For the second case (if you left line 6 as is), you could use
set-adcomputer -identity $computer -Description $username.username

Open in new window

0
Ben HartAuthor Commented:
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.
0
footechCommented:
Because you changed from using $computer.name to $computer on line 6.  However, you could also keep line 6 as is and change line 1 to:
$computers = Get-ADComputer -Properties  OperatingSystem -Filter "OperatingSystem -Like 'Windows 7*'" -erroraction SilentlyContinue | Select -expandProperty Name

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ben HartAuthor Commented:
Nice, nice... thanks.

So what about being able to not overwrite if the Description field is populated already?
0
footechCommented:
If you don't want to ever overwrite the description field, the best way is to filter your list of computers.
$computers = Get-ADComputer -filter {Description -notlike "*"  -and OperatingSystem -Like "Windows 7*"} -erroraction SilentlyContinue | Select -expandProperty Name

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ben HartAuthor Commented:
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.
0
footechCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.