开始开发PowerShell功能以向我显示有用的信息。
我正在尝试在未指定任何参数或采用命令行参数(但这是另一个版本)时将其默认设置为localhost。我要解决的主要问题是输出DNSServerSearchOrder。
这是我的代码。
function Get-CompInf {
$ComputerName = "."
$ComputerSystem = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName
$OperatingSystem = Get-WmiObject -Class win32_OperatingSystem -ComputerName $ComputerName
$Bios = Get-WmiObject -Class win32_BIOS -ComputerName $ComputerName
$Net = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $ComputerName |
Where-Object {$_.IPConnectionMetric -ne $null}
# DNS - *** want to fix this so it breaks to one address per line.
$DNSstring = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName . |
Where-Object {$_.IPConnectionMetric -ne $null} |
Select-Object -ExpandProperty DNSServerSearchOrder
$Disk = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $ComputerName |
Where-Object DeviceID -eq "c:"
# Prepare Output
$NetInfo = [ordered]@{
"Computer Name. . ." = $Net.PSComputerName
"Link Speed . . . ." = (Get-NetAdapter | Where-Object status -eq "up" | Select-Object -ExpandProperty LinkSpeed)
"MAC. . . . . . . ." = $Net.MacAddress
"IP Address . . . ." = ($Net.IPAddress.trim("{}"))
"Default Gateway. ." = ($Net.DefaultIPGateway.trim("{}"))
"DHCP Server. . . ." = $Net.DHCPServer
"DNS Servers. . . ." = $DNSstring.Split(',')
"Domain . . . . . ." = $Net.DNSDomain
}
$CompInfo = [ordered]@{
"Manufacturer . . ." = $ComputerSystem.Manufacturer
"Model. . . . . . ." = $ComputerSystem.Model
"Service Tag. . . ." = $Bios.SerialNumber
"OS. . . . . . . ." = $OperatingSystem.Caption
"OS Build . . . . ." = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\" | Select-Object -expandproperty ReleaseId)
"OS Version . . . ." = $OperatingSystem.Version
}
# Output Information
New-Object -TypeName PSobject -Property $NetInfo
New-Object -TypeName PSobject -Property $CompInfo
Get-WMIObject -ComputerName $ComputerName -Class Win32_LogicalDisk |
Where-Object {$_.DriveType -eq 3} |
Select-Object @{Name="Disk Letter. . . .";Expression={($_.Name)}},
@{Name="Disk Size (GB) . .";Expression={([Math]::Round($_.size/1gb))}},
@{Name="Free Space (GB). .";Expression={([Math]::Round($_.freespace/1gb))}}
}
New-Item -Path alias:gcinf -Value Get-CompInf >$null 2>&1
无论DNS搜索顺序输出如下
DNS Servers. . . . : {203.1.64.1, 134.148.24.3, 134.148.24.1, 157.85.116.16}
如果我分开走线
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName . |
Where-Object {$_.IPConnectionMetric -ne $null} |
Select-Object -ExpandProperty DNSServerSearchOrder
我得到以下输出。
203.1.64.1
134.148.24.3
134.148.24.1
157.85.116.16
如何显示我的功能
DNS Servers. . . . : 203.1.64.1
134.148.24.3
134.148.24.1
157.85.116.16
答案 0 :(得分:0)
@Ansgar Wiechers指出,以下代码对其进行了修复。
替换$DNSstring.Split(',')
与
$DNSstring -join "`n"
提供所需的确切输出。