Automation

 View Only
Expand all | Collapse all

Question on how to include virtual machine in output

  • 1.  Question on how to include virtual machine in output

    Posted Jun 13, 2011 02:45 PM

    Currently the use of this command:

    get-vm | get-networkadapter | ForEach-Object {Write-Host $_.Name","$_.macaddress }

    Gives me this output:

    Network adapter 1 , 00:50:56:bc:00:1c

    Network adapter 1 , 00:50:56:bc:00:28

    I am interested in also writing out the actual virtual machine name as well.

    virtual machine name 1 , Network adapter 1 , 00:50:56:bc:00:1c
    virtual machine name 1 , Network adapter 1 , 00:50:56:bc:00:28

    Not sure how to work the virtual machine name output as "Name" is also calling information from the network adapter.



  • 2.  RE: Question on how to include virtual machine in output

    Posted Jun 13, 2011 03:30 PM

    Getting closer:

    [vSphere PowerCLI] S:\SCRIPTS\READ ONLY SCRIPTS\MAC Addess Compare> get-vm | get-networkadapter | ForEach-Object {Write-
    Host $_.ParentId","$_.Name","$_.macaddress }
    VirtualMachine-vm-2390 , Network adapter 1 , 00:50:56:bc:00:1c
    VirtualMachine-vm-2058 , Network adapter 1 , 00:50:56:bc:00:28
    VirtualMachine-vm-2029 , Network adapter 1 , 00:50:56:bc:00:06
    VirtualMachine-vm-2310 , Network adapter 1 , 00:50:56:bc:00:72
    VirtualMachine-vm-2099 , Network adapter 1 , 00:50:56:bc:00:34



  • 3.  RE: Question on how to include virtual machine in output

    Posted Jun 13, 2011 04:12 PM

    Use $_.parent

    Get-VM | Get-NetworkAdapter | foreach {Write-Host $_.parent","$_.name","$_.macaddress}


  • 4.  RE: Question on how to include virtual machine in output

    Posted Jun 13, 2011 04:14 PM

    I saw your comment that the Parent property doesn't seem to work for you.

    Which PowerCLI version are you using ?

    Do a

    Get-PowerCLIVersion


  • 5.  RE: Question on how to include virtual machine in output

    Posted Jun 13, 2011 05:59 PM

    You're right, the current 4.1U1 doesn't support that vCenter version.

    As a bypass try this

    $report = foreach($vm in Get-VM){
        Get-NetworkAdapter -VM $vm | `
        Select @{N="VMname";E={$vm.Name}},         @{N="NIC name";E={$_.Name}},         @{N="MAC address";E={$_.MacAddress}} } $report | Export-Csv "C:\test.csv" -NoTypeInformation -UseCulture


  • 6.  RE: Question on how to include virtual machine in output

    Posted Jun 13, 2011 06:30 PM

    Thanks.

    When using this:

    $report = foreach($vm in Get-VM xyz*){
        Get-NetworkAdapter -VM $vm | `
    Select @{N="VMname";E={$vm.Name}},
            @{N="NIC name";E={$_.Name}},
            @{N="MAC address";E={$_.MacAddress}}
    }
    $report | Export-Csv ".\test.csv" -NoTypeInformation -UseCulture

    I am getting this:

    Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
    At S:\SCRIPTS\READ ONLY SCRIPTS\MAC Addess Compare\get_vm.ps1:7 char:21
    + $report | Export-Csv <<<<  ".\test.csv" -NoTypeInformation -UseCulture
        + CategoryInfo          : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCo
       mmand

    Do I need to alter this:

    xyz*



  • 7.  RE: Question on how to include virtual machine in output

    Posted Jun 13, 2011 06:37 PM

    My mistake, there was a name mask from my test environment in the code I pasted.

    It's corrected now.



  • 8.  RE: Question on how to include virtual machine in output

    Posted Jun 13, 2011 06:57 PM

    That did it.  Thanks again.



  • 9.  RE: Question on how to include virtual machine in output

    Posted Jun 13, 2011 07:54 PM

    I figured I would post while I worked to mesh these two, but I am trying to add logic to this one:

    $report = foreach($vm in Get-VM){
        Get-NetworkAdapter -VM $vm `
    | Select @{N="VMname";E={$vm.Name}},
             @{N="MAC address";E={$_.MacAddress}}
    }
    $report | Export-Csv ".\test.csv" -NoTypeInformation -UseCulture

    Logic I would like to add:

    -Filter out powered off virtual machines.  Only run on powered on virtual machines.

    -Filter out virtual machines that are not at hardware version 4.  Only run on virtual machines with hardware version 4.

    I have that separate logic here, but having trouble meshing the two:

    Get-VM | ForEach-Object {Get-View $_.ID} | Where-Object { $_.Config.Version -eq "vmx-04" } | Where-Object { $_.guest.gueststate -eq "running" } | ForEach-Object {Write-Host $_.Name","$_.guest.guestfamily }

    Thanks



  • 10.  RE: Question on how to include virtual machine in output
    Best Answer

    Posted Jun 13, 2011 08:21 PM

    Try it like this

    $report = foreach($vm in (Get-VM | where {$_.PowerState -eq "PoweredOn" -and $_.Version -eq "v7"})){
        Get-NetworkAdapter -VM $vm  | `
       
    Select @{N="VMname";E={$vm.Name}},          @{N="MAC address";E={$_.MacAddress}} }

    $report | Export-Csv ".\test.csv" -NoTypeInformation -UseCulture


  • 11.  RE: Question on how to include virtual machine in output

    Posted Jun 14, 2011 02:07 PM

    So close.  Looks like my version of powersCLI is causing a problem again.  Having issues with this part:

    $_.Version -eq "v7

    Version doesn't look to be a property of get-VM in my version:

    [vSphere PowerCLI] S:\SCRIPTS\READ ONLY SCRIPTS\MAC Addess Compare> Get-VM | Get-Member


       TypeName: VMware.VimAutomation.Client20.VirtualMachineImpl

    Name                MemberType Definition
    ----                ---------- ----------
    Equals              Method     bool Equals(System.Object obj)
    GetHashCode         Method     int GetHashCode()
    GetType             Method     type GetType()
    ToString            Method     string ToString()
    CDDrives            Property   VMware.VimAutomation.Types.CDDrive[] CDDrives {get;}
    CustomFields        Property   System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=2.0.0.0, C...
    DatastoreIdList     Property   System.String[] DatastoreIdList {get;}
    Description         Property   System.String Description {get;}
    DrsAutomationLevel  Property   System.Nullable`1[[VMware.VimAutomation.Types.DrsAutomationLevel, VMware.VimAutomatio...
    FloppyDrives        Property   VMware.VimAutomation.Types.FloppyDrive[] FloppyDrives {get;}
    FolderId            Property   System.String FolderId {get;}
    Guest               Property   VMware.VimAutomation.Types.VMGuest Guest {get;}
    HAIsolationResponse Property   System.Nullable`1[[VMware.VimAutomation.Types.HAIsolationResponse, VMware.VimAutomati...
    HardDisks           Property   VMware.VimAutomation.Types.HardDisk[] HardDisks {get;}
    HARestartPriority   Property   System.Nullable`1[[VMware.VimAutomation.Types.HARestartPriority, VMware.VimAutomation...
    Host                Property   VMware.VimAutomation.Types.VMHost Host {get;}
    HostId              Property   System.String HostId {get;}
    Id                  Property   System.String Id {get;}
    MemoryMB            Property   System.Int32 MemoryMB {get;}
    Name                Property   System.String Name {get;}
    NetworkAdapters     Property   VMware.VimAutomation.Types.NetworkAdapter[] NetworkAdapters {get;}
    Notes               Property   System.String Notes {get;}
    NumCpu              Property   System.Int32 NumCpu {get;}
    PowerState          Property   VMware.VimAutomation.Types.PowerState PowerState {get;}
    ResourcePoolId      Property   System.String ResourcePoolId {get;}
    UsbDevices          Property   VMware.VimAutomation.Types.UsbDevice[] UsbDevices {get;}
    VMHostId            Property   System.String VMHostId {get;}
    VMSwapfilePolicy    Property   System.Nullable`1[[VMware.VimAutomation.Types.VMSwapfilePolicy, VMware.VimAutomation....



  • 12.  RE: Question on how to include virtual machine in output

    Posted Jun 14, 2011 03:58 PM

    Ok, try it like this, but I'm afraid I couldn't test this, since I don't have an older PowerCLI build installed right now.

    $report = foreach($vm in (Get-VM | `
       
    where {$_.PowerState -eq "PoweredOn" -and `
                (Get-View $_.Id).Config.Version -eq "vmx-07"})){     Get-NetworkAdapter -VM $vm  | `
        Select @{N="VMname";E={$vm.Name}},          @{N="MAC address";E={$_.MacAddress}} } $report | Export-Csv ".\test.csv" -NoTypeInformation -UseCulture


  • 13.  RE: Question on how to include virtual machine in output

    Posted Jun 14, 2011 04:01 PM

    Worked like a charm.  Thanks again.