Powershell: Add NFS Datastore to Multiple ESX Hosts

Adding a datastore to an esx host isn’t to time consuming or difficult but to save a little bit of time and avoid the repetitiveness, here’s a script to do it for you. This script will mount an NFS datastore. Of course, you can change the protocol by using a different switch like -cifs.

$esx = (Get-Content esx_servers.txt)
$Host_Name = (Read-Host "Enter the server (ex:storage-server-san:")
$Path = (Read-Host "Enter the path (ex:/vol/vol/ds_name):")
$Name = (Read-Host "Enter the name of the datastore:")

foreach($server in $esx){
Get-VMHost $server | New-Datastore -Nfs -NfsHost $Host_Name -Path $Path -Name $Name
}

The script asks for the following information:
1. Storage server
2. Path to the storage location
3. Datastore Name

Note: The script will only mount the SAME datastore on each esx server you specify. You’ll need to create a text file called “esx_servers.txt” in the same location as the script and enter the esx server names in it.

UPDATE: I’ve had to modify this script to add multiple datastores to multiple esx hosts. Here is the updated code:

$servers = @()
$paths = @()
$datastores = @()

$answer = "y"

while($answer -eq "y"){
$servers += Read-Host "Enter the server (ex:usadc-nas05a-san)"
$paths += Read-Host "Enter the path (ex:/vol/vol/ds_name)"
$datastores += Read-Host "Datastore name ?"
$answer = Read-Host "Enter another NFS mount?"}

$Error.Clear()

Get-Content servers.txt | %{
$esx = Get-VMHost -Name $_
$countNFS = $servers.Count
if($countNFS -ne 0){
0..($countNFS - 1) | %{
$esx | New-Datastore -Nfs -NfsHost $servers[$_] -Path $paths[$_] -Name $datastores[$_] | Out-Null
if($Error.count -gt 0){
$Error | out-file ('c:\powershell scripts\logs\' + $esx + '.txt') -append
$Error.Clear()
}
}
}
}

Powershell: Mount Datastore ISO to Multiple VM’s

I had to install a piece of software on several vm’s a few days ago and got somewhat bored of the same repetitive task of mounting the ISO to all of the vm’s cd-rom drive. So, I wrote a script that will automatically mount the ISO that I specify and connect the cd-rom drive.

$VMs = (Get-Content servers.txt)
$ds = (Read-Host "Type the datastore name:")
$ISO_Path = (Read-Host "Type the path to the iso file:")

foreach($vm in $VMs){
	Get-CDDrive $vm | Set-CDDrive -StartConnected:$true -Connected:$true -IsoPath [$ds]  $ISO_Path -Confirm:$false
}

What this script does is prompt you for the datastore location that your ISO’s are located in. For example, if your ISO is located in the datastore “ISOs” you would type that. The script then prompts you for the full path to the iso. This is what your input should look like

1. Type the datastore name: vm_isos
2. Type the path to the iso file: folder/folder1/folder2/file.iso

On the flip side if you want to disconnect the cd-rom from multiple vm’s, you just need to modify the script a bit.

$VMs = (Get-Content servers.txt)

foreach($vm in $VMs){
	Get-CDDrive $vm | Set-CDDrive -NoMedia -StartConnected:$false 
-Connected:$false -Confirm:$false
}

Note: This will actually remove the mounted iso (-NoMedia) but it will also disconnect the cd-rom and set the startconnected switch to false.

Powershell: Shutdown VM and Delete From Disk

This is a fairly simple script but I thought I’d share. I needed to delete a group of about 6 virtual machines this morning. I could easily do this through the vSphere GUI but I figured I’d write a quick script so I could use it for future deletions.


$VMs = (Get-Content servers.txt)

foreach($vm in $VMs){
$active = Get-VM $vm
if($active.PowerState -eq "PoweredOn"){
Stop-VM -VM $vm -Confirm:$false
Start-Sleep -Seconds 10
Remove-VM -VM $vm -DeleteFromDisk -Confirm:$false -RunAsync}
else
{Remove-VM -VM $vm -DeleteFromDisk -Confirm:$false -RunAsync}
}

Updated Script:

$VMs = (Get-Content servers.txt)
$vmObj = Get-vm $vms

foreach($active in $vmObj){
if($active.PowerState -eq "PoweredOn"){
Stop-VM -VM $active -Confirm:$false -RunAsync | Out-Null} 
}
Start-Sleep -Seconds 7

foreach($delete in $vmObj){
Remove-VM -VM $delete -DeleteFromDisk -Confirm:$false -RunAsync | Out-Null}

This script will run a lot quicker and the code is a bit cleaner.

Powershell: List VM Name, Hard Drive Name and Datastore Name

I recently had a request for a script that listed virtual machines hard drives and datastore locations. This is a fairly quick and easy script. The only problem is the formatting. Since some VM’s have more than one HD the formatting is a little odd. Instead of use the Export-CSV cmdlet I decided to use the Out-File cmdlet and save the results to a text file. It looks a bit cleaner to me.

$VMs = Get-vm

foreach ($vm in $VMs){
$ds = Get-HardDisk -VM $vm | Select-Object name,filename
$vm.Name,$ds | Format-Table | Out-File -Append -FilePath "c:\filename.txt}

Here’s a preview of the output: