VMware Cloud Community
dmshirkeyNMB
Contributor
Contributor
Jump to solution

Scripting Issue

I am trying to issue a command to get network interface IP address from a list of computer names, All works well for getting the address, but how would I get the output to show the input of the computer name?

$vm= Import-Csv c:\vm.txt

$vm | ForEach-Object { Get-VMGuestNetworkInterface -VM $_.hostname -guestpassword ****** -guestuser domain.us.local\******** -hostuser root -hostpassword ********* }

I tried to do a | select $_.hostname,Ip but this didnt work?

What I am looking for is ex.

Hostname Ipaddress

Server1 192.168.1.1

I am just diving into powershell, powergui and powercli, any help would be great.

Tags (2)
Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can do:

$GuestCredential = Get-Credential
$HostCredential = Get-Credential
$vm= Get-Content c:\vm.txt
$vm | ForEach-Object {
  $vm = $_ 
  $report = "" | Select-Object -property HostName,IpAddress
  $report.HostName = $vm
  $report.IpAddress = (Get-VMGuestNetworkInterface -VM (Get-VM $vm) -GuestCredential $GuestCredential -HostCredential $HostCredential).IP
  $report
}

Robert

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
3 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can do:

$GuestCredential = Get-Credential
$HostCredential = Get-Credential
$vm= Get-Content c:\vm.txt
$vm | ForEach-Object {
  $vm = $_ 
  $report = "" | Select-Object -property HostName,IpAddress
  $report.HostName = $vm
  $report.IpAddress = (Get-VMGuestNetworkInterface -VM (Get-VM $vm) -GuestCredential $GuestCredential -HostCredential $HostCredential).IP
  $report
}

Robert

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
dmshirkeyNMB
Contributor
Contributor
Jump to solution

Thanks, That helped a lot.

Now if I could just understand it fully Smiley Happy

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I will try to explain my script so you can understand it fully :-).

The Get-Credential cmdlet gives you a window in which you can type a username and password. In the script you get this window twice. The first time for the guest credential and the second time for the host credential. In this way you don't have to store the credentials in the script, which I think is a bad practice concerning security.

I used the Get-Content cmdlet instead of the Import-Csv cmdlet because I think this is easier. Now you can just put all the VM names in the file. If you use Import-Csv you have to start with a header line with the column names. If you use VmName as the column name and you use Import-Csv you have to change $vm = $_ into $vm = $_ .VmName.

The ForEach-Object loops through all the VM names. $_ is the object you get via the pipe. To be able to use it later I save it in the variable $vm. The line $report = "" | Select-Object -property HostName,IpAddress creates a new object $report with the properties HostName and IpAddress. You can do this in several other ways in PowerShell, e.g. with the New-Object cmdlet, but this way is short and you see it often. You have to realise that the Select-Object cmdlet allways creates a new object. That feature is used here.

In the next two lines the object is filled with the hostname and the IP address.

$report is a shortcut for Write-Output $report. This sends the output to the screen.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition