VMware Cloud Community
Niram
Contributor
Contributor

get-datastore results

Hi all,

When running get-datasore command against an ESX server I get the results in this format.

Name                               FreeSpaceMB      CapacityMB
----                               -----------      ----------
datastore1                              167863          237056
DS2                                     101583          102144


When I'm running same command from script I get it in the following format:

DatacenterId         : Datacenter-datacenter-2
Datacenter           : New Datacenter
ParentFolderId       : Folder-group-s5
ParentFolder         : datastore
DatastoreBrowserPath : vmstores:\localhost@443\New Datacenter\datastore1
FreeSpaceMB          : 167863
CapacityMB           : 237056
Accessible           : True
Type                 : VMFS
ExtensionData        : VMware.Vim.Datastore
Id                   : Datastore-datastore-10
Name                 : datastore1
Uid                  : /VIServer=@localhost:443/Datastore=Datastore-datastore-1
                       0/


DatacenterId         : Datacenter-datacenter-2
Datacenter           : New Datacenter
ParentFolderId       : Folder-group-s5
ParentFolder         : datastore
DatastoreBrowserPath : vmstores:\localhost@443\New Datacenter\DS2
FreeSpaceMB          : 101583
CapacityMB           : 102144
Accessible           : True
Type                 : VMFS
ExtensionData        : VMware.Vim.Datastore
Id                   : Datastore-datastore-67
Name                 : DS2
Uid                  : /VIServer=@localhost:443/Datastore=Datastore-datastore-6


Any idea how to get my script show the results as from regular command?

Thanks,

Nir

Reply
0 Kudos
10 Replies
RvdNieuwendijk
Leadership
Leadership

Hi Nir,

your problem has to do with PowerShell output formatting. The output you get from the Get-Datastore command is the default output as defined by PowerCLI. You get only the Name, FreeSpaceMB and CapacityMB properties in a table format. The output from the script is all the properties in a list format. To get the same output in your script you can try:

Get-Datastore | `
Select-Object -Property Name,FreeSpaceMB,CapacityMB | `
Format-Table

In this case you do all the formatting yourself.

Regards, Robert

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

Hi Robert,

Thanks for the quick reply.

When I run the command from PowerCLI I get results but when I combine it in my script I get the following error:

out-lineoutput : Object of type "Microsoft.PowerShell.Commands.Internal.Format.
FormatStartData" is not legal or not in the correct sequence. This is likely caused by a user-specified "format-table" command which is conflicting with the default formatting.

Thanks,

Nir

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

Hi Nir,

can you share your script so I can see what it does?

Regards, Robert

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

Sure.

It's a mass VM creation script:

cls
$VIServer = Read-Host "Please enter your Virtual Center name"
Connect-VIServer $VIServer
if ($? -match "true")
{
    $vmhost = Read-Host "Please enter the ESX name"
    ping $vmhost > null
    if ($? -match "true")
    {
        $amount = Read-Host "Please enter the amount of Vms you wish to create"
        $name = Read-Host "Please enter the prefix name of the VMs"
        Write-Host " "
        Write-Host "These are the available datastores"
        Get-Datastore | ` Select-Object -Property Name,FreeSpaceMB,CapacityMB | ` Format-Table
        Write-Host " "
        $ds_user = Read-Host "Please enter the datastore you want to create the VMs on"
        $ds = get-datastore -name $ds_user
        if ($? -match "true")
        {
            Write-Host " "
            Write-Host (8*$amount)"GB will be used which will leave "($ds.FreeSpaceMB/1024)"GB From the datastore."
            $continue = Read-Host "Would you like to continue? (yes/no)"
            if ($continue -match "yes")
            {
                1..$amount | foreach {new-vm -name $name$_ -datastore $ds -vmhost $vmhost -template "New Template" -oscustomizationspec CUSTOM1}
            }
            else
            {
            Write-Host "Aborted by user"}
        }
    }
    else {
    Write-Host $vmhost " could not be reached"}
}

Thanks,

Nir

Reply
0 Kudos
Niram
Contributor
Contributor

