【问题标题】:How can I change my output from DHCP Lease info to ScopeId "Name"如何将我的输出从 DHCP 租赁信息更改为 ScopeId“名称”
【发布时间】:2021-04-09 19:40:26
【问题描述】:

这是我的输出的样子,它只是主机名所在的租约信息:

IPAddress       ScopeId         ClientId           HostName       AddressState         
---------       -------         --------          --------        ------------  
10.10.10.10   99.99.99.99     11-11-11-11-11-11    AL10              Active               

使用这个 PowerShell 脚本:

$hostname = "AL10"
$locationArray = @()
  foreach ($Server in $DHServers){
        
        $scope = Get-DHCPServerv4scope -ComputerName $Server.dnsname  | Get-DHCPServerv4Lease -ComputerName $Server.dnsname | Where-Object HostName -like "$hostName*"
        
        $locationArray += $scope
}
 
$locationArray

我想要的,只是输出吗:

ScopeID Name               
---------        
Name 

目标是:提供主机名.txt,找到对应的DHCP Server租约,然后像使用Get-DHCPServerv4scope -ComputerName $Server.dnsname | Select-Object "name"时一样输出ScopeID的“名称”

【问题讨论】:

  • 好的,这里的实际问题是什么?
  • 在通过Get-DHCPServerv4Lease 传递我的主机名后,如何让我的输出更改为 ScopeID 对象“名称”

标签: arrays powershell output dhcp scope-id


【解决方案1】:

我使用了哈希表。

$hashtable = @{} #create hash table

$i=0
foreach ($Server in $DHServers){
    $scopes = Get-DHCPServerv4Scope -ComputerName $Server.dnsname #get all scopes
    foreach ($hostname in (Import-Csv C:\script\Asset_List.csv | Select-Object -ExpandProperty asset)){ #get hostnames from list
        foreach ($scope in $scopes) {
            
            if($scope | Get-DhcpServerV4Lease -ComputerName $server.dnsname | Where-Object HostName -like "$hostName*" ) { #compares the hostname to find which lease it is in
                try{
                $hashtable.add($hostname, $scope.name)# add keys, values to table
                }
                
                catch {
                    $ErrorMessage = $_.Exception.Message
                    if ($ErrorMessage  -like '*Key in dictionary Already*') {
                         write-host 'Network issues could be causing process to take longer than normal' -ForegroundColor Green
                    }
                }
           
            } 
        }
    }
}

然后我在我的PSCustomObject中调用它

[PSCustomObject]@{ #Rename varibles in data pull for output file
    Asset = $hostname
    Model = $System.Model
    SerialNumber = $BIOS.SerialNumber
    Location = $hashtable[$hostname]
    LastUser = $User
    MacAddress = $mac
    IpAddress = $IpV
    Status = "Online"
    }
}   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 2015-07-06
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 2021-05-14
    相关资源
    最近更新 更多