VMware Cloud Community
antize
Enthusiast
Enthusiast

How to rename a VMDK with PowerCLI?

Pretense:

So I am currently in the process of writing a PowerCLI script that will convert targeted VMDKs from thin to thick format. What I'm trying to do is the PowerCLI equivalent of:

1. Convert to thin.

vmkfstools -i "$filename" -d thin "thin_$filename"

2. Move the original out of the way.

vmkfstools -E '$filename" "orig_$filename"

3. Move the new thin disk into place.

vmkfstools -E "thin_$filename" "$filename"

4. Remove the thick disk.

vmkfstools -U "orig_$filename"

Is there any way to rename a VMDK with the PowerCLI cmdlets as "vmkfstools -E" ?

Here is my script so far:

NOTE - I can't do the Get-VM approach that I've seen in another PowerCLI scripts because I'm using Lab Manager and the majority of the VMs are not registered in vCenter at any one given time, so I have to connect to the datastores directly.

param (
        [string] $esxHost,
        [string] $username,
        [string] $password
)

# Load PowerCLI
$psSnapInName = "VMware.VimAutomation.Core"
if (-not (Get-PSSnapin -Name $psSnapInName -ErrorAction SilentlyContinue))
{
     # Exit if the PowerCLI snapin can't be loaded
     Add-PSSnapin -Name $psSnapInName -ErrorAction Stop
}

if (-not $esxHost)
{
     $esxHost = Read-Host "Enter IP/Host of ESX Server To Connect"
}

# Connect to VCenter with current credentials
Connect-VIServer $esxHost -User $username -Password $password -ErrorAction Stop

cls

$datastores = @(Get-Datastore | ? {$_.Type -eq "VMFS"} | sort Name)

# Build a little menu in the shell.
$i = 1
$datastores | % {
     $choiceLabel = "${i} : $($_.Name)"
     Write-Host $choiceLabel
     $i += 1
}

Write-Host "`n"
$selection = Read-Host "Select Datastore"

$selectedDatastore = $datastores[$selection - 1]

Write-Host "`n"
$lmDirId = Read-Host "Enter Lab Manager Directory ID"

$disks = @(Get-HardDisk -Datastore $selectedDatastore -DatastorePath "[$selectedDatastore] labmanager/$lmDirId/" -ErrorAction Stop)

# Build a little menu in the shell.
$i = 1
$disks | % {
     $choiceLabel = "${i} : $($_.Name)"
     Write-Host $choiceLabel
     $i += 1
}

Write-Host "`n"
$selection = Read-Host "Select Disk"

$selectedDisk = $disks[$selection - 1]

$thinName = "thin_$($selectedDisk.Name)"
$thinPath = $selectedDisk.Filename -replace $selectedDisk.Name,$thinName
$thinDisk = $selectedDisk | Copy-HardDisk -Confirm -DestinationStorageFormat Thin -DestinationPath $thinPath -Force

if ($thinDisk)
{
     # ToDo
     # Remove the original thick disk.
     # Move the thin disk into place.
}

Reply
0 Kudos
13 Replies
avlieshout
VMware Employee
VMware Employee

Instead of using Copy-Harddisk you can use Set-Harddisk to move the original disk to another datastore and convert it.

This way there's no need for a rename.

$thinDisk = $selectedDisk | Set-HardDisk -StorageFormat Thin -Datastore<destDatastore> $thinPath -Confirm:$false

-


.

Arnim van Lieshout

Blogging: http://www.van-lieshout.com

Twitter: http://www.twitter.com/avlieshout

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
Reply
0 Kudos
avlieshout
VMware Employee
VMware Employee

You could use the datastore provider to rename a file.

rename-item vmstore:\<datacenter>\<datastore>\<vmfolder>\thin_file.vmdk -newname file.vmdk

-


Arnim van Lieshout

Blogging: http://www.van-lieshout.com

Twitter: http://www.twitter.com/avlieshout

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
Reply
0 Kudos
antize
Enthusiast
Enthusiast

Question 1

Can Set-HardDisk just change the format of the disk in the current location (instead of specifying a new location)?

This would save me the steps of getting rid of the original thick disk and renaming the thin disk.

Question 2

Is there any way to tell if a VMDK is thin or not using the method I am? Like I said I've seen other PowerCLI scripts determining whether a disk is thin but those scripts are using the Get-VM cmdlet or get-view -ViewType VirtualMachine which won't do the trick for me since I'm using Lab Manager and the majority of my VMs are not deployed (registered in vCenter). My approach needs to connect directly to the datastore.

Here is an example of what I'm trying to do, but by connecting directly to a datastore:

It looks like I need to get a hold of two types:

VMware.Vim.VirtualDisk

VMware.Vim.VirtualDiskFlatVer2BackingInfo

Is there anyway to create these types of objects by connecting directly to the datastore?

