.net PowerShell-实例化IPAddressCollection
我正在尝试创建[System.Net.NetworkInformation.IPAddressCollection]对象以存储多个[System.Net.IPAddress]对象 理想情况下,我希望实现以下类似目标:.net PowerShell-实例化IPAddressCollection,.net,powershell,ip-address,.net,Powershell,Ip Address,我正在尝试创建[System.Net.NetworkInformation.IPAddressCollection]对象以存储多个[System.Net.IPAddress]对象 理想情况下,我希望实现以下类似目标: $IPAddresses = [System.Net.NetworkInformation.IPAddressCollection]::new() foreach ($target in [System.IO.File]::ReadLines($file)) { IPAdd
$IPAddresses = [System.Net.NetworkInformation.IPAddressCollection]::new()
foreach ($target in [System.IO.File]::ReadLines($file)) {
IPAddresses .Add([System.Net.IPAddress]::Parse($target))
}
我知道我可以更轻松地实现,但为了提高对.Net/PowerShell的理解,我想知道如何创建[System.Net.NetworkInformation.IPAddressCollection]的实例
谢谢你的类型加速器太复杂了
$Collection = @()
ForEach ($IP in @(Get-Content -Path $file))
{
$Collection += @([System.Net.IPAddress]$IP)
}
$Collection
您将得到$Collection
的[System.Net.IPAddress[]]
类型
进一步缩短为@Bill_Stewart:
$collection=Get Content-Path$file | ForEach对象{[IPAddress]$}
为什么?只需获取文件内容,并将每一行转换为[IPAddress]
对象。我同意@Bill\u Stewart的观点,“为什么?”此外,你的问题是什么?您的代码是否因某种原因无法运行?为什么?因为我希望以这种方式实现它,就这么简单,因此我的问题是:)@AresOlympusIPAddressCollection
不公开公共构造函数,因为它不是为用户实例化而设计的“因为我想”,这是一个XY问题。询问您的实际问题,而不是尝试的解决方案。我不想重新发明[System.Net.NetworkInformation.IPAddressCollection]可用。我问题的一部分是如何在PowerShell中创建此类的实例?你没有。该类没有可用的构造函数@AresOlympus我认为PowerShell与.Net类、方法等交互,因此我们基本上可以使用[System.Net.NetworkInformation.IPAddressCollection]它所做的。这并不意味着您可以在PowerShell
中编写C#
,并期望它能正常工作。如果您有问题,请使用此语法确定是否可以生成它:[Classname]| Get Member-Static
。如果它没有new
方法,则无法构造它@AresOlympusMore economic-$collection=Get Content file | ForEach对象{[IPAddress]$}