簡體   English   中英

使用腳本獲取IP地址和機器的主機名

[英]Get ip add and hostname of machines using script

我想在啟動機器時獲取機器的IP地址和主機名。 我已經在具有動態IP的vmware上安裝了3個虛擬機。 其格式為:xxxx.xxxx.xxxx.xxxx。 主機名。

嘗試獲取Ipdata函數:

Function Get-IPData {
#this function assumes admin credentials
[cmdletBinding()]
Param(
    [Parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
    [ValidateNotNullOrEmpty()]
    [Alias("name")]
    [string[]]$computername=$env:computername
)

Process {
    ForEach ($computer in $computername) {
        Write-Verbose "Querying $($computer.ToUpper())"
        Try 
        {
        #get NICS that are IP and DHCP enabled
         Get-WMIObject -Class win32_networkadapterconfiguration -computername $Computer `
        -Filter "IPEnabled='TRUE' AND DHCPEnabled='TRUE'" -ErrorAction "Stop" | 
        Select Description,DNSHostname,
        @{Name="IPAddress";Expression={$_.IPAddress[0]}},
        @{Name="SubnetMask";Expression={$_.IPSubnet[0]}},
        @{Name="DefaultGateway";Expression={$_.DefaultIPGateway[0]}},DNSDomain,
        @{Name="PrimaryDNS";Expression={$_.DNSServerSearchOrder[0]}},DHCPServer,
        @{Name="DHCPLease";Expression={$_.ConvertToDateTime($_.DHCPLeaseObtained)}},
        @{Name="DHCPExpires";Expression={$_.ConvertToDateTime($_.DHCPLeaseExpires)}},
        @{Name="DHCPTimeToLive";Expression={ $_.ConvertToDateTime($_.DHCPLeaseExpires) - (Get-Date)}},
        MACAddress,
        @{Name="Speed";Expression={
        #use an Associators Of query to get the NIC
          $nic=Get-WmiObject -query "associators of {Win32_NetworkAdapterConfiguration.Index=$($_.index)}" -computername $computer
          $nic.Speed
          }}
    } #close Try
    Catch
        {
            Write-Warning "Failed to retrieve IP configuration from $($computer.ToUpper())"
            Write-Warning $_.Exception.Message

        } #close Catch
    } #close ForEach
 } #close Process
} #end function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM