我的脚本的目的是:
Sub ChangeColor()
Dim lRow As Long, MR As Range, cell As Range, ws As Worksheet, lCol As Long
Dim wf As WorksheetFunction
Set wf = WorksheetFunction
For Each ws In Worksheets
lRow = ws.Range("A" & Rows.Count).End(xlUp).Row
lCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
Set MR = ws.Range("A2").Resize(lRow - 1, lCol)
For Each cell In MR
If cell.Value = "CENTRL DISTRICT" Then ws.Cells(cell.Row, 1).Resize(, lCol).Interior.ColorIndex = 10
If cell.Value = "KC DISTRICT" Then ws.Cells(cell.Row, 1).Resize(, lCol).Interior.ColorIndex = 3
If cell.Value = "NE DISTRICT" Then ws.Cells(cell.Row, 1).Resize(, lCol).Interior.ColorIndex = 11
If cell.Value = "SE DISTRICT" Then ws.Cells(cell.Row, 1).Resize(, lCol).Interior.ColorIndex = 30
If cell.Value = "ST LOUIS DIST" Then ws.Cells(cell.Row, 1).Resize(, lCol).Interior.ColorIndex = 12
If cell.Value = "SW DISTRICT" Then ws.Cells(cell.Row, 1).Resize(, lCol).Interior.ColorIndex = 13
Next cell
Next ws
End Sub
并过滤用户名到目前为止,我得到了我正在寻找的结果。但有时Get-WMIObject Win32_Serverconnection
包含IPv4地址,有时是主机名,有时是IPv6地址。
如果它显示为IPv6地址,那么对于脚本来说它是无用的,所以我想我最终会使用computername
来获取正确的信息。
但是当我将nslookup
的结果分配给变量时,似乎无法将变量与Get-WMIOBject
一起使用。
结果通常包括几个计算机名(主机名),当nslookup
运行时,它包含字符串nslookup
。所以它无法解决。
示例:
computername=
很抱歉,到目前为止我无法向您提供我的代码,但我现在正在工作,而且我现在在家。
答案 0 :(得分:2)
nslookup是一个CMD应用程序,虽然您可以从PowerShell对象创建变量并将它们作为字符串传递给CMD应用程序,但在此之后执行任何操作都很困难,因为输出是文本而非基于对象的。您最好使用PowerShell Resolve-DNSName
。
您有两种选择。您可以将其作为脚本执行:
$What = Get-WmiObject -class Win32_ServerConnection
$What | % {
$ComputerName = (Resolve-DNSName $_.ComputerName).NameHost
$ComputerName
}
或作为一个班轮:
Get-WmiObject -class Win32_ServerConnection | Select *,@{n='Hostname';e={(Resolve-DnsName $_.ComputerName).NameHost}}
这将添加一个名为'主机名'的附加属性。使用已解析的DNS名称。
免责声明:Resolve-DNSName仅适用于Windows Server 2012 R2 / Windows 8.1及更高版本。在旧机器上,您需要使用[Net.DNS]::GetHostEntry("IP")
之类的东西。在任何情况下,如果你想对信息做任何有用的事情而不做一堆转换,就应该避免使用nslookup。
要证明差异:
[nick@nick-lt scripts]# $resolve = Resolve-DnsName 8.8.8.8
[nick@nick-lt scripts]# $resolve
Name Type TTL Section NameHost
---- ---- --- ------- --------
8.8.8.8.in-addr.arpa PTR 1772 Answer google-public-dns-a.google.com
[nick@nick-lt scripts]# Test-NetConnection $resolve.NameHost
ComputerName : google-public-dns-a.google.com
RemoteAddress : 8.8.8.8
InterfaceAlias : Ethernet
SourceAddress : 10.87.102.133
PingSucceeded : True
PingReplyDetails (RTT) : 60 ms
[nick@nick-lt scripts]# $nslookup = nslookup 8.8.8.8
[nick@nick-lt scripts]# $nslookup
Server: redacted
Address: 10.87.1.11
Name: google-public-dns-a.google.com
Address: 8.8.8.8
[nick@nick-lt scripts]# Test-NetConnection $nslookup.name
Resolve-DnsName : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\NetTCPIP\Test-NetConnection.psm1:315 char:74
+ ... ctionResult.DNSOnlyRecords = @( Resolve-DnsName $ComputerName -DnsOnl ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Resolve-DnsName], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.DnsClient.Commands.ResolveDnsName
一旦开始使用CMD应用程序,您就可以在脚本中的步骤之间使用结果变得更加复杂。