简体   繁体   English

Powershell脚本,用于将AD计算机与文本文件进行比较,并更改这些计算机上的注册表服务,然后编写脱机的计算机

[英]Powershell script to compare AD computers to text file and change registry service on those computer and then write the computers that were offline

I'm trying to filter out computers that have already ran the script (that enables remote registry service) within AD from a list in a text file 我正在尝试从文本文件中的列表中筛选出已在AD中运行脚本(启用远程注册表服务)的计算机

$NamesFromFile = Get-Content 
C:\scripts\Inventory\offlineRemoteRegStartupWorkstations.txt

$computers = get-adcomputer -Filter * | Where-Object { 
$_.Name.SubString(1) -in $NamesFromFile } 
foreach ($computer in $computers) 
{ 
if (Test-Connection -count 1 -computer $computer.Name -quiet){ 
Write-Host "Updating system" $computer.Name "....." -ForegroundColor 
Green 
Set-Service –Name remoteregistry –Computer $computer.Name -StartupType 
Automatic 
Get-Service remoteregistry -ComputerName $computer.Name | start-service 
} 
else 
{ 
Write-Host "System Offline " $computer.Name "....." -ForegroundColor Red 
echo $computer.Name >> C:\scripts\Inventory\offlineRemoteRegStartup.txt} 
}

no errors just blank 没有错误只是空白

Below a re-write of your script. 下面重写你的脚本。

Because you test if only the first character of the computer name is in the list of computers, your $computers variable will remain empty, so nothing happens. 因为您测试计算机名称的第一个字符是否只在计算机列表中,所以$computers变量将保持为空,因此没有任何反应。

Also, I think it would be wise to add a check if the startup type of the RemoteRegistry service is not already set to Automatic, because after all.. the computernames you read in from the file may not be accurate. 此外,我认为添加一个检查是否RemoteRegistry服务的启动类型尚未设置为自动是明智的,因为毕竟..您从文件中读取的计算机名可能不准确。

To avoid having to use $computer.Name all the time, I use a Select-Object -ExpandProperty Name , so we only have to run through a list of strings. 为了避免必须一直使用$computer.Name ,我使用Select-Object -ExpandProperty Name ,因此我们只需要运行一个字符串列表。

$NamesFromFile = Get-Content -Path 'C:\scripts\Inventory\offlineRemoteRegStartupWorkstations.txt' | Sort-Object -Unique

Get-ADComputer -Filter * | 
    Where-Object { $NamesFromFile -contains $_.Name } |  # if the computer name is in the list
    Select-Object -ExpandProperty Name |                 # we're only interested in the Name property
    ForEach-Object {
        # the automatic variable '$_' represents a single computername from the list
        if (Test-Connection -Count 1 -ComputerName $_ -Quiet) {
            # test if the RemoteRegistry service startup type is not already Automatic
            # you can do the same with (Get-WmiObject -Class Win32_Service -Filter "Name='RemoteRegistry'" -ComputerName $_)
            # only slower..
            if ((Get-CimInstance -Class Win32_Service -Filter "Name='RemoteRegistry'" -ComputerName $_).StartMode -ne 'Auto') {
                Write-Host "Updating system '$_'....." -ForegroundColor Green
                Set-Service –Name RemoteRegistry –Computer $_ -StartupType Automatic
                Get-Service -Name RemoteRegistry –Computer $_ | Start-Service
            }
            else {
                Write-Host "RemoteRegistry service startup type already Automatic on computer '$_'....." -ForegroundColor Yellow
            }
        }
        else {
            Write-Host "System Offline '$_'....." -ForegroundColor Red
            Add-Content -Path 'C:\scripts\Inventory\offlineRemoteRegStartup.txt' -Value $_
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较用于AD计算机的两个Powershell脚本的输出 - Compare outputs of two powershell scripts for AD computers PowerShell脚本删除AD计算机上用户中的文件夹 - PowerShell script to delete folder in Users on AD computers 如何删除AD中的计算机并通过电子邮件发送给您自己的计算机 - How to remove computer in AD and email the computers that were removed to yourself 获取 AD 计算机 PowerShell - Get AD Computers PowerShell 多台计算机可以使用 PowerShell 将数据写入一台远程计算机中的单个文件吗? - Is it okay for multiple computers write data with PowerShell to a single file in one remote computer? 需要帮助编写Powershell脚本以在AD用户和计算机>> OU >>分发组中将Extension Attribute 1值更改为“Staff”? - Need help in writing Powershell script to change the Extension Attribute 1 value to ‘Staff’ in AD Users and Computers >> OU >> Distributions groups? Powershell脚本可在域中的所有计算机上运行-等待计算机联机 - Powershell script to run on all computers in domain - wait until computer is online PowerShell脚本可排除在AD中被禁用的计算机列表 - PowerShell script to exclude a list of computers from getting disabled in AD 需要 PowerShell 脚本的帮助来清理 AD 中符合标准的计算机 - Need assistance with PowerShell Script to clean up computers in AD that meet criteria 显示AD中所有计算机的计算机信息 - Display computer information for all computers in AD
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM