VMware Cloud Community
Pilu1978
Enthusiast
Enthusiast

Automate Migration through PowerCli

Hi,

We have VC 2.5 U2 and ESX 3.5 U4 in our environment. We have build our new environment with VSpshere 4.1 U1. Now we will migrating VM from old to new VC. We want to automate this task using powercli. Being a novice in Powercli need your help execute this task properly. Our planis as mentioned below:

1. Everyday we will migrate 30 VMs

2. We will first collect the inventory (like CPU, memory, Netowrk information , HardDisk information etc) of these VMs and save it in a csv or xls file. Each VM has two NICs and multiple harddisks

3. Export folderpath (blue) of these 30 VMs and save it to a csv file.

4. Poweroff each VM and unregister it from the OLD VC

5.Import the folderpath in the New VC

6. Poweron each VM and reister it in the New VC

7. Upgrade the Hardware and VMwareTools.

I have tried to write a script to get the inventory of the VM but all the information are not getting displayed in the xls or csv file. However when running the same command in command prompt it is showing properly.

Need your help to achieve the above.

0 Kudos
11 Replies
LucD
Leadership
Leadership

I suspect that your line to report the information looks something like this

Get-VM | Select Name

and probably a bunch of other properties on that Select cmdlet.

If you do this

Get-VM | Select Name | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture

you will save the output in a CSV file.

Does this work for you ?


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

0 Kudos
Pilu1978
Enthusiast
Enthusiast

Hi,

Thanks for your reply.

From the above command only name will be displayed in the csv file. We have two virtual nics attached to each vm and it has multiple disk attached to it.

I want to retrieve the entire information of a VM like:

1.Name

2.IP Address

3. Number of CPU

4. Memory in MB

5. OS installed

6. Network adapater name

7. MacAddress

8. Port Group Name

9.Hard Disk Number

10. VMdk path/datastore

11. HDD Size in GB

12. Hostname

I have tried to create the script but not every information is coming in the report. Need help to get the information.

0 Kudos
Pilu1978
Enthusiast
Enthusiast

Hi ,

I have attached the script which I have written to collect the VM information. But network card information are not showing in the excel files. Nework fields are in the excel files are blank. Not sure why it is not showing.

0 Kudos
RvdNieuwendijk
Leadership
Leadership

The next PowerCLI script retrieves the information you asked for in your second post and writes the information in a .csv file. If a VM has more than one IP address, network interface or hard disk it will put the information for all the objects in the same field seperated by semicolons.

