Windows 网络适配器发现
Microsoft.Windows.Server.10.0.NetworkAdapterDiscovery.ModuleType (DataSourceModuleType)
此规则发现网络适配器。
Element properties: Member Modules:
Overrideable Parameters:
Source Code: <DataSourceModuleType ID="Microsoft.Windows.Server.10.0.NetworkAdapterDiscovery.ModuleType" Accessibility="Internal">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ComputerName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ComputerID" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="IntervalSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TimeoutSeconds" type="xsd:int"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="DiscoverDisabledNetworkAdapters" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="UseMacAddress" type="xsd:boolean"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="TimeoutSeconds" ParameterType="int" Selector="$Config/TimeoutSeconds$"/>
<OverrideableParameter ID="DiscoverDisabledNetworkAdapters" ParameterType="bool" Selector="$Config/DiscoverDisabledNetworkAdapters$"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<DataSource ID="Scheduler" TypeID="System!System.Discovery.Scheduler">
<Scheduler>
<SimpleReccuringSchedule>
<Interval Unit="Seconds">$Config/IntervalSeconds$</Interval>
</SimpleReccuringSchedule>
<ExcludeDates/>
</Scheduler>
</DataSource>
<ProbeAction ID="ScriptProbe" TypeID="Microsoft.Windows.Server.10.0.PowerShellDiscoveryProbe">
<ScriptName>Microsoft.Windows.Server.10.0.NetworkAdapterDiscovery.ModuleType.ps1</ScriptName>
<PSparam>param ($SourceID, $ManagedEntityId, $TargetComputer, $TargetComputerID, $IsDiscoverDisabled, $IsUseMacAddress)</PSparam>
<ScriptBody><Script> #Copyright (c) Microsoft Corporation. All rights reserved.
# Parameters that should be passed to this script
# 0 MPElement ID ($MPElement$)
# 1 Target Id for ME this rule is running against ($Target/Id$)
# 2 Computer (FQDN) that the Network Adapter will be hosted on
# 3 Computer ID (Key) that the Network Adapter will be hosted on
# 4 Should we discover disabled Network Adapters or not
# 5 Use MacAddress for discovery (true), otherwise NetConnectionID
Function Main()
{
$oDiscoveryData = $momAPI.CreateDiscoveryData(0, $SourceID, $ManagedEntityId)
if ((DoDiscovery $TargetComputer $TargetComputerID $oDiscoveryData $IsDiscoverDisabled $IsUseMacAddress) -ne 0)
{
$oDiscoveryData = $momAPI.CreateDiscoveryData(0, $SourceID, $ManagedEntityId)
$oDiscoveryData.IsSnapshot = $false
}
$oDiscoveryData
}
Function DoDiscovery
{
param ([string]$sTargetComputer, [string]$sTargetComputerID, $oDisc, $IsDiscoverDisabled, $IsUseMacAddress)
$ErrorActionPreference = "SilentlyContinue"
$bIsUseMacAddress = Convert-ToBoolean -sBool $IsUseMacAddress
$bIsDiscoverDisabled = Convert-ToBoolean -sBool $IsDiscoverDisabled
$bLoad = Load-Module -ModuleName "NetAdapter"
if ($false -eq $bLoad)
{
return 1
}
$bLoad = Load-Module -ModuleName "DnsClient"
if ($false -eq $bLoad)
{
return 1
}
$error.Clear()
$netAdapters = Get-NetAdapter -Physical | Where `
{
(
(($true -eq $bIsUseMacAddress -and $false -eq [string]::IsNullOrEmpty($_.MacAddress)) -or $false-eq $bIsUseMacAddress) `
-and (($true -eq $bIsDiscoverDisabled -and $_.MediaConnectState -ne 2 ) -or $false -eq $bIsDiscoverDisabled ) `
-and $false -eq [string]::IsNullOrEmpty($_.Name)
)
}
if ($error.Count -ne 0)
{
$iResult = 1
return $iResult
}
$errorCount = 0
foreach ($netAdapter in $netAdapters)
{
$sDeviceId = Get-StringProperty -Property $netAdapter.DeviceID
$InterfaceIndex = $netAdapter.InterfaceIndex
if ([string]::IsNullOrEmpty($sDeviceId))
{
continue
}
$error.Clear()
$oInstance = $oDisc.CreateClassInstance("$MPElement[Name='Microsoft.Windows.Server.10.0.NetworkAdapter']$")
if ($error.Count -ne 0)
{
$errorCount += $error.Count
break
}
Add-MainDiscovery -oInstance $oInstance -netAdapter $netAdapter -sDeviceId $sDeviceId -sTargetComputerID $sTargetComputerID -InterfaceIndex $InterfaceIndex
$errorCount += $error.Count
Add-NetConfig -oInstance $oInstance -ifIndex $InterfaceIndex
$errorCount += $error.Count
if ($null -ne $oInstance)
{
$oDisc.AddInstance($oInstance)
}
}
if ($errorCount -eq 0)
{
$iResult = 0
}
else
{
$iResult = 1
}
return $iResult;
}
Function Add-MainDiscovery($oInstance,$netAdapter,$sDeviceId,$sTargetComputerID,$InterfaceIndex)
{
$ErrorActionPreference = "SilentlyContinue"
if ($null -eq $oInstance -or $null -eq $netAdapter)
{
return
}
$InterfaceGuid = $netAdapter.InstanceID
$InterfaceDesc = GetPerfmonInstance $netAdapter.InterfaceDescription
$serviceName = Get-ServiceName -DriverName $netAdapter.DriverName
$SlotNumber = Get-SlotNumber -InterfaceGuid $InterfaceGuid
$DhcpServers = Get-IpV4DhcpServers -InterfaceId $InterfaceGuid
$AdapterType = Get-NicAdapterType -MediaType $netAdapter.MediaType
$oInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $sTargetComputerID)
$oInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.LogicalDevice']/DeviceID$", $sDeviceId)
$oInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.LogicalDevice']/Name$", (Get-StringProperty -Property $netAdapter.Name))
$oInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.LogicalDevice']/Description$", (Get-StringProperty -Property $netAdapter.Description))
$oInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.NetworkAdapter']/ProductName$", (Get-StringProperty -Property $netAdapter.DriverDescription))
$oInstance.AddProperty("$MPElement[Name='WindowsServer!Microsoft.Windows.Server.NetworkAdapter']/AdapterType$", (Get-StringProperty -Property $AdapterType))
$oInstance.AddProperty("$MPElement[Name='WindowsServer!Microsoft.Windows.Server.NetworkAdapter']/Index$", (Get-StringProperty -Property $InterfaceIndex))
$oInstance.AddProperty("$MPElement[Name='WindowsServer!Microsoft.Windows.Server.NetworkAdapter']/Manufacturer$", (Get-StringProperty -Property $netAdapter.DriverProvider))
$oInstance.AddProperty("$MPElement[Name='WindowsServer!Microsoft.Windows.Server.NetworkAdapter']/MACAddress$", (Get-StringProperty -Property $netAdapter.MACAddress))
$oInstance.AddProperty("$MPElement[Name='WindowsServer!Microsoft.Windows.Server.NetworkAdapter']/ServiceName$",(Get-StringProperty -Property $serviceName))
$oInstance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", (Get-StringProperty -Property $netAdapter.Name))
$oInstance.AddProperty("$MPElement[Name='WindowsServer!Microsoft.Windows.Server.NetworkAdapter']/PerfmonInstance$", (Get-StringProperty -Property $InterfaceDesc) )
$oInstance.AddProperty("$MPElement[Name='WindowsServer!Microsoft.Windows.Server.NetworkAdapter']/DHCPServer$", (Get-StringProperty -Property $DhcpServers) )
$oInstance.AddProperty("$MPElement[Name='Microsoft.Windows.Server.10.0.NetworkAdapter']/SlotNumber$", $SlotNumber )
}
Function Get-NicAdapterType($MediaType)
{
$ErrorActionPreference = "SilentlyContinue"
$error.Clear()
$result = "Unknown"
if([string]::IsNullOrEmpty($MediaType) )
{
return $result
}
$NdisMediaType = Convert-ToUint32 -Value $MediaType
$AdapterTypes = @{
[uint32]0 = "Ethernet 802.3";[uint32]1 = "Ethernet 802.5";[uint32]2 = "FDDI";[uint32]3 = "WAN";[uint32]4 = "LocalTalk";[uint32]5 = "DIX";[uint32]6 = "Raw Arcnet";
[uint32]7 = "Ethernet 878.2";[uint32]8 = "ATM";[uint32]9 = "Wireless WAN";[uint32]10 = "IRDA";[uint32]11 = "BPC";[uint32]12 = "Connection Oriented WAN";[uint32]13 = "IP 1394";
[uint32]14 = "IB";[uint32]15 = "Tunnel";[uint32]16 = "Native 802.11";[uint32]17 = "Loopback";[uint32]18 = "WiMAX";[uint32]19 = "IP"
}
$result = $AdapterTypes[$NdisMediaType]
if ($null -eq $result)
{
$result = "Unknown"
}
return $result
}
Function Convert-ToUint32($Value)
{
$ErrorActionPreference = "SilentlyContinue"
$result = [uint32]0
if ([string]::IsNullOrEmpty($value))
{
return $result
}
$result = [uint32]::MaxValue
$type = $Value.GetType()
$error.Clear()
$result = [uint32]$Value
if (0 -eq $error.Count)
{
return $result
}
else
{
$error.Clear()
$Value = [int32]$Value
if (0 -ne $error.Count)
{
$error.Clear()
return $result
}
else
{
if ($Value -lt 0)
{
$result = [uint32](-bnot $Value) + [uint32]([int32]::MaxValue) + 1
}
else
{
$result = [uint32]$Value
}
}
}
return $result
}
Function Get-ServiceName($DriverName)
{
$ErrorActionPreference = "SilentlyContinue"
$error.Clear()
$DriverNameProperty = Get-ServiceNameHelper -DriverName $DriverName
if ($error.Count -ne 0)
{
$DriverNameProperty = [string]::Empty
}
return $DriverNameProperty
}
Function Get-ServiceNameHelper([string]$DriverName)
{
$ErrorActionPreference = "SilentlyContinue"
if ([string]::IsNullOrEmpty($DriverName))
{
return [string]::Empty
}
$PathIndex = $DriverName.LastIndexOf("\")
if ($PathIndex -eq ($DriverName.Length - 1))
{
return [string]::Empty
}
$DriverNamePostfix = $DriverName.Substring($PathIndex + 1)
$PathIndex = $DriverNamePostfix.IndexOf(".")
$ServiceName = $DriverNamePostfix
if (-1 -ne $PathIndex )
{
$ServiceName = $DriverNamePostfix.Substring(0,$PathIndex)
}
return $ServiceName
}
Function Get-NetIpConfig($ifIndex)
{
$ErrorActionPreference = "SilentlyContinue"
$error.Clear()
$netcfg = Get-NetIPConfiguration -ifIndex $ifIndex
$error.Clear()
return $netcfg
}
Function Get-IPAddressString ($NetIpConfig,[int]$MaxSize=256)
{
$ips = $NetIpConfig.AllIPAddresses | Where {($_.PrefixOrigin -ieq "Dhcp" -or $_.PrefixOrigin -ieq "Manual") -or ($_.AddressFamily -eq "IPV6")}
return Get-StringFromIpArray -ips $ips
}
Function Get-DefaultGW ($NetIpConfig,[int]$MaxSize=256)
{
if ($null -eq $NetIpConfig)
{
return [string]::Empty
}
$ips = $NetIpConfig.IPv4DefaultGateway
return Get-StringFromIpArray -ips $ips -Property "NextHop" -MaxSize $MaxSize
}
Function Get-DhcpStatus ($NetIpConfig)
{
$dhcp = $NetIpConfig.AllIPAddresses | Where {$_.PrefixOrigin -ieq "Dhcp" }
if ($null -eq $dhcp -or 0 -eq $dhcp.Count)
{
$DhcpEnabled = "false"
}
else
{
$DhcpEnabled = "true"
}
return $DhcpEnabled
}
Function Get-IPSubNetString ($NetIpConfig,[int]$MaxSize=256)
{
if ($null -eq $NetIpConfig)
{
return [string]::Empty
}
$ips = $NetIpConfig.AllIPAddresses | Where {$_.PrefixOrigin -ieq "Dhcp" -or $_.PrefixOrigin -ieq "Manual"}
return Get-SubNetFromIpArray -ips $ips
}
Function Get-IpSubnetFromPrefix([int]$Prefix)
{
if ($Prefix -gt 32)
{
return "$Prefix"
}
$Subnet = @(0..3)
$Subnet[0] = 0
$Subnet[1] = 0
$Subnet[2] = 0
$Subnet[3] = 0
if ($Prefix -le 8)
{
$Subnet[0] = (1 -shl 8) - (1 -shl (8 - $Prefix))
}
elseif ($Prefix -le 16)
{
$tprefix = $Prefix - 8
$Subnet[0] = 255
$Subnet[1] = (1 -shl 8) - (1 -shl (8 - $tprefix))
}
elseif ($Prefix -le 24)
{
$tprefix = $Prefix - 16
$Subnet[0] = 255
$Subnet[1] = 255
$Subnet[2] = (1 -shl 8) - (1 -shl (8 - $tprefix))
}
elseif ($Prefix -le 32)
{
$tprefix = $Prefix - 24
$Subnet[0] = 255
$Subnet[1] = 255
$Subnet[2] = 255
$Subnet[3] = (1 -shl 8) - (1 -shl (8 - $tprefix))
}
return [string]::Join(".",$Subnet)
}
Function Get-StringFromIpArray($ips,[string]$Property="IPAddress",[int]$MaxSize=256)
{
$ErrorActionPreference = "SilentlyContinue"
$IpCount = Get-IpCount -ips $ips
if ( 0 -eq $IpCount -or $null -eq $IpCount)
{
return [string]::Empty
}
$Length = 0;
$IpRealCount = 0
$IpArray = @(1..$IpCount)
foreach ($ip in $ips)
{
$ipAddr = $ip.$Property
$IpLength = $ipAddr.Length
if ($null -eq $IpLength -or 0 -eq $IpLength)
{
continue
}
$delta = $MaxSize - $Length
if (0 -ne $IpRealCount)
{
$delta--
}
if ( $IpLength -gt $delta)
{
continue
}
$IpArray[$IpRealCount] = $ipAddr
$Length += $IpLength
if (0 -ne $IpRealCount)
{
$Length++
}
$IpRealCount++
}
$sResult = [string]::Empty
if (0 -ne $IpRealCount)
{
for ($i = $IpRealCount;$i -lt $IpCount; $i++)
{
$IpArray[$i] = [string]::Empty
}
$sResult = [string]::join(",",$IpArray).Trim(",")
}
return $sResult
}
Function Get-SubNetFromIpArray($ips,[string]$Property="PrefixLength",[int]$MaxSize=256)
{
$ErrorActionPreference = "SilentlyContinue"
$IpCount = $ips.Count
$IpCount = Get-IpCount -ips $ips
if ( 0 -eq $IpCount -or $null -eq $IpCount)
{
return [string]::Empty
}
$Length = 0;
$IpRealCount = 0
$IpArray = @(1..$IpCount)
$ipSubNetPrefix = [int]0
foreach ($ip in $ips)
{
$subnetP = $ip.$Property
if ([string]::IsNullOrEmpty($subnetP))
{
$ipSubNetPrefix = 0
}
else
{
[void][int]::TryParse($subnetP,[ref]$ipSubNetPrefix)
}
$ipSubnet = Get-IpSubnetFromPrefix -Prefix $ipSubNetPrefix
$IpLength = $ipSubnet.Length
if ($null -eq $IpLength -or 0 -eq $IpLength)
{
continue
}
$delta = $MaxSize - $Length
if (0 -ne $IpRealCount)
{
$delta--
}
if ( $IpLength -gt $delta)
{
continue
}
$IpArray[$IpRealCount] = $ipSubnet
$Length += $IpLength
if (0 -ne $IpRealCount)
{
$Length++
}
$IpRealCount++
}
$sResult = [string]::Empty
if (0 -ne $IpRealCount)
{
for ($i = $IpRealCount;$i -lt $IpCount; $i++)
{
$IpArray[$i] = [string]::Empty
}
$sResult = [string]::join(",",$IpArray).Trim(",")
}
return $sResult
}
Function Get-SlotNumber([string]$InterfaceGuid)
{
$ErrorActionPreference = "SilentlyContinue"
$nic = Get-NetAdapterHardwareInfo | Where {$_.InstanceId -ieq $InterfaceGuid}
$SlotNumber = $nic.SlotNumber
$error.Clear()
return "$SlotNumber"
}
Function Add-NetConfig($oInstance,$ifIndex)
{
$ErrorActionPreference = "SilentlyContinue"
if ($null -eq $oInstance)
{
return
}
$netcfg = Get-NetIpConfig -ifIndex $ifIndex
if ($null -eq $netcfg)
{
return
}
$dhcp = Get-DhcpStatus -NetIpConfig $netcfg
$IpAddr = Get-IPAddressString -NetIpConfig $netcfg
$IpSubnets = Get-IPSubNetString -NetIpConfig $netcfg
$IpEnabled = (-Not [string]::IsNullOrEmpty($IpAddr)).ToString()
$DefaultGw = Get-DefaultGW -NetIpConfig $netcfg
$dnsData = Get-DnsClientGlobalSetting
if ($null -eq $dnsData)
{
$domain = [string]::Empty
}
else
{
$domain = [string]::Join(",",$dnsData.DNSSuffixesToAppend)
$domain = Get-StringProperty -Property $domain
}
$oInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.NetworkAdapter']/IPEnabled$", $IpEnabled)
$oInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.NetworkAdapter']/DefaultIPGateway$", $DefaultGw)
$oInstance.AddProperty("$MPElement[Name='WindowsServer!Microsoft.Windows.Server.NetworkAdapter']/IPAddress$", $IPAddr )
$oInstance.AddProperty("$MPElement[Name='WindowsServer!Microsoft.Windows.Server.NetworkAdapter']/IPSubnet$", $IPSubnets )
$oInstance.AddProperty("$MPElement[Name='WindowsServer!Microsoft.Windows.Server.NetworkAdapter']/DHCPEnabled$", $dhcp )
$oInstance.AddProperty("$MPElement[Name='WindowsServer!Microsoft.Windows.Server.NetworkAdapter']/DNSDomain$", $domain )
}
Function Get-IpV4DhcpServers([string]$InterfaceId)
{
$ErrorActionPreference="SilentlyContinue"
$DhcpServers = (Get-Item "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\$InterfaceId").GetValue("DhcpServer")
if ([string]::IsNullOrEmpty($DhcpServers))
{
$DhcpServers = [string]::Empty
}
$error.Clear()
return $DhcpServers
}
Function Get-IpCount($ips)
{
$ErrorActionPreference = "SilentlyContinue"
$IpCount = $ips.Count
if ( $null -eq $IpCount)
{
if ($null -ne $ips)
{
$IpCount = 1
}
}
return $IpCount
}
Main
</Script> </ScriptBody>
<Parameters>
<Parameter>
<Name>SourceID</Name>
<Value>$MPElement$</Value>
</Parameter>
<Parameter>
<Name>ManagedEntityId</Name>
<Value>$Target/Id$</Value>
</Parameter>
<Parameter>
<Name>TargetComputer</Name>
<Value>$Config/ComputerName$</Value>
</Parameter>
<Parameter>
<Name>TargetComputerID</Name>
<Value>$Config/ComputerID$</Value>
</Parameter>
<Parameter>
<Name>IsDiscoverDisabled</Name>
<Value>$Config/DiscoverDisabledNetworkAdapters$</Value>
</Parameter>
<Parameter>
<Name>IsUseMacAddress</Name>
<Value>$Config/UseMacAddress$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="ScriptProbe">
<Node ID="Scheduler"/>
</Node>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.Discovery.Data</OutputType>
</DataSourceModuleType>