DNS设置更新帮助以及输出结果

时间:2014-11-13 16:35:05

标签: powershell

两部分问题,需要帮助。

最终目标是找到AD中所有在线且在设置中具有特定DNS条目的成员服务器。

我想将这些服务器列表转储成CSV,就像它在屏幕上显示的那样。所以我可以在做下一部分问题之前先复习一下。每当我添加Out-File或Export-CSV时,我都会获得每个服务器的头信息和一堆间距。格式化并不反映我在屏幕上看到的内容。

如果符合我指定的条件,我还想添加额外的代码,以便将这些DNS设置更新为另一个。我两个都有点麻烦。所有成员服务器都是2008r2或2003.所以我不能使用新的Powershell DNS代码。

##Query for computer objects with a Server OS
$servers = get-adcomputer -LDAPFilter "(&(objectcategory=computer)(operatingsystem=*server*))"|select-object name

$arr=@()
##For every server found above do the following.
foreach($server in $servers)
{
    ##Test to see if Server is online through Ping.
    $test = test-connection -cn $server.name -count 1 -quiet

    ##If server is online do the following.
    if($test -match 'True')
    {
        ##From those servers that are found online, check to see if they are classified as a "Member Server".  We don't want DC's.
        $memberservers = gwmi win32_computersystem -cn $server.name -filter "domainrole='3'"|Select-Object name

        ##For some reason i get an empty server name from the list above.  So i get rid of those.
        if($memberservers.name -ne $null)
        {
            ##Find out if any of those servers have the following IP as there DNS settings.
            $DNSResults=gwmi win32_networkadapterconfiguration -cn $memberservers.name|`
            where-object{$_.dnsserversearchorder -match "10.100.1.50" -and $_.ipenabled -match "true"}|`
            select-object dnshostname,@{Name='dnsserversearchorder';Expression={[string]::join(";",($_.dnsserversearchorder))}}

            $arr+=$DNSResults
            $DNSResults.SetDNSServerSearchOrder(10.100.1.51,10.100.1.52)
        }
    }
}
$arr|Export-csv C:\results.csv

1 个答案:

答案 0 :(得分:0)

关于你的第一个问题:

在foreach循环之前创建一个空数组

$arr=@() 

然后在循环中将输出添加到数组

$obj = gwmi win32_networkadapterconfiguration -cn $memberservers.name ...  
$arr+= $obj

并在循环导出后

$arr | export-csv