[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] powershell 运行 Add-LocalGroupMember 报错

运行环境为win11LTSC24H2这个版本,写了一个加域的脚本工具,在加域时把当前用户添加到本地管理员组内,和允许本地登陆
但是 Add-LocalGroupMember -Group "administrators"  -Member $PC_UserName   这一句子就会报错,Add-LocalGroupMember未将对象引用设置到对旬的实例,但这个脚本在win10下是能正常运行的。看看有大神给看看呢,十分感谢。

[code]
$path = Get-Location
Set-Location  -path $path
# Write-Host "脚本文件路径是: $PSScriptRoot"
# Write-Host "准备切换到" $PSScriptRoot
Set-Location  $PSScriptRoot
$username = "Admin-69800090"  
$domain = "synmatic.com"
$passwordFile = ".\pwd.txt"
$keyFile = ".\ase.key"
$key = Get-Content $keyFile
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, (Get-Content $passwordFile | ConvertTo-SecureString -Key $key)
$key = Get-Content $keyFile
$Computername = $env:COMPUTERNAME
$DomainController = "AD-02"  
# 构建 LDAP 查询路径
$ldapPath = "LDAP://$DomainController "
# 创建 DirectoryEntry 对象,使用指定的凭据进行连接
$directoryEntry = New-Object System.DirectoryServices.DirectoryEntry($ldapPath, $credential.UserName, $([Runtime.InteropServices.Marshal]:trToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($credential.Password))))
# 创建 DirectorySearcher 对象
$searcher = New-Object System.DirectoryServices.DirectorySearcher($directoryEntry)



Write-Host "域服务器关键端口连通性和DNS解析测试..."

$Ports = @(
   @{Port = 53; Name = "DNS" }
   @{Port = 88; Name = "Kerberos" }
   @{Port = 389; Name = "LDAP" }
   @{Port = 636; Name = "LDAPS" }
   @{Port = 3268; Name = "Global Catalog" }
   @{Port = 3269; Name = "Global Catalog SSL" }
   @{DnsName = $DomainController; Name = "DomainControllerName" }
)

foreach ($entry in $Ports) {
   if ($entry.ContainsKey("ort")) {
      $port = $entry.Port
      $name = $entry.Name
      # 设置警告偏好为 SilentlyContinue 以抑制警告信息
      $WarningPreference = "SilentlyContinue"
      $TestResult = Test-NetConnection -ComputerName $DomainController -Port $Port    -InformationLevel Quiet -ErrorAction SilentlyContinue
      # # 恢复原来的警告偏好设置
      $WarningPreference = "Continue"
      if ($TestResult) {
         Write-Host "$($Name) 端口 ($($Port)) 连通性正常。" -ForegroundColor Green
      }
      else {
         Write-Host "$($Name) 端口 ($($Port)) 连通性失败,请检查网络连接状态。" -ForegroundColor Red
         # cmd.exe /c pause
         # exit

      }
   }
   elseif ($entry.ContainsKey("DnsName")) {
      $dnsName = $entry.DnsName
      $name = $entry.Name
      $DNSResult = Resolve-DnsName -Name $DnsName   -ErrorAction SilentlyContinue
      if ( $DNSResult) {
         Write-Host "域名 $dnsName ($name) 解析成功,IP 地址为: $($dnsResult.IPAddress)"  -ForegroundColor Green
           
      }
      else {
         Write-Host "域名 $dnsName ($name) 解析失败!"  -ForegroundColor Red
         # return $false
      }
      $NetResult = Test-Connection -ComputerName $DnsName -Count 1 -Quiet
      if ( $NetResult) {
         Write-Host "域名 $dnsName ($name) 网络连通性正常。"  -ForegroundColor Green
      }
      else {
         Write-Host "域名 $dnsName ($name) 网络连通性失败。"  -ForegroundColor Red
      }
   }
}


###以上执行域服务器网络连通性测试函数





