Automation

 View Only
  • 1.  Add Multiple NFS Datastores to Multiple ESX Hosts

    Posted Feb 28, 2011 08:14 PM

    I'm trying to write a script that will mount multiple NFS exports to multiple ESX hosts. Below is the script I have been using, it will add one NFS export to a list of esx hosts.

    $esx = (Get-Content servers.txt)

    Do{
    $Host_Name = (Read-Host "Enter the server (ex:storage-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){
    $Error.Clear()
    Get-VMHost $server | New-Datastore -Nfs -NfsHost $Host_Name -Path $Path -Name $Name | Out-Null
    if($error.count -gt 0){$error | out-file ('c:\powershell scripts\logs\' + $server + '.txt')}
    }

    $add_more = Read-Host "Do you want to mount another datastore?"
    }
    While($add_more -eq "yes" -or $add_more -eq "y")

    This script actually works OK but I can't just kick it off and walk away. If I have 10 datastores to add, I've got to wait for the current one to finish and then type in the info for the second one, etc.

    I was hoping to just type all of the information upfront and kick the script off and let it go. Here's what I've added, can you see where I'm going with this?

    $esx = (Get-Content servers.txt)

    $num_mounts = (Read-Host "Do you have multiple mounts")
    if($num_mounts -eq "yes" -or $num_mounts -eq "y")
    {
      $mounts = @(Read-Host "How many different mounts do you have")

    #prompt me for all of the different mounts right up front and then run through the actual task of mounting them....
    }

    Any ideas?



  • 2.  RE: Add Multiple NFS Datastores to Multiple ESX Hosts
    Best Answer

    Posted Feb 28, 2011 09:37 PM

    Try something like this

    $servers = @()
    $paths = @()
    $datastores = @()
    
    $answer = Read-Host "Enter another NFS mount ?" 
    while
    ($mount -eq "y"){     $servers += Read-Host "Enter the NFS host"
        $paths += Read-Host "Enter the path"
       
    $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) | %{             New-Datastore -Nfs -NfsHost $servers[$_] -Path $paths[$_] -Name $datastores[$_] | Out-Null
                if($Error.count -gt 0){                 $Error | out-file ('c:\powershell scripts\logs\' + $server + '.txt')                 $Error.Clear()             }         }     } }

    The script reads all the data into arrays.

    Then it loops through all ESX hosts from the servers.txt file, and for each host it will create all the NFS datastores.