Powershell - 调用命令 - 如果 reg 存在

问题描述 投票:0回答:1

我已经写了那个脚本,但是所有计算机的结果都是一样的。更准确地说,如果我的设备有注册表名称,那么所有人都有它,这不反映现实。如果我的设备有它,也是一样的。 我需要帮助才能理解我的错误。 提前谢谢你

$ADSearchBaseComputers = 'ou=computers,ou=xxx,ou=xx,ou=xxx,dc=xx,dc=xxx,dc=xxx'
$computers= get-adcomputer -filter * -searchbase "$ADSearchBaseComputers" | select -Property SamAccountName

foreach ($computer in $computers)
    {
        
        Invoke-Command {    
                            $RegPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
                            $RegName= Get-ItemProperty -Path $RegPath -Name MTBLogin 

                            
                            if  ($RegName)
                             {
                             Write-Output "Reg Key presente pour $($computer.SamAccountName)"
                             
                             }

                            else
                            {
                            Write-Output "Reg Key non presente pour $($computer.SamAccountName)"
                            }
                       }
    
    }

最初我在循环之前有

$RegPath
$RegName
值所以我将它们移到里面但结果是一样的。

powershell if-statement exists invoke-command
1个回答
0
投票

我会这样做。给 invoke-command 整个计算机名数组将使其并行运行。如果您返回一个对象,您将自动获得 pscomputername。它应该给出注册表项是否存在的结果。

$ADSearchBaseComputers = 'ou=computers,ou=xxx,ou=xx,ou=xxx,dc=xx,dc=xxx,dc=xxx'
$computers= get-adcomputer -filter * -searchbase $ADSearchBaseComputers |
  select -ExpandProperty Name
Invoke-Command $computers {    
  $RegPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'
  $RegName = Get-ItemProperty -Path $RegPath -Name MTBLogin
  [pscustomobject]@{RegName = [boolean]$RegName}
}

RegName PSComputerName RunspaceId
------- -------------- ----------
   True COMP001        d4124bfd-f3d1-4af0-b065-1607627f224a
© www.soinside.com 2019 - 2024. All rights reserved.