Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何将我的输出从DHCP租约信息更改为ScopeId“;名称“;_Arrays_Powershell_Output_Dhcp_Scope Id - Fatal编程技术网

Arrays 如何将我的输出从DHCP租约信息更改为ScopeId“;名称“;

Arrays 如何将我的输出从DHCP租约信息更改为ScopeId“;名称“;,arrays,powershell,output,dhcp,scope-id,Arrays,Powershell,Output,Dhcp,Scope Id,这是我的输出,只是主机名所在的租约信息: IPAddress ScopeId ClientId HostName AddressState --------- ------- -------- -------- ------------ 10.10.10.10 99.99.99.99 11-11-11-11-11-11 AL10

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

IPAddress       ScopeId         ClientId           HostName       AddressState         
---------       -------         --------          --------        ------------  
10.10.10.10   99.99.99.99     11-11-11-11-11-11    AL10              Active               
$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 
使用此PowerShell脚本:

IPAddress       ScopeId         ClientId           HostName       AddressState         
---------       -------         --------          --------        ------------  
10.10.10.10   99.99.99.99     11-11-11-11-11-11    AL10              Active               
$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 
我想要的只是输出:

IPAddress       ScopeId         ClientId           HostName       AddressState         
---------       -------         --------          --------        ------------  
10.10.10.10   99.99.99.99     11-11-11-11-11-11    AL10              Active               
$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服务器租约,然后像使用
Get-DHCPServerv4scope-ComputerName$Server.dnsname时一样输出ScopeID的“name”
我使用了一个哈希表。

$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"
    }
}   

好的,那么这里的实际问题是什么呢?在通过
get-DHCPServerv4Lease