Answer the question
In order to leave comments, you need to log in
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 " - Компьютер выключен"}
}
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
Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question