Ben Hart
asked on
Powershell syntax help
I have the below script:
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.
$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
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nice, nice... thanks.
So what about being able to not overwrite if the Description field is populated already?
So what about being able to not overwrite if the Description field is populated already?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
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.