使用脚本获取计算机的ip add和hostname

时间:2012-12-01 07:04:06

标签: linux bash shell powershell

我想在启动时获取机器的ip add和hostname。我已经使用动态IP在vmware上安装了3个虚拟机。 形式如下:  xxxx.xxxx.xxxx.xxxx。主机名。

1 个答案:

答案 0 :(得分:0)

尝试获取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