Get-Annotation and export to CSV

I am trying to export of Annotation custom fields in a CSV with foreach and difficulty to operate there where I want.

1. is it possiible export with foreach or do I have to use foreach-object?

$vms = get-vm pt*
foreach ($vm in $vms){
    $vm = Get-View $vm.ID
    $nm = $vm.name
    $hn = $vm.guest.hostname
    $ip = $vm.guest.ipaddress
    $contact = get-vm $vms | Get-Annotation -CustomAttribute "System Contact"|select Value
    $vm | select `
    @{Name = "Name"; E={$nm}},`
    @{Name = "Contact"; E={$contact}},`
    @{Name = "Hostname"; E={$hn}},`
    @{Name = "IP"; E={$ip}}
}format-table -AutoSize -Wrap

2. How can I get rid of the "{@{value ="in the exit?}} "

Name                          Contact                       Hostname                      IP
----                          -------                       --------                      --
PT1                      {@{Value=abcd.Serverteam...     pt1.abcd.com              10.0.1.5
PT2                      {@{Value=abcd.Serverteam...     pt2.abcd.com              10.0.1.33

The better, more PowerShell, way to do this would be to use the pipeline.

Something like that

Get-VM tp* | Select Name,  @{N="Hostname";E={$_.Guest.Hostname}},  @{N="IP address";E={$_.Guest.IpAddress}},  @{N="Contact";E={
    Get-Annotation -Entity $_ -CustomAttribute "System Contact" |
    Select -ExpandProperty Value}} | Export-Csv c:\report.csv -NoTypeInformation -UseCulture

Tags: VMware

Similar Questions

  • script to get the csv vm disk usage and export to csv format

    Hi all

    I am trying to create a script to get the use of disk for vm - space - free space and total disk space.

    I used this script that allows me to get the required result.

    but I need to edit the script for each cluster, VM he should get computers virtual cluster and check the vm disuage. and export of csv file

    $vms = import-CSV "C:\Cluster.csv".

    $outputfile = "c:\Diskusage.csv".

    SE connect-viserver VC-vcadmin username-password

    {foreach ($vm to $vms)

    Get - VM $vm.name | Where {$_.} PowerState - eq "Receptor"} | Get-VMGuest | Select the ExpandProperty - VMName disks | Select VMName, path, @{Name = "DiskCapacityGB"; Expression = {[math]: Round ((($_.))} {{((Ability) / 1 GB), 2)}}, @{Name = "DiskUsedMB"; Expression = {[math]: Round ((($_.))} Capacity - $_. {{((FreeSpace) / 1 MB), 2)}}, @{Name = "DiskFreeMB"; Expression = {[math]: Round ((($_.))} {{((FreeSpace) / 1 MB), 2)}} | Export-Csv - NoTypeInformation $outputfile}

    Viserver disconnect

    Joint the output file

    Veuileez help me in creating the script

    You can try like this?

    Connect-viserver VC -user vcadmin -password
    foreach($cluster in Get-CLuster){
        $outputfile = "c:\" + $cluster.Name + "-Diskusage.csv"    Get-VM -Location $cluster |
        Where { $_.PowerState -eq "PoweredOn" } |
        Get-VMGuest |
        Select VMName -ExpandProperty Disks |
        Select VMName, Path,
            @{Name="DiskCapacityGB";Expression={[math]::Round((($_.Capacity)/1GB),2)}},
            @{Name="DiskUsedMB";Expression={[math]::Round((($_.Capacity - $_.FreeSpace)/1MB),2)}},
            @{Name="DiskFreeMB";Expression={[math]::Round((($_.FreeSpace)/1MB),2)}} |
        Export-Csv -NoTypeInformation $outputfile}
    
    Disconnect-viserver
    

    The script creates a CSV file for each cluster.

  • Find records VM size and export to csv format

    Hello

    I need this script to export to csv format, but I have problems to make it work.

    Notice-EEG - ViewType VirtualMachine-property name, Config.Hardware.Device, LayoutEx | %{

    $viewVM = $_; $viewVM.Config.Hardware.Device |? {$_-is [VMware.Vim.VirtualDisk]} | %{

    # for each device VirtualDisk, get some info

    $oThisVirtualDisk = $_

    # get the disk element LayoutEx, which corresponds to the VirtualDisk

    $oLayoutExDisk = $viewVM.LayoutEx.Disk |? {$_. {Key - eq $oThisVirtualDisk.Key}

    # get the FileKeys that correspond to the LayoutEx-> sort items of this VirtualDisk

    $arrLayoutExDiskFileKeys = $oLayoutExDisk.Chain |? {$_-is [VMware.Vim.VirtualMachineFileLayoutExDiskUnit]}

    New-Object PSObject-property @ {} TypeName

    # Add the VM name

    VMName = $viewVM.Name

    # disc label, such as "disk 1".

    DiskLabel = $_. DeviceInfo.Label

    # the path to the data store for the file VirtualDisk

    DatastorePath = $_. Backing.FileName

    # the size provisioned to the VirtualDisk

    ProvisionedSizeGB = [Math]: tour ($_.) CapacityInKB / 1 MB, 1).

    # Get the file LayoutEx elements that correspond to the FileKeys for this LayoutEx disk and the size for items that are "diskExtents" (retrieved in the form of bytes, so convert to GB)

    SizeOnDatastoreGB = [Math]: round (($arrLayoutExDiskFileKeys | % {$_.)) FileKey} | % {$intFileKey = $_; $viewVM.LayoutEx.File |?} {($_. Eq - $intFileKey - button) and ($_.) Type - eq "diskExtent")}} | Measure - Object - size of the sum). Summary / 1 GB, 1).

    } # end new object

    } # end foreach-object

    } # end external foreach-object

    Capture all the output in a variable and send this variable in a CSV file.

    Something like that

    $result = Get-View -ViewType VirtualMachine -Property Name, Config.Hardware.Device, LayoutEx | %{
        $viewVM = $_; $viewVM.Config.Hardware.Device | ?{$_ -is [VMware.Vim.VirtualDisk]} | %{
            ## for each VirtualDisk device, get some info         $oThisVirtualDisk = $_        ## get the LayoutEx Disk item that corresponds to this VirtualDisk         $oLayoutExDisk = $viewVM.LayoutEx.Disk | ?{$_.Key -eq $oThisVirtualDisk.Key}
            ## get the FileKeys that correspond to the LayoutEx -> File items for this VirtualDisk         $arrLayoutExDiskFileKeys = $oLayoutExDisk.Chain | ?{$_ -is [VMware.Vim.VirtualMachineFileLayoutExDiskUnit]}
            New-Object -TypeName PSObject -Property @{
                ## add the VM name            VMName = $viewVM.Name
                ## the disk label, like "Hard disk 1"            DiskLabel = $_.DeviceInfo.Label
                ## the datastore path for the VirtualDisk file            DatastorePath = $_.Backing.FileName
                ## the provisioned size of the VirtualDisk            ProvisionedSizeGB = [Math]::Round($_.CapacityInKB / 1MB, 1)
                ## get the LayoutEx File items that correspond to the FileKeys for this LayoutEx Disk, and get the size for the items that are "diskExtents" (retrieved as bytes, so converting to GB)            SizeOnDatastoreGB = [Math]::Round(($arrLayoutExDiskFileKeys | %{$_.FileKey} | %{$intFileKey = $_; $viewVM.LayoutEx.File | ?{($_.Key -eq $intFileKey) -and ($_.Type -eq "diskExtent")}} | Measure-Object -Sum Size).Sum / 1GB, 1)
            } ## end new-object    } ## end foreach-object } ## end outer foreach-object
    
    $result | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture
    
  • Output on screen and EXPORT. CSV

    Hi all

    Were the creation of scripts for some general tasks and I got a little confused on what is happening.  I have two scripts that are output on the screen even with EXPORT. CSV commond to end.  I get the output sent to the CSV fine and there is no obvious errors.

    Any whay Idears, that this could happen?  I can post my script if that helps.

    Thank you

    Phil

    This is the line with only $objHba in it.

    It will display the content of this variable to the screen.

    # $vmlist = Get-VM
    $table = @()
    
    foreach ($vmhost in (Get-view -ViewType Hostsystem )) {
         foreach ($hba in ($vmhost.config.storagedevice.hostbusadapter | where {$_.PortWorldWideName})) {
              $objHba = "" | Select-Object Hostname,Device,Model,Driver,Pci,Wwpn
              $objHba.HostName = $vmhost.Name
              $objHba.Device = $hba.Device
              $objHba.Model = $hba.Model
              $objHba.Driver = $hba.Driver
              $objHba.Pci = $hba.Pci
              $objHba.Wwpn = "{0:x}" -f $hba.PortWorldWideName
    #           $objHba
              $table += $objHba
         }
    }
    
    $table | Export-Csv $outputDir\data\$date.$viserver.$filename.csv -NoTypeInformation
    

    BTW what is the first (of line) doing here? It has nothing to do with the rest of the script.

    Copy & paste mistake I guess?

    ____________

    Blog: LucD notes

    Twitter: lucd22

  • Leading zeros and export to CSV


    Hello

    I run a sql query for retrieving telephone numbers and when exporting to CSV 0 are deleted. I understand that this is a problem of excel formatting, however does it perform a work around in the SQL that can preserve the leading zeros when exporting to CSV?

    sliderrules wrote:

    The format of data is of type char

    SELECT «=» | test ctelno

    When they are copied to surpass the value bed = 0999 6767 in excel (including =)

    You need double quotes

    '="'|| c.ctelno |' » "

  • Reports on multiple virtual centers and export to CSV format

    Everyone,

    Does anyone know if possible a way for me to do the following in the following format.

    Requirements:

    (1) report on multiple virtual centers

    (2) export results to CSV or Excel with a title between each virtual Center

    (3) the need for Virtual Center Cluster belongs, name of the Cluster, host number of ESX and number of virtual machines. (Additional information can be added to the report later).

    It would be not so hard if I could just connect and report on all virtual centers and my results to export in this way.  However, each virtual server Center have a different purpose and that is how senior management wants to see the results.

    For example:

    VC3 VC1 and VC2 = service server environment

    VC4 and VC5 VC6 = service Workstation environments

    VC7 VC8 = environment of Cloud Computing Services

    What I would do is:

    (1) connect to the first 3 virtual centers and collect the above information.

    (2) I would like to create a title in the CSV or Excel named 'Standard Server environment summary'

    (3) then I would like to export these 4 columns with data for each cluster.

    Cluster is managed by this Virtual Center, the name of the Cluster, number # VM and ESX host

    I then unplug the 3 first virtual centres.

    Next

    (1) I would like to connect to the 3 next virtual centres

    2) add to the previous CSV or Excel with a title 'desktop environment summary.

    (3) export the same information as above in the CSV file

    Any help would be greatly appreciated.

    Try something like this

    $vcenters = @{}
    $vcenters["Server Environment"] = @("VC1","VC2","VC3")
    $vcenters["Workstation Environment"] = @("VC4","VC5","VC6")
    $vcenters["Cloud Environment"] = @("VC7","VC8")
    
    Set-PowerCLIConfiguration -DefaultVIServerMode Single -Confirm:$false | Out-Null
    $vcenters.GetEnumerator() | %{
      &{foreach($vc in $_.Value){
          $currentvc = Connect-VIServer -Server $vc      Get-Cluster -Server $currentvc | %{
            New-Object PSObject -Property @{
              "Cluster is managed by VC" = $currentvc.Name          Cluster = $_.Name          "# of ESX hosts" = @($_.ExtensionData.Host).Count          "# of VM" = Get-View $_.ExtensionData.Host -Server $currentvc |          %{@($_.Vm).Count} |          Measure-Object -Sum |          Select -ExpandProperty Sum        }
          }
        }} | Select "Cluster is managed by VC",Cluster,"# of ESX hosts","# of VM" |  Export-Xls -Path C:\report.xls -WorksheetName $_.Name  Disconnect-VIServer -Server $currentvc -Confirm:$false}
    

    You will have to render the Xls export function.

    Simplest is to include in the script with the code above.

    The name of the group is reflected in the name of the worksheet, having a separate line would be a little more difficult.

  • What kind of ADOBE I need for editing and exporting PDF files

    What kind of ADOBE should I get mount and export PDF files

    Acrobat.

    Be well...

  • How to extract data from an arbitrary xml file and export it to a CSV friendly?

    Hallo,
    I am facing big problems in the use of XML files. I have a
    application that generates XML files with clusters containing arrays
    and scalars as in the example pasted below. My task is to
    Read it and export the data into a CSV document readable by a human.
    Since I do not know the actual content of the cluster, I need some sort
    Smart VI through the XML looking for berries
    and other data structures for export properly in the CSV file
    format (columns with headers).
    Thank you



    3


    6


    0



    1



    2



    3



    4



    5




    3.14159265358979



    Ciao

    Rather than to get the

    node, you can just go directly to the node since ' one that really interests you. Basically what it means to determine the elements of table how much you have, and it depends on if you have 1 or 2 knots . The rest is just of the child nodes and the next siblings. See attachment as a starting point. The attached XML file is a table 2D (change the .xml extension).

    Notes on the example:

    • I did not close properly references, so it's something you need to do.
    • It is limited to tables 1 d or 2D.
    • I suggest using a control path of the file to specify the input XML file and path of the file/folder control to specify the location of the output file.

  • When I look at my pictures on library it loads and it is super grainy, as my iso was too high, but when I look on developing it seems. What is happening and how I keep good fixed when I get back to the library and export?

    When I look at my pictures on library it loads and it is super grainy, as my iso was too high, but when I look on developing it seems. What is happening and how I keep good fixed when I get back to the library and export?

    Please see: photos look grainy on library but not on developing

    soft blurry images / when you view in the library but strong in develop mode? Lightroom forums

    Kind regards

    Mohit

  • I'm missing the function of "organize bookmarks...". "on the Bookmarks tab. How can I get this feature so that I can import and export favorites?

    In the past, this feature was available, because I imported my IE Favorites and export information for backup reasons. I noticed that since I am updating to Firefox, I don't have more this option. I don't want to reinstall (if I don't) in fear of losing newly acquired bookmarks.

    View all bookmarks in Firefox 4.0 has been renamed this menu item.

  • APEX 5.0 Export to CSV produces duplicate records

    Good day to you all:

    I use APEX 5.0, and I have a classic report that has a total number of lines of 274.  (274 documents also in SQL Developer).  However, when I export the report to CSV, duplicate records are produced and the total number of lines increases to 365.  Has anyone already known this before bug in APEX 5.0?  I tried to reproduce the report to a type of interactive, but I get the same results when you export to CSV.  Advice or guidance would be appreciated.  Thank you.

    Aqua

    Hey Aqua,

    If you are APEX 5.0.0 or 5.0.1? And which version of the database, you are on?

    There was a problem with CLOB (which are used for the download) in 5.0.0 running on specific versions of 11 GR 2. A fix is included in the 5.0.1 patch set.

    Concerning

    Patrick

  • Export to CSV from a form of manually created tab

    Version 4.1.1.00.23

    Hello

    I have seen a lot of messages about the present, and of course, I do not find anything!

    So, I have a form tab created manually, built using the API APEX_ITEM and Collections and now demand came to allow the report to export in CSV and activation of export, I get all the values that are in the list select etc..

    How can I get the export of the recovered data in the report?

    Thank you

    Joe

    Fixed.

  • Can be combined in the same script Get - VM and Get - view?

    Hello

    I am trying to find a way to combine these two Get - VM and Get - view in the same script.

    Script # 1

    Get-Cluster Management | Get - VM | Select Name,@{Name="HardwareVersion; Expression = {$_.} ExtensionData.Config.Version}}

    Script # 2

    Get-Cluster Management | Get - vm | where {$_.powerstate - don't "PoweredOff"} | % {get-view $_.id} | Select Name, @{Name = "ToolsVersion"; {Expression = {$_.config.tools.toolsVersion}}, @{Name = "ToolStatus"; Expression = {$_.} Guest.ToolsVersionStatus}}

    I've always had problems try this out and now, I need to combine them into a single script in figure.

    If you can help me or point me in the right direction would be great.

    Thank you

    Mike

    Curiosity got the best of me so I came with this. You had more correct, but you need to make a custom object so that the properties you are looking for can all be used simultaneously.

    It does not use the pipeline and uses a foreach loop.

    ----------------------------------------------------------------------------------------------

    $vms = get-Cluster Management | Get - VM? {$_.powerstate - don't "PoweredOff"}

    {foreach ($vm to $vms)

    $VMID = get-View ($vm.id)

    New-object psobject-property @ {}

    VM = $vm

    HardwareVersion = $vm. ExtensionData.Config.Version

    ToolsVersion = $VMID.config.tools.toolsVersion

    ToolsStatus = $VMID. Guest.ToolsVersionStatus

    } | Select the virtual machine, ToolsVersion, HardwareVersion, ToolsStatus

    }

    ----------------------------------------------------------------------------------------------

    Or use this option if you like the pipeline with Foreach-Object approach.

    ----------------------------------------------------------------------------------------------

    Get-Cluster Management | Get - VM? {$_.powerstate - don't "PoweredOff"} | % {

    $VMID = get-View ($_.id)

    New-object psobject-property @ {}

    VM = $_

    HardwareVersion = $_. ExtensionData.Config.Version

    ToolsVersion = $VMID.config.tools.toolsVersion

    ToolsStatus = $VMID. Guest.ToolsVersionStatus

    } | Select the virtual machine, ToolsVersion, HardwareVersion, ToolsStatus

    }

    ----------------------------------------------------------------------------------------------

    Both methods are correct that I can see. I tried both and both work well. You can easily piple only out to Export-Csv at the end of the select line. I hope this helps.

    Andrew

    Post edited by: andrewsardinha
    condition of powerstate added to each block operator and foreach-object script to the last script block.

  • Get - VM and Get-Snapshot does not host/VMHost return

    Hello

    summer banging my head on this one a bit lately.

    When I run Get - VM | Select VMHost, name I got a return on the VMhost and comments.

    If I add a bit to the selection such as Get-Snapshot and a few more selections I don't get the VmHost but the rest of the information does so very well.

    Get - VM | Get-Snapshot | Select VMHost, name, Description, SizeMB

    Can anyone be so kind as to point out what I'm doing wong.

    Finally, so that's part of something that I intend to run a little more carefully all the line is something like this:

    Get - VM | Get-Snapshot | Select name, Description, VMHost SizeMB, @{N = "DaysOld"; E = {((get-date) - $_.)} Created). Days}} |? {$_. {DaysOld - gt 7} | Export-Csv "C:\Filename.csv.

    The cmdlet works, but just not with the host, who quitely franky would be useful

    Martin

    Post edited by: maishsk Object has been changed

    HI Martin,

    you select a snapshot object and not a virtual computer object properties. A snapshot object has different properties. You can use the following code:

    Get-VM |
    Get-Snapshot |
    Select-Object -Property @{N="VMHost";E={$_.VM.VMHost.Name}},
    @{N="VM";E={$_.VM.Name}}, Name, Description, SizeMB,
    @{N="DaysOld"; E={((Get-Date) - $_.Created).Days}} |
    Where-Object {$_.DaysOld -gt 7} |
    Export-Csv "C:\Filename.csv" -NoTypeInformation -UseCulture 
    

    Post edited by: maishsk
    Topic has been corrected

  • PowerCLI - export to CSV

    Hello world

    I'm trying to run a command powercli and export the results to a csv file. I run the command below, but when I opened the whole output CSV file is added in column 1 of Excel, but there are 4 columns of data. Is there a way I can get it in excel, but each of the columns (name, powerstate, num CPU, memory) are rather than 1 in 4 columns Excel?

    Get - vm | WHERE-object {$_.powerstate - eq "poweredoff"} > c:\temp\poweredoffvms.csv

    Thank you

    Scott

    If its possible, check below line

    Get-VM | Where-Object {$_.PowerState -eq "PoweredOff"} | Export-Csv -Path c:\poweredoff-vms.csv -NoTypeInformation
    

    And if you want just a few items, you can filter using "select."

    Get-VM | Where-Object {$_.PowerState -eq "PoweredOff"} | Select Name,PowerState,NumCPU,MemoryMB | Export-Csv -Path c:\poweredoff-vms.csv -NoTypeInformation
    

    To export to the csv file, you can use "Export-Csv.

Maybe you are looking for