我正在尝试查询服务器列表并将 DNS 服务器导出到电子表格。如果我对一台服务器运行以下命令,我会被拒绝访问:
Get-DnsClientServerAddress -CimSession server-name -InterfaceAlias ethernet -AddressFamily IPv4 |ForEach-Object ServerAddresses
Get-DnsClientServerAddress: server-name: Cannot connect to CIM server. Access is denied.
我假设我需要使用
Invoke-VMScript
来运行它,但我需要帮助以获得可读的 CSV 输出。我正在寻找这样的东西:
SERVER NAME DNS SERVERS
server-1 10.0.0.1
server-1 10.0.0.2
server-2 10.0.0.1
如何查询计算机列表以查找其 DNS 服务器并将其导出到 CSV 文件?
我有这个用于查询服务的代码,但我不知道如何修改它来获取 DNS 服务器:
$user = 'domain\username'
$pwd = 'password' | ConvertTo-SecureString -Force -AsPlainText
$credential = New-Object System.Management.Automation.PsCredential($user, $pwd)
$code = @'
$obj = New-object PSObject -Property @{
Name = 'Service does not exist'
Status = 'Not Installed'
}
$service = Get-Service -Name SERVICE -ErrorAction SilentlyContinue
if($service)
{
$obj.Name = $service.Name
$obj.Status = $service.Status
}
$obj | ConvertTo-Csv
'@
Get-VM -Name (Get-Content -Path "C:\folder\server-list.txt") -PipelineVariable vm |
ForEach-Object -Process {
$o = Invoke-VMScript -VM $vm -ScriptText $code -ScriptType Powershell -GuestCredential $credential
$o.ScriptOutput | ConvertFrom-Csv |
Select-Object -Property @{N = 'VM'; E = { $vm.Name } }, Name, Status
} | Export-Csv -Path "C:\folder\server-results.csv" -NoTypeInformation -UseCulture
查询服务器列表并导出DNS服务器
Invoke-Command
与 -ComputerName
参数一起使用以传递 $machineList
。使用 Get-Credential
在运行时输入凭据,而不是在脚本中嵌入密码。将 $cred
传递给 -Credential
参数以定义在远程计算机上执行命令的安全上下文。
注意: 我假设您已处理导出到 CSV 的操作,并且已在所涵盖的脚本块中处理任何必要的服务存在条件。不过,如果需要的话,我很乐意协助进行一些琐碎的调整。
$cred = Get-Credential "domain\username";
$machineList = Get-Content -Path "C:\temp\List.txt";
Invoke-Command -ComputerName $machineList -ScriptBlock {Get-DnsClientServerAddress -AddressFamily IPv4} -Credential $cred |
Where-Object {$_.ServerAddresses.Count -ge 1} |
ForEach-Object {
$serverName = $_.PSComputerName;
$_.ServerAddresses | ForEach-Object {
[PSCustomObject]@{
'Server Name' = $serverName;
'DNS Server' = $_;
}
}
};
Server Name DNS Server
----------- ----------
homocide002 10.105.50.15
homocide002 10.105.51.15
homocide002 10.105.51.10
gangunit001 10.105.50.15
gangunit001 10.105.51.15
gangunit001 10.105.51.10
$cred = Get-Credential "domain\username";
$machineList = Get-Content -Path "C:\temp\List.txt";
Invoke-Command -ComputerName $machineList -ScriptBlock {Get-DnsClientServerAddress -AddressFamily IPv4} -Credential $cred |
Where-Object {$_.ServerAddresses.Count -gt 0} |
Select-Object @{n="Server Name";e={$_.PSComputerName}}, @{n="DNS Server";e={$_.ServerAddresses}};
Server Name DNS Server
----------- ----------
homocide002 {10.105.50.15, 10.105.51.15, 10.105.51.10}
gangunit001 {10.105.50.15, 10.105.51.15, 10.105.51.10}
$cred = Get-Credential "domain\username";
$machineList = Get-Content -Path "C:\temp\List.txt";
Invoke-Command -ComputerName $machineList -ScriptBlock {Get-DnsClientServerAddress -AddressFamily IPv4} -Credential $cred |
Where-Object {$_.ServerAddresses.Count -gt 0} |
Select-Object @{n="Server Name";e={$_.PSComputerName}}, @{n="DNS Server";e={$_.ServerAddresses.split(',') -join '; '}};
Server Name DNS Server
----------- ----------
homocide002 10.105.50.15; 10.105.51.15; 10.105.51.10
gangunit001 10.105.50.15; 10.105.51.15; 10.105.51.10
$cred = Get-Credential "domain\username";
$machineList = Get-Content -Path "C:\temp\List.txt";
Invoke-Command -ComputerName $machineList -ScriptBlock {Get-DnsClientServerAddress -AddressFamily IPv4} -Credential $cred |
Where-Object {$_.ServerAddresses.Count -gt 0} |
Select-Object @{n="Server Name";e={$_.PSComputerName}}, @{n="DNS Server";e={$_.ServerAddresses.split(',') -join ', '}};
Server Name DNS Server
----------- ----------
homocide002 10.105.50.15, 10.105.51.15, 10.105.51.10
gangunit001 10.105.50.15, 10.105.51.15, 10.105.51.10