OK So this is literally my first attempt at working with power shell so forgive the messy code and drafting at the moment.
My question is bascially.... Why does the Get-HardDisk -vm vm Command show different output .....basically a really detailed disk infomation when run with the connect-VIServer command and a less detailed more compact list when run without the connect-viserver command in the script
# Setup Environment
Connect-VIServer vcfhesc
clear-variable -name ConfirmDisk
clear-variable -name SERVERNAME
# $SERVERNAME =
# Start Script
$SERVERNAME = Read-Host "Type the Server Name that you would like to add the Drive to"
Get-HardDisk -VM $SERVERNAME
$ConfirmDisk = Read-Host "Confirm The current disks? A new drive will be added! (y/n)"
If ($ConfirmDisk -eq "y") {
New-HardDisk -CapacityGB 300 -VM $SERVERNAME
Get-HardDisk -VM $SERVERNAME
}
I need to make sure the script connects to the vcenter but when i do the get-disk -vm vm output shows this (which is way to much information):
StorageFormat : Thin
Persistence : Persistent
DiskType : Flat
Filename : [Local0751] CM_PR1_0751/CM_PR1_0751.vmdk
CapacityKB : 104857600
CapacityGB : 100
ParentId : VirtualMachine-vm-135900
Parent : CM_PR1_0751
Uid : /VIServer=admin\judsonm@vcfhesc:443/VirtualMachine=VirtualMachine-vm-135900/HardDisk=2000/
ConnectionState :
ExtensionData : VMware.Vim.VirtualDisk
Id : VirtualMachine-vm-135900/2000
Name : Hard disk 1
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
StorageFormat : Thick
Persistence : Persistent
DiskType : Flat
Filename : [Local0751] CM_PR1_0751/CM_PR1_0751_1.vmdk
CapacityKB : 262144000
CapacityGB : 250
ParentId : VirtualMachine-vm-135900
Parent : CM_PR1_0751
Uid : /VIServer=admin\judsonm@vcfhesc:443/VirtualMachine=VirtualMachine-vm-135900/HardDisk=2001/
ConnectionState :
ExtensionData : VMware.Vim.VirtualDisk
Id : VirtualMachine-vm-135900/2001
Name : Hard disk 2
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
If I comment out or remove the connect-VIserver command at the beginning of the script then then output of the get-harddisk -vm vm command changes to the less detailed list which is what i want.
# Setup Environment
# Connect-VIServer vcfhesc
clear-variable -name ConfirmDisk
clear-variable -name SERVERNAME
# $SERVERNAME =
# Start Script
$SERVERNAME = Read-Host "Type the Server Name that you would like to add the Drive to"
Get-HardDisk -VM $SERVERNAME
$ConfirmDisk = Read-Host "Confirm The current disks? A new drive will be added! (y/n)"
If ($ConfirmDisk -eq "y") {
New-HardDisk -CapacityGB 300 -VM $SERVERNAME
Get-HardDisk -VM $SERVERNAME
}
CapacityGB Persistence Filename
---------- ----------- --------
100.000 Persistent [Local0751] CM_PR1_0751/CM_PR1_0751.vmdk
250.000 Persistent [Local0751] CM_PR1_0751/CM_PR1_0751_1.vmdk
This output is what i want displayed not all the detailed information that is shown above
That is due to the way PowerShell handles output.
At the end of the script, the PowerShell output formatter looks at the objects in the pipeline.
It determines the type of object, and then looks up which format to use for that type.
The definitions how specific types shall be output are defined in XML files (for PowerCLI objects these XML files are located in the PowerCLI installation folder).
What happens in your case, and what explains the difference in formatting is the following.
The Connect-VIServer cmdlet places a VIServer object in the pipeline, the Get-Harddisk cmdlet places one or more HardDisk objects in the pipeline.
At the end of the script, the output formatter sees the VIServer object first, and looks up the type in the XML table.
The next objects that it sees are the HardDisk objects, but it doesn't look up that type anymore, so you get the full output.
To avoid that the VIServer object influences that formatting of the output at the end of the script, you can just throw away that object (provided you don't need it further in the script).
That can be done via an Out-Null cmdlet.
In short that could look like this
Connect-VIServer vcfhesc | Out-Null
Get-HardDisk -VM $SERVERNAME
Now you will again get the short display of the HardDisk objects
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you Very Much!
I was able to add the | out-null to the end of the cmdlet then the get-hardisk cmdlet gave the short output.
However Now I decided to add a cmdlet to get the size of the data store and verify the current disk on a particular VM but when I use the out-null it prints the detailed data again so when I add out-null to the end to get the smaller get-harddisk output it complete removes the Datastore information from the script output.
# Setup Environment
$ErrorActionPreference = "stop"
Connect-VIServer vcfhesc | Out-Null
# Start Script
$SERVERNAME = Read-Host "Type the Server Name that you would like to add the Drive to"
Get-VM -Name $SERVERNAME | Get-Datastore
Get-HardDisk -VM $SERVERNAME
This is what I get:
Type the Server Name that you would like to add the Drive to: CM_pr1_2031
Name FreeSpaceGB CapacityGB
---- ----------- ----------
Local2031 1,379.818 3,723.750
StorageFormat : Thin
Persistence : Persistent
DiskType : Flat
Filename : [Local2031] CM_PR1_2031/CM_PR1_2031.vmdk
CapacityKB : 104857600
CapacityGB : 100
ParentId : VirtualMachine-vm-135903
Parent : CM_PR1_2031
Uid : /VIServer=admin\judsonm@vcfhesc:443/VirtualMachine=VirtualMachine-vm-135903/HardDisk=2000/
ConnectionState :
ExtensionData : VMware.Vim.VirtualDisk
Id : VirtualMachine-vm-135903/2000
Name : Hard disk 1
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
StorageFormat : Thick
Persistence : Persistent
DiskType : Flat
Filename : [Local2031] CM_PR1_2031/CM_PR1_2031_1.vmdk
CapacityKB : 262144000
CapacityGB : 250
ParentId : VirtualMachine-vm-135903
Parent : CM_PR1_2031
Uid : /VIServer=admin\judsonm@vcfhesc:443/VirtualMachine=VirtualMachine-vm-135903/HardDisk=2001/
ConnectionState :
ExtensionData : VMware.Vim.VirtualDisk
Id : VirtualMachine-vm-135903/2001
Name : Hard disk 2
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
# Setup Environment
$ErrorActionPreference = "stop"
Connect-VIServer vcfhesc | Out-Null
# Start Script
$SERVERNAME = Read-Host "Type the Server Name that you would like to add the Drive to"
Get-VM -Name $SERVERNAME | Get-Datastore | out-null
Get-HardDisk -VM $SERVERNAME
This is what I get with out-null added to the end of the cmdlet to check the datastore size.
Type the Server Name that you would like to add the Drive to: CM_pr1_2031
CapacityGB Persistence Filename
---------- ----------- --------
100.000 Persistent [Local2031] CM_PR1_2031/CM_PR1_2031.vmdk
250.000 Persistent [Local2031] CM_PR1_2031/CM_PR1_2031_1.vmdk
What can I do to get the summary output of both the datastore storage and the harddisks
I essentially want it to look like this:
Name FreeSpaceGB CapacityGB
---- ----------- ----------
Local2031 1,379.818 3,723.750
CapacityGB Persistence Filename
---------- ----------- --------
100.000 Persistent [Local2031] CM_PR1_2031/CM_PR1_2031.vmdk
250.000 Persistent [Local2031] CM_PR1_2031/CM_PR1_2031_1.vmdk
Disregard!
I figured out a way. I was able to add out-host | out-null to the end of the datastore cmdlet and it worked out. There may be a better way to do this, but for now I found a way.
# Setup Environment
$ErrorActionPreference = "stop"
Connect-VIServer vcfhesc | Out-Null
# Start Script
$SERVERNAME = Read-Host "Type the Server Name that you would like to add the Drive to"
Get-VM -Name $SERVERNAME | Get-Datastore | out-host | out-null
Get-HardDisk -VM $SERVERNAME
Now the script output is:
Type the Server Name that you would like to add the Drive to: CM_pr1_2031
Name FreeSpaceGB CapacityGB
---- ----------- ----------
Local2031 1,379.817 3,723.750
CapacityGB Persistence Filename
---------- ----------- --------
100.000 Persistent [Local2031] CM_PR1_2031/CM_PR1_2031.vmdk
250.000 Persistent [Local2031] CM_PR1_2031/CM_PR1_2031_1.vmdk
As a matter of fact, the following should be sufficient
Get-VM -Name $SERVERNAME | Get-Datastore | out-host
What happens is that you force the output formatter to act on the objects in the pipeline at that point in time, instead of doing that at the end of the script.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference