我已经在Microsoft文档中上下搜索了一个cmdlet,该cmdlet返回一个包含接口默认网关的对象。我知道Get-NetIPConfiguration返回默认网关,但是它不返回供我使用的对象。有任何想法吗?
在这里编辑我正在运行的内容。我正在尝试自动更改nic的ip地址,为此,我需要nic的默认网关。
import java.util.Comparator;
public class MedalCount {
String name;
int goldMedals;
int silverMedals;
int bronzeMedals;
int totalMedals;
/**
* Creates a new MedalCount instance.
* @param name is the name of the nation
* @param nGoldMedals is the number of gold medals
* @param nSilverMedals is the number of silver medals
* @param nBronzeMedals is the number of bronze medals
*/
public MedalCount(String name, int nGoldMedals, int nSilverMedals,
int nBronzeMedals) {
this.name = name;
this.goldMedals = nGoldMedals;
this.silverMedals = nSilverMedals;
this.bronzeMedals = nBronzeMedals;
totalMedals = nGoldMedals + nSilverMedals + nBronzeMedals;
}
/** Return a string describing the medal count information for the country */
public String toString() {
return name + ":\t" + goldMedals + ",\t" + silverMedals + ",\t"
+ bronzeMedals + ",\t"+ totalMedals;
}
/**
* Compare instance of medal count with another.
* Nations compared based on number of total medals (descending order).
*/
// TODO: create comparator
public static final Comparator<MedalCount> COMPARATOR = new Comparator<MedalCount>() {
@Override
public int compare(MedalCount totalMedals1, MedalCount TotalMedals2) {
if (totalMedals1.totalMedals >= totalMedals1.totalMedals) {
return 1;
} else
return 0;
}
}
/** Comparator for sorting by nation name alphabetically */
// TODO: create comparator
/** Comparator for sorting by gold medal count (descending order) */
// TODO: create comparator
答案 0 :(得分:0)
在Powershell中,事情并不总是看起来一样。为了便于阅读,可以过滤屏幕上对象的显示。就像这里:
PS> Get-NetIPConfiguration -InterfaceIndex 8
InterfaceAlias : Wi-Fi
InterfaceIndex : 8
InterfaceDescription : Intel(R) Dual Band Wireless-AC 8265
NetProfile.Name : xxxxxx
IPv4Address : 10.0.0.26
IPv6DefaultGateway :
IPv4DefaultGateway : 10.0.0.251
DNSServer : 10.0.0.251
212.54.35.25
212.54.40.25
似乎IPv4DefaultGateway只是一个包含IP地址的字符串。但这不是:
PS> (Get-NetIPConfiguration -InterfaceIndex 8).ipv4defaultgateway
ifIndex DestinationPrefix NextHop RouteMetric ifMetric PolicyStore PSComputerName
------- ----------------- ------- ----------- -------- ----------- --------------
8 0.0.0.0/0 10.0.0.251 0 45 ActiveStore
现在您可以看到IPv4DefaultGateway具有成员,而NextHop是包含网关的实际IP地址的成员。有时,您需要剥离对象以找出其中的内容。此处未使用,但是Get-Member
在这些问题上可能会非常有帮助
所以您需要的代码是这样:
$ip = Get-NetIPConfiguration -InterfaceIndex 17
New-NetIPAddress -IPAddress (New Ip address) -PrefixLength (Current Prefix length) -DefaultGateway $ip.IPv4DefaultGateway.NextHop