> ## Documentation Index
> Fetch the complete documentation index at: https://help.gorelo.io/llms.txt
> Use this file to discover all available pages before exploring further.

# تدوير كلمات مرور المسؤول المحلي

> أتمتة تدوير كلمات مرور المسؤول المحلي في Gorelo بنص PowerShell ينشئ الحساب ويعيّن كلمة مرور آمنة ويخزّنها في حقل مخصص.

يساعدك هذا الدليل على إعداد تدوير تلقائي لكلمات مرور المسؤول المحلي في Gorelo. ينشئ النص البرمجي حساب مسؤول محلي (إذا لم يكن موجودًا)، ويعيّنه لمجموعة المسؤولين المحليين، ويعيّن كلمة مرور عشوائية آمنة، ويخزّن كلمة المرور تلك في Gorelo لاسترجاعها بسهولة.

<Steps>
  <Step title="إنشاء حقل الأصل المخصص.">
    1. انتقل إلى **Settings** > **Assets** > **[Custom Fields](https://app.gorelo.io/admin/admin-settings#asset#assetcustomfields).**
    2. أضف حقلًا مخصصًا بالتفاصيل التالية:
       * **Name**: Local Admin Password
       * **Variable**: localadminpassword
       * **Type**: Text
       * فعّل **Show on Asset Detail** و **Blue value**.
    3. انقر على **Save.**
  </Step>

  <Step title="إنشاء النص البرمجي.">
    1. انتقل إلى **[Scripts](https://app.gorelo.io/asset/script-list).**
    2. أنشئ نصًا برمجيًا جديدًا بالتفاصيل التالية:
       * **Name**: 🔐 Set-LocalAdminPassword
       * **Platform**: Windows
       * **Content**: *\[انسخ نص PowerShell البرمجي المقدّم أدناه]*
    3. انقر على **Save.**

    ```powershell theme={null}
    # =========================================================================
    # Simple Local Admin Password Management Script for Gorelo RMM
    # =========================================================================

    # Configuration variables - change as needed
    $localAdminAccount = "localadmin"
    $accountFullName = "Local Administrator"
    $accountDescription = ""
    $hideFromLogonScreen = $true  # Set to $false to show the account on logon screen

    try {
        # Generate a strong random password
        $CharSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{}|;:,.<>?"
        $Password = ""
        $Random = New-Object System.Random
        
        # Create a 16-character random password
        1..16 | ForEach-Object { $Password += $CharSet[$Random.Next(0, $CharSet.Length)] }
        
        # Check if the account exists
        $userExists = Get-LocalUser -Name $localAdminAccount -ErrorAction SilentlyContinue
        
        if (-not $userExists) {
            # Create the account if it doesn't exist
            $securePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
            New-LocalUser -Name $localAdminAccount -Password $securePassword -FullName $accountFullName -Description $accountDescription -AccountNeverExpires | Out-Null
            Add-LocalGroupMember -Group "Administrators" -Member $localAdminAccount
            Write-Output "Created local admin account: $localAdminAccount"
        } else {
            # Update password if account exists
            $securePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
            Set-LocalUser -Name $localAdminAccount -Password $securePassword -FullName $accountFullName -Description $accountDescription
            Write-Output "Updated password for: $localAdminAccount"
            
            # Check if user is already in Administrators group, add if not
            $adminGroup = Get-LocalGroupMember -Group "Administrators" -ErrorAction SilentlyContinue
            $isAdmin = $adminGroup | Where-Object { $_.Name -like "*\$localAdminAccount" -or $_.Name -eq $localAdminAccount }
            
            if (-not $isAdmin) {
                Add-LocalGroupMember -Group "Administrators" -Member $localAdminAccount
                Write-Output "Added $localAdminAccount to Administrators group"
            }
        }
        
        # Configure account visibility on logon screen
        if ($hideFromLogonScreen) {
            # Hide the account from logon screen
            $registryPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"
            if (-not (Test-Path $registryPath)) {
                New-Item -Path $registryPath -Force | Out-Null
            }
            Set-ItemProperty -Path $registryPath -Name $localAdminAccount -Value 0 -Type DWORD -Force
            Write-Output "Account hidden from logon screen"
        } else {
            # Show the account on logon screen (by removing the registry entry if it exists)
            $registryPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"
            if (Test-Path $registryPath) {
                if (Get-ItemProperty -Path $registryPath -Name $localAdminAccount -ErrorAction SilentlyContinue) {
                    Remove-ItemProperty -Path $registryPath -Name $localAdminAccount -Force
                }
            }
            Write-Output "Account visible on logon screen"
        }
        
        # Store the password in Gorelo RMM
        GoreloAction -SetCustomField -Name "asset.localadminpassword" -Value $Password
        Write-Output "Password stored in custom field"
        
    } catch {
        # Output error to console for on-demand runs
        Write-Error "Error managing local admin account: $_"
        exit 1
    }
    ```
  </Step>

  <Step title="نشر النص البرمجي عبر سياسة.">
    1. انتقل إلى **[Assets](https://app.gorelo.io/asset/asset-list).**
    2. حدّد أي أصل تم تشغيل النص البرمجي عليه.
    3. اعرض قسم **Custom Fields** لرؤية Local Admin Password المخزّنة.
    4. انقر على أيقونة الكشف لعرض كلمة المرور عند الحاجة.
  </Step>
</Steps>

## تخصيص النص البرمجي

يتضمن النص البرمجي عدة متغيرات في الأعلى يمكنك تعديلها:

* `$localAdminAccount` = "localadmin" # اسم المستخدم لحساب المسؤول المحلي
* `$accountFullName` = "Local Administrator" # الاسم الكامل للحساب
* `$accountDescription` = "" # وصف الحساب (اختياري)
* `$hideFromLogonScreen` = `$true` # اضبط على `$false` لعرض الحساب على شاشة تسجيل الدخول

اضبط هذه المتغيرات لتناسب احتياجات مؤسستك قبل نشر النص البرمجي.