Reply
0 Kudos
LucD
Leadership
Leadership

1) No, the Set-HardDisk cmdlet help explictely states that you can only use the -StorageFormat parameter when you're moving a hard disk to another datastore.

2) I don't think you can create the backing objects without using a guest.

But why don't you use what is called a "helper VM" ?

You search through a datastore for all .vmdk files you can find.

If you find a .vmdk file, you attach it to a guest, the "help VM", and then you can determine if the disk is Thin or Thick.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
antize
Enthusiast
Enthusiast

Ah that's an interesting work around. Let me give that a try (Helper VM)... Will report back...

On a side note this takes forever!

[vSphere PowerCLI] vmstore:\ha-datacenter\FC_Array0\labmanager\15963> measure-command {get-item 015963-App_Server-0-0-sc
si.vmdk}

Days : 0

Hours : 0

Minutes : 2

Seconds : 0

Milliseconds : 459

Ticks : 1204598053

TotalDays : 0.00139421070949074

TotalHours : 0.0334610570277778

TotalMinutes : 2.00766342166667

TotalSeconds : 120.4598053

TotalMilliseconds : 120459.8053

Good grief!

Reply
0 Kudos
LucD
Leadership
Leadership

Fyi, I just published a function to find Thin virtual disks on a datastore without using virtual machine(s).

See my Finding Thin disks post.

Are you using PowerCLI 4.1 ? The providers became a lot faster in the latest version.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
antize
Enthusiast
Enthusiast

Ah how timely!

Will give this a try first before going the helper VM route.

LucD, what do you recommend for learning the VMware namespaces? Just browsing the SDK documentation?

http://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/

These are great finds!

$dsBrowser = Get-View $datastoreHandle.Extensiondata.Browser

$dsBrowser.SearchDatastoreSubFolders_Task etc...

The other day I read your Onyx 2.0 post and I gave it a try. Unfortunately it doesn't write any code when browsing around the datastore.

Reply
0 Kudos
antize
Enthusiast
Enthusiast

Just tried the script and I don't have a type loaded:

Unable to find type [ VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.DatastoreImpl [ ] ] : make sure that the assembly containing this type is loaded.

VMware.VimAutomation.ViCore is not my GAC. Apparently this is not enough:

Add-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction Stop

Reply
0 Kudos
avlieshout
VMware Employee
VMware Employee

Are you using PowerCLI 4.1?

The specified type is a new PowerCLI 4.1 typename.

Arnim

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
Reply
0 Kudos
LucD
Leadership
Leadership

Like Arnim already said, the script requires PowerCLI 4.1.

I added a test to the function to verify the PowerCLI version.

Learning the SDK APIs is a process that could take a bit of time.

The VMware vSphere API Reference Documentation is of course a good source to learn.

But there are also some other manuals that come with the SDK that could help.

Have, for example, a look at the Programming Guide.

And then there is of course the ultimate source for SDK information, Steve Jin's excellent book VMware VI and vSphere SDK: Managing the VMware Infrastructure and vSphere. The examples all use primarily Java, but it is the best source of SDK information around. Even for PowerCLI users.

In our upcoming book there will be a chapter dedicated to the SDK and another one to Onyx Smiley Wink

In Onyx there are a number of methods that are suppressed by default, one of these being the SearchDatastore_Task.

You can change this through the option.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
antize
Enthusiast
Enthusiast

Ah PowerCLI 4.1 , now we're cooking with gas! An order of magnitude faster in terms of the provider.

[vSphere PowerCLI] vmstore:\ha-datacenter\FC_Array0\labmanager\15963> measure-command {get-item 015963-App_Server-0-0-sc
si.vmdk}

Days : 0

Hours : 0

Minutes : 0

Seconds : 10

Milliseconds : 414

Ticks : 104143685

TotalDays : 0.000120536672453704

TotalHours : 0.00289288013888889

TotalMinutes : 0.173572808333333

TotalSeconds : 10.4143685

TotalMilliseconds : 10414.3685

Reply
0 Kudos
antize
Enthusiast
Enthusiast

Yep, I had 4.0.1 installed. Upgrading to 4.1 allowed me to run LucD's script and it gives me the info I need.

Thanks LucD and Arnim!

Now I have a way to query for non-thin disks Smiley Happy

My overall goal is to see which of my Lab Manager templates have not had their disks converted to thin format. So I think the next thing I have to do is come up with a SQL query for the Lab Manager database to figure out what directories the templates are stored in and then use LucD's implementation to figure out whether they are thin or not and then convert them if they are not thin.

Reply
0 Kudos
cfsullivan
Contributor
Contributor

Thanks, avlieshout. That PS command worked perfectly for me. I just needed to rename a couple of VMDKs to use for pre-seeding a vSphere Replication target. I had to first clone the source VM to the target site and it seems that you don't have control of the VMDK names on the target when you do a clone.

Reply
0 Kudos