Get-VM | `
  ForEach-Object {
    $VM = $_
    $NetworkAdapter = $VM | Get-NetworkAdapter
    $HardDisk = $VM | Get-HardDisk
    $VM | `
      Select-Object -Property Name,
        @{N="IPAddress";E={[string]::Join(";",$_.Guest.IPAddress)}},
        NumCPU,
        MemoryMB,
        Guest,
        VMHost,
        @{N="NetworkAdapter";E={([string]::Join(";",$($NetworkAdapter | ForEach-Object {$_.Name})))}},
        @{N="MacAddress";E={([string]::Join(";",$($NetworkAdapter | ForEach-Object {$_.MacAddress})))}},
        @{N="PortGroup";E={([string]::Join(";",$($NetworkAdapter | ForEach-Object {$_.NetworkName})))}},
        @{N="HardDisk";E={([string]::Join(";",$($HardDisk | ForEach-Object {$_.Name})))}},
        @{N="FileName";E={([string]::Join(";",$($HardDisk | ForEach-Object {$_.FileName})))}},
        @{N="CapacityGB";E={([string]::Join(";",$($HardDisk | ForEach-Object {$_.CapacityKB/1MB})))}}
  } | `
  Export-Csv "report.csv" -NoTypeInformation -UseCulture

Regards, Robert

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

Thanks for the script. It is working perfectly. But instead of taking the invenory of all the VMs if I try fetch the computer names from text file and run the below script:

Get-VM -Name (Get-Content c:\serverlist.txt) | ForEach-Object {
    $VM = $_
    $NetworkAdapter = $VM | Get-NetworkAdapter
    $HardDisk = $VM | Get-HardDisk
    $VM | `
      Select-Object -Property Name,
        @{N="IPAddress";E={[string]::Join(";",$_.Guest.IPAddress)}},
        NumCPU,
        MemoryMB,
        Guest,
        VMHost,
        @{N="NetworkAdapter";E={([string]::Join(";",$($NetworkAdapter | ForEach-Object {$_.Name})))}},
        @{N="MacAddress";E={([string]::Join(";",$($NetworkAdapter | ForEach-Object {$_.MacAddress})))}},
        @{N="PortGroup";E={([string]::Join(";",$($NetworkAdapter | ForEach-Object {$_.NetworkName})))}},
        @{N="HardDisk";E={([string]::Join(";",$($HardDisk | ForEach-Object {$_.Name})))}},
        @{N="FileName";E={([string]::Join(";",$($HardDisk | ForEach-Object {$_.FileName})))}},
        @{N="CapacityGB";E={([string]::Join(";",$($HardDisk | ForEach-Object {$_.CapacityKB/1MB})))}}
  } | `
  Export-Csv "report.csv" -NoTypeInformation -UseCulture

I am getting these below error:

Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or
empty. Supply an argument that is not null or empty and then try the command ag
ain.
At line:1 char:13
+ get-vm -name <<<<  (get-content c:\serverlist.txt)
    + CategoryInfo          : InvalidData: (:) [Get-VM], ParameterBindingValid
   ationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.GetVM.

Not sure what I am doing wrong here. Need help

0 Kudos
LucD
Leadership
Leadership

Looks like the Get-Content c:\serverlist.txt part is not working.

Is the filename correct ?

Does it return the names when you do

Get-Content c:\serverlist.txt


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

0 Kudos
Pilu1978
Enthusiast
Enthusiast

Yes the filename is correct and it also returns the name successfully. But whenever I am running Get-VM -Name (Get-Content c:\serverlist.txt) command I am getting the below error both from command prompt and inside the script. I have spent entire last week with this error and could not find out the reason of the below error. Need your help to find the resolution.

0 Kudos
LucD
Leadership
Leadership

Is there only 1 name in that file ?

Would you mind attaching the file in question ?


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

0 Kudos
Pilu1978
Enthusiast
Enthusiast

Please find the file attached here with.

0 Kudos
LucD
Leadership
Leadership

Works perfectly for me, nothing wrong with the file itself.

Are you sure the file is in that location on the machine where you execute the script ?


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

0 Kudos
Pilu1978
Enthusiast
Enthusiast

Yeah I am sure that the location of the file is correct but not sure why it is not working for me. Anyway I have found a alternative to this problem. Instead of the ServerList.txt file when I am using ServerList.csv and  it is working perfectly. So now I decided to use the csv file instead of txt file. Thanks for your help and support so far.

I need one more help regarding automation. It is regarding the blue folders. We have  a huge folder structure in VM and Templates view in old  VC to manage the VMs easily. Folder structure is created on basis of Application name with the subfolders based on the projects running on the an so on. I have shown one of such folder srtucture below for a application as an example. We want to have  same folder sturcture in the new VC. N. But as we are no migrating all the VMs all together to the new VC so I wanted to have script which will first check the folder path of a each VM in old VC and it will check if the same path is available in the New VC, if not then create the folder strycture.

One of such folder sturcture:

  • Datacenter
    • Sieble (application name)
      • XYZ (Project name)
        • Development (environment)
          • S0001(Windows server name)
        • PreProduction
          • S0002
          • S0003
        • Production
          • S0004
          • S0005

Is it achievable?  If yes then I have doubt like when we register a VM over GUI it asks for Folder name, Cluster, Resourcepool, Host etc. Now in script as we create the folder structure first and then register the VM. When we register a VM with New-VM command there is no option of mentioning the folder path. So how it is done through script?

0 Kudos