if ( $TestResult -eq $true -and $NetResult -eq $true -and $DNSResult.GetType().FullName -eq "Microsoft.DnsClient.Commands.DnsRecord_A" ) {
   Write-Host "到达域服务器网络正常,准备执行加域脚本" -ForegroundColor Green
   # 提示用户输入当前电脑使用的用户名, 如果为空,循环提示输入用户名
   do {
      $UserName1 = Read-Host "请输入当前电脑的用户名,格式为姓名完整的拼音"
      $PC_UserName = $UserName1.Trim()
      if ([string]::IsNullOrWhiteSpace($PC_UserName)) {
         Write-Host "输入的用户名不能为空,请重新输入。"
      }
   } while ([string]::IsNullOrWhiteSpace($PC_UserName))

   ####查询用户名是否存在
   try {
      # 设置查询过滤器,查找指定用户名的用户
      $searcher.Filter = "(&(objectClass=user)(sAMAccountName=$PC_UserName))"
      # 执行查询
      $UserNameresult = $searcher.FindOne()

      if ($UserNameresult) {
         Write-Host "用户名:$PC_UserName 在 $domain 域中存在。"  
   
         do {
            $PC_Name = Read-Host "请输入当前电脑名"
            if ([string]::IsNullOrWhiteSpace($PC_Name)) {
               Write-Host "输入的电脑名不能为空,请重新输入。"
            }
         }while ([string]::IsNullOrWhiteSpace($PC_Name))
         
         ##################查询旧的电脑名在域中是否存在        
         # 设置查询过滤器,查找当前电脑名

         $searcher.Filter = "(&(objectClass=computer)(Name=$PC_Name))"
         # 执行查询
         $OldPC_Nameresult = $searcher.FindOne()
         if ($OldPC_Nameresult) {
            Write-Host "原来电脑名Computername 在 $domain 域中发现“
            $OldPC_Name = $OldPC_Nameresult.GetDirectoryEntry()
            $OldPC_Name.DeleteTree()
            Write-Host "原电脑名:$Computername 在 $domain 域已删除。"
         }
         ##################查询旧的电脑名在域中是否存在
        

         ####识别电脑类型
         $a = $PC_Name.substring($PC_Name.Length - 3, 1) #####格式为 C-IT-CJF-D-1 或 C-PLO-CJF-M-1  或 C-RDLW-M-1 用户自己名下的电脑
         $b = $PC_Name.substring($PC_Name.Length - 6, 1) #####格式为 C-PLO-CJF-M-1-PR 或 C-QA-GXL-M-2-PU 或 C-RDZPF-M-1-PR用户自己名下的公用电脑
         $P = $PC_Name.SUBSTRING(0, 4)                  #####格式为产线电脑 ACYYAS001 取前面四个字条串为ACYY  与生产统一约定格式
         ####识别电脑类型

         if ($PC_Name -eq $Computername) {
            Write-Host "输入的电脑名和原来电脑名一致"  ####不带入新电脑名 -NewName 这个参数
            if (($a -eq "M") -or ($b -eq "M")) {
               Add-Computer -DomainName $domain  -Credential  $Credential  -OUPath "OU=Laptop-Pc,OU=Office,OU=ANC-Computers,DC=synmatic,DC=com"  
               Write-Output "笔记本电脑加域完成,电脑名为 $PC_Name"
            }
      
            if (($a -eq "D") -or ($b -eq "D")) {
               Add-Computer -DomainName $domain  -Credential  $Credential  -OUPath "OU=Desktop-Pc,OU=Office,OU=ANC-Computers,DC=synmatic,DC=com"
               Write-Output "台式机电脑加域完成,电脑名为 $PC_Name"
            }
           
            if ($P -eq "ACYY") {
               Add-Computer -DomainName $domain   -Credential  $Credential  -OUPath "OU=Production,OU=ANC-Computers,DC=synmatic,DC=com"  
               Write-Output "产线电脑加域完成"
         
            }

         }
         else {
            Write-Host "输入的电脑名和原来电脑名不相同"
            if (($a -eq "M") -or ($b -eq "M")) {
               Add-Computer -DomainName $domain -NewName $PC_Name -Credential  $Credential  -OUPath "OU=Laptop-Pc,OU=Office,OU=ANC-Computers,DC=synmatic,DC=com"  
               Write-Output "笔记本电脑加域完成,电脑名为 $PC_Name"
            }
      
            if (($a -eq "D") -or ($b -eq "D")) {
               Add-Computer -DomainName $domain -NewName $PC_Name -Credential  $Credential  -OUPath "OU=Desktop-Pc,OU=Office,OU=ANC-Computers,DC=synmatic,DC=com"
               Write-Output "台式机电脑加域完成,电脑名为 $PC_Name"
            }
      
            if ($P -eq "ACYY") {
               Add-Computer -DomainName $domain  -NewName $PC_Name -Credential  $Credential  -OUPath "OU=Production,OU=ANC-Computers,DC=synmatic,DC=com"  
               Write-Output "产线电脑加域完成"
         
            }
         }
         
         Write-Output "把用户 $PC_UserName 添加本地管理员组内"
         Import-Module Microsoft.PowerShell.LocalAccounts -ErrorAction Stop
         Add-LocalGroupMember -Group "administrators"  -Member $PC_UserName
        

         Write-Output "把用户 $PC_UserName 添加本地允许登陆"
         cmd.exe /c ntrights +r SeInteractiveLogonRight -u $PC_UserName
      
         
      }
      else {
         ####用户名不存在就直接退脚本运行
         Write-Host "用户名:$PC_UserName 在 $domain 域中不存在。"
         Write-Host "用户名不存在,退出加域执行脚本" -ForegroundColor Red
         cmd.exe /c pause
         exit
      }
   
   
   }
   catch {
      Write-Host "用户名查询过程中出现错误:$($_.Exception.Message)"
   }
   


   Write-Host "按空格退出脚本执行,将自动执行重启"
   cmd.exe /c pause  
   cmd.exe /c shutdown -r -t 180 -c "管理员设置的,加域后重启电脑,180秒之后重启"
}
else {
   write-host "到达域服务器网络异常, 检查网络连通性" -ForegroundColor Red
   Write-Host "按空格退出脚本执行"
   cmd.exe /c pause  
   exit
}






#

返回列表