R
R
Randomiser2016-01-21 16:49:16
PowerShell
Randomiser, 2016-01-21 16:49:16

Where is the error in the PowerShell code?

Began to deal with powershell.
The task is to fill in the description field in AD in computer accounts, which should contain the Display Name of the current logged in user.
I know that there are 2 ways:
1. This is a group policy for logon and logoff user.
2. According to the schedule, run a script that will poll all machines in the container
The first option was discarded due to the fact that the domain users group will have to be delegated the rights to make changes to the description field. It would not be desirable to expand to users of the right. I am more inclined to the fact that the less rights the user has, the less problems I have.
Second option. Wrote a script on PS.

cls
Import-Module ActiveDirectory
$comps = Get-ADComputer -filter {enabled -eq $true} -SearchBase "OU=computers,ou=Локация 1,dc=РОГА,dc=Копыта,dc=ru" | Select-Object -property "Name","Description"
$comps | ForEach-Object {
  if (Test-Connection -ComputerName $_.name -Quiet) {
    $curentuser = (get-wmiobject Win32_ComputerSystem -ComputerName $_.name).UserName
      if ($curentuser -ne $NULL) {
        $curentusername = Get-ADUser $curentuser.Split("\")[1] | select -ExpandProperty Name
        $curenthostdescription = Get-ADComputer $_.name -Properties description | select -ExpandProperty description
        if ($curenthostdescription -ne $curentusername) {
          Set-ADComputer $_.name -Description "$curentusername"
          Write-Host "Описание компьютера",$_.name,":",$curenthostdescription,"было изменено на",$curentusername
          Clear-Variable -Name curentuser
          Clear-Variable -Name curentusername
          Clear-Variable -Name curenthostdescription
        }
      }
      else {Write-Host $_.name " - Проблема со считыванием текущего пользователя"}
        
    } else {Write-Host $_.name " - Компьютер выключен"}
}

There are several questions:
1. When executing the script, an error appears like:
Select-Object : Cannot process argument because the value of argument "obj" is null. Change the value of argument "obj" to a non-null value.
At C:\Users\uvinv\AppData\Local\Temp\5e8ab969-4c0d-40a9-9aa7-ea268e8e7921.ps1:10 char:85
+                 $curenthostdescription = Get-ADComputer $_.name -Properties description | select <<<<  -ExpandProperty description
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], PSArgumentNullException
    + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.SelectObjectCommand

As I understand it, the error is due to the fact that the description field is empty. I would like to know how to solve this problem
2. On some computers (get-wmiobject Win32_ComputerSystem -ComputerName host).UserName. Returns an empty value even though the user is logged on to the computer. I do not understand what it is connected with.
3. I wanted to hear in general criticism and suggestions on the script that can be optimized. And in general, is it worth solving such problems on PS? The script takes a long time to complete. Maybe I invented the wheel and there is a best practice on this issue.
Thanks in advance for your attention :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2016-01-21
@yellowmew

I won’t tell you about the script now, tomorrow we’ll see if it’s still relevant for you)
In general, IMHO, it’s more correct to create a task on all computers that is tied to the interactive logon event.
To do this, you do not need to give users rights: the description will be recorded from the user whose credentials are specified in the script.
Well, it’s probably worth monitoring not only logins but also logouts in order to know which user last used the computer and broke it

A
Anatoly, 2016-01-27
@rbobot

So it's easier to add filling in the accounting attribute to the logon script and you're done.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question