Exploring PowerShell: Understanding a Script to Move VMware Virtual Machines to a specific folder.

PowerShell is a powerful automation tool used to manage and automate Windows-based systems. With its rich set of commands, it can help administrators perform various tasks, including managing virtual machines (VMs) on VMware platforms. In this blog post, we will explain a PowerShell code that imports a CSV file containing a list of VMs and moves them to a specified folder.

Importing a CSV File

The first line of the code imports a CSV file containing a list of VMs. This is done using the “Import-Csv” cmdlet, which reads data from a CSV file and creates a PowerShell object for each row in the file. In this case, the CSV file is located in the “C:\temp” folder and is named “VmList.csv”. Once the CSV file is imported, the pipeline operator “|” is used to send the PowerShell object to the next command in the pipeline.

$vm = Get-VM -Name $_.Name

The next command in the pipeline is a “ForEach-Object” cmdlet, which is represented by the “%” symbol. This cmdlet loops through each object in the pipeline and performs a set of commands for each object. In this case, it retrieves the virtual machine name from the current object using the “$_.Name” syntax and assigns it to the “$vm” variable.

$folder = Get-Folder -Name “Folder_Name”

The next line of code retrieves the folder where the VMs will be moved. In this case, the folder is named “Folder_Name”, and it is retrieved using the “Get-Folder” cmdlet. The folder object is then assigned to the “$folder” variable.

Move-VM -VM $vm -Destination $folder -ErrorAction Stop

The “Move-VM” cmdlet is used to move the VM to the destination folder. The “-VM” parameter specifies the virtual machine to be moved, which is represented by the “$vm” variable. The “-Destination” parameter specifies the destination folder where the VM will be moved, which is represented by the “$folder” variable. The “-ErrorAction Stop” parameter ensures that any errors that occur during the move operation will cause the command to stop.

Error Handling

In the event that an error occurs during the move operation, the code block in the “catch” statement will be executed. This code block writes an error message to the console that includes the name of the VM and the error message. The error message is retrieved using the “$_.Exception.Message” syntax, which retrieves the exception message from the current object in the pipeline.

Code:

Import-Csv "C:\temp\VmList.csv" | %{
    $vm = Get-VM -Name $_.Name
    $folder = Get-Folder -Name "Folder_Name"

    try {
        Move-VM -VM $vm -Destination $folder -ErrorAction Stop
    }
    catch {
        Write-Host "Error moving VM $($vm.Name): $($_.Exception.Message)"
    }
}

The full script can be downloaded from my GitHub repository.

🔥Subscribe to the channel: https://bit.ly/3vY16CT🔥

🚨Read my blog: https://angrysysops.com/

👊Twitter: https://twitter.com/AngrySysOps
👊Facebook: https://www.facebook.com/AngrySysOps
👊My Podcast: https://bit.ly/39fFnxm
👊Mastodon: https://techhub.social/@AngryAdmin

🔥vExpert info: https://bit.ly/3vXGPOa

🛒 VMware EMEA store: https://imp.i263671.net/c/3505578/814646/11461

🛒 VMware US store: https://imp.i263671.net/c/3505578/814642/11461

🛒 VMware APAC store: https://imp.i263671.net/c/3505578/814645/11461

Please leave the comment