PowerCLI - Get VM Disk Partition Type - powershell

I'm looking to conduct an audit on our virtual environment to get the disk partition types (MBR, GPT) of our VMs. I haven't found any documentation in PowerCLI to get the partition type. Any ideas how I can go about this? Thanks!

That sort of information is normally not known at the VM object level and instead known at the Guest-OS level. If the VMs you're working with have VMware Tools (or Open VM Tools), you can still use PowerCLI to run scripts against them to pull that information with Invoke-VMScript (docs), but you'll still need to write your own code to pass to the guest OS to pull partition type.
If they're windows systems, you may be able to do something as simple as:
Invoke-VMScript -ScriptText {Get-Partition | select DriveLetter, Type} -VM VMName -GuestCredential $guestCredential

Thanks #Kyle Ruddy!
This was what I did:
$vmName = "VM NAME"
$output = Invoke-VMScript -ScriptText {Get-Disk | select Number, #{name='Size (GB)';expr={[int]($_.Size/1GB)}}, PartitionStyle} -VM $vmName -GuestUser $Username -GuestPassword $Password
$output.ScriptOutput | FT -AutoSize

Related

How to run VMware commands from remote scripts on windows

Have a local basic Powershell form for searching and creating VMware virtual machines.
Using new powershell powerCLI module, as described in link
Let's take Get-VM for example:
LOGIC: Type a certain string in TextBox > click search > prints VM's status/parameters in the form
The problem is, I can't execute Get-VM straight away from the script, but first have to connect using Connect-VIServer command and only than Get-VM will work
Is there any way to do it from the script? Something similar to -m flag of commands plink or putty.
Like: Connect-VIServer -server testvc -flagForExample "commands_list.txt"
Yes, you can. Before providing an immediate answer I'd like to explain what is actually happening.
When you call Connect-VIServer the command sets the value of the variable $DefaultVIServer behind the scenes, which is later used by other cmdlets (such as Get-VM).
However, the Get-VM documentation states that there is a Server parameter available. Which means that you can store your server connection in a variable and then pass it to the Get-VM cmdlet.
Here's a pseudo-code example:
$server = Connect-VIServer -server testvc
Get-VM -Server $server
Furthermore, the Get-VM supports an array of servers, so theoretically you can run the cmdlet on multiple servers at once. For example:
$server1 = Connect-VIServer -server testvc
$server2 = Connect-VIServer -server testvc2
Get-VM -Server #($server1, $server2)

New-VM - "The operation is not supported on this object"

I am trying to clone a VM using PowerCLI. Running the following snippet returns the error "The operation is not supported on the object."
$vmHost = Get-VMHost
New-VM -Name VM1 -VM SourceVM -VMHost $vmHost
Why does this return: 'operation not supported on the object error'
There is only one host, so it's not an issue with $vmHost selection.
Thanks
I'm recreating this error with PowerCLI 6.5R1 directly connected to the ESXi host rather than to the vCenter server (with Connect-VIServer). The New-VM line you have works fine when connected to vCenter. So I'd suggest doing that if you're able. Otherwise it might be something to take up with VMware. If you don't have a support contract, you could try their Forums.
edit - You can't clone a vm without vCenter (thanks #Jelphy) although in a pinch you could copy the disks.

Getting Azure VM OS name using PowerShell

I have been trying to get the VM OS name from Microsoft Azure using PowerShell.
I think I am very close to the solution but I don't know where I'm going wrong.
This is the command that I am using to get the VM details:
Get-AzureRmVM -ResourceGroupName TEST -Name VF-Test1 | Select OsType
The answer I get is just blank.
When running the following command:
Get-AzureRmVM -ResourceGroupName TEST -Name VF-Test1
I get all the details that belong to that VM.
The osType property lives inside $_.StorageProfile.osDisk
Get-AzureRmVM -ResourceGroupName TEST -Name VMNAME |
Format-Table Name, #{l='osType';e={$_.StorageProfile.osDisk.osType}}
Name osType
------ ------
VMNAME Windows
Use https://resources.azure.com to explore the object representation when in doubt, or pipe to Show-Object, like i did below.
You can get resource groups' VMs by Get-AzureRmVM and classic VMs by Get-AzureVM. Both of the returning values of the two cmdlets contain OS type properties but in different paths.
For the cmdlet Get-AzureRmVM, the OS type property path is $vm.StorageProfile.OsDisk.OsType
For the cmdlet Get-AzureVM, the OS type property path is $vm.VM.OSVirtualHardDisk.OS
There exists a sample code about fetching Azure VM OS Type here: https://gallery.technet.microsoft.com/How-to-retrieve-Azure-5a3d3751
Get-AzVM -name SERVERNAME | select name, #{n="OS";E={$_.StorageProfile.OsDisk.OsType}}, #{n="Offer";E={$_.StorageProfile.ImageReference.offer}} , #{n="SKU";E={$_.StorageProfile.ImageReference.sku}}, #{n="Publisher";E={$_.StorageProfile.ImageReference.Publisher}}
RESULT:

Connects a virtual network adapter to a virtual switch

How can connect a virtual switch to a virtual machine from powershell by Ids?
I tried:
# get virtual machine object
$vm = get-vm -Id '...id...'
# get virtual switch object
$vs = get-switch -Id '...id...'
# connect both
connect-vmnetworkadapter -vm $vm -vmswitch $vs
I want to connect VM to VS by Ids because the name is not unique.
I get the error:
Connect-VMNetworkAdapter : Parameter cannot be processed because the parameter name 'vm'
is ambiguous. Possible match include: -VMName -VMNetworkAdapter -VMSwitch -Name.
The error message is actually rather self-explanatory. The parameter -VM is ambiguous, i.e. the Connect-VMNetworkAdapter cmdlet has more than one parameter beginning with VM. Use the parameter -VMName with the name of the VM:
Connect-VMNetworkAdapter -VMName $vm.Name -VMSwitch $vs
If you can't use the name you must read the adapter from a pipeline, since AFAICS Connect-VMNetworkAdapter doesn't accept VM objects as input.
$vm.NetworkAdapters | Connect-VMNetworkAdapter -VMSwitch $vs
You may need to select the right adapter, if the VM has more than one, though.

With PowerShell, is there a way to make an external hard disk attached to host available to VM?

I have Hyper-V module installed but don't know what cmdlet to use. I've seen blogs showing how to do this using Hyper-V Manager. I have added a VHDx disk image with following cmdlet:
Add-VMHardDiskDrive -VMName MyWin7PC -ControllerType IDE -ControllerNumber 0 `
-ControllerLocation 0 -Path "C:\Virtual Hard Disks\VDisk.MyWin7PC.Vhdx"
What I now need is a way for VM to have another drive E: which will show files and folders the host has on its G: drive (which is physically connected to a USB hard disk). I need this temporarily to install applications from the USB hard disk. You can do this using the Hyper-V Manager GUI.
I don't use Win8, so this is entirely untested. Judging from the description of Add-VMHardDiskDrive something like this might work, though:
$usbdisk = gwmi Win32_DiskDrive | ? { $_.PNPDeviceID -like 'USBSTOR\*' }
Add-VMHardDiskDrive -VMName MyWin7PC -ControllerType IDE -ControllerNumber 0 `
-ControllerLocation 1 -DiskNumber $usbdisk.Index
You have to make this disk Offliine. Try this:
"select disk 1","offline disk" | diskpart
Where 1 is your USB HD id. And then use Add-VMHardDiskDrive. If you want to do this when VM is online you must use SCSI Controller in VM.