cls
$VIServer = Read-Host "Please enter your Virtual Center name"
Connect-VIServer $VIServer
if ($? -match "true")
{
    $vmhost = Read-Host "Please enter the ESX name"
    ping $vmhost > null
    if ($? -match "true")
    {
        $amount = Read-Host "Please enter the amount of Vms you wish to create"
        $name = Read-Host "Please enter the prefix name of the VMs"
        Write-Host " "
        Write-Host "These are the available datastores"
        Get-Datastore | ` Select-Object -Property Name,FreeSpaceMB,CapacityMB | ` Format-Table
        Write-Host " "
        $ds_user = Read-Host "Please enter the datastore you want to create the VMs on"
        $ds = get-datastore -name $ds_user
        if ($? -match "true")
        {
            Write-Host " "
            Write-Host (8*$amount)"GB will be used which will leave "($ds.FreeSpaceMB/1024)"GB From the datastore."
            $continue = Read-Host "Would you like to continue? (yes/no)"
            if ($continue -match "yes")
            {
                1..$amount | foreach {new-vm -name $name$_ -datastore $ds -vmhost $vmhost -template "New Template" -oscustomizationspec CUSTOM1}
            }
            else
            {
            Write-Host "Aborted by user"}
        }
    }
    else {
    Write-Host $vmhost " could not be reached"}
}

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

The problem is in the combination of the Write-Host lines of your script and the PowerShell output formating. These can not be combined. You can try to format the output of the Get-Datastore cmdlet yourself with Write-Host cmdlets. Like in:

Write-Host "Name`t`tFreeSpaceMB`tCapacityMB"
Get-Datastore | `
  ForEach-Object {Write-Host "$($_.Name)`t$($_.FreeSpaceMB)`t`t$($_.CapacityMB)"}

 

`t stands for a tab character.

Message was edited by: RvdNieuwendijk

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

This is just great, thanks! Smiley Happy

So there's no way I can format the output as a table?

Thanks,

Nir

P.S.

As you can see I'm pretty new to PowerShell/PowerCLI, if you have any comments about the script I'd be happy to hear.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

I have made some changes to the script to make life easier. First I used the param statement to read the parameters. Now you can run the script like:

.\MyScript -VIserver vCenter -VMHost esx1 -Amount 3 -Prefix Server_ -datastore Datastore


If you don't specify the parameters on the commandline, there will still be asked for these parameters. If you don't specify the Datastore parameter, you will still get the list of available datastores. But only the datastores available on the specified ESX server.

Further I changed the ping statement in a .NET solution.

Have fun!

param([parameter(Mandatory=$true)][string]$VIServer,
      [parameter(Mandatory=$true)][string]$VMHost,
      [parameter(Mandatory=$true)][int]$Amount,
      [parameter(Mandatory=$true)][string]$Prefix,
      [string]$Datastore
)

Connect-VIServer $VIServer
if ($? -match "true")
{
    $ping = New-Object System.Net.NetworkInformation.Ping
    $Reply = $ping.send($VMHost)
    if ($Reply.status –eq Success) 
    {
        if (-not $Datastore) {
            Write-Host ""
            Write-Host "These are the available datastores"
            Write-Host "Name`t`tFreeSpaceMB`tCapacityMB"
            Write-Host "----`t`t----------`t----------"
            Get-VMHost $VMHost | Get-Datastore | `
                  ForEach-Object {Write-Host "$($_.Name)`t$($_.FreeSpaceMB)`t`t$($_.CapacityMB)"}
                Write-Host " "
            $Datastore = Read-Host "Please enter the datastore you want to create the VMs on"
        }
        $ds = Get-VMHost $VMHost | Get-Datastore -name $Datastore
        if ($? -match "true")
        {
            Write-Host " "
            Write-Host (8*$Amount)"GB will be used which will leave "($ds.FreeSpaceMB/1024)"GB From the datastore."
            $continue = Read-Host "Would you like to continue? (yes/no)"
            if ($continue -match "yes")
            {
                1..$amount | foreach {new-vm -name $Prefix$_ -datastore $ds -vmhost $vmhost -template "New Template" -oscustomizationspec CUSTOM1}
            }
            else
            {
            Write-Host "Aborted by user"}
        }
    }
    else {
    Write-Host "$vmhost could not be reached"}
}

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

Thank you so much for this Robert.

Much appreciated.

Nir

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

You're welcome.

Robert

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