VMware Cloud Community
drwoodberry
Contributor
Contributor

Creating Datastores

I am automating the creation of datastores on ESXi hosts. When the hosts are in a datacenter or cluster, if I add the datastore to one host, it adds to all the hosts which is awesome. The issue I am having is when I am trying to attach shared storage to stand alone hosts. You may ask why I am doing this, but in this scenario I am dealing with, the hosts are standalone because a virtual center server is not available, so i am not adding the hosts into a cluster at this time.

I can get the script to attach the storage to the first host with no issues. I disconnect from that host, and I connect to the second host. When it trys to create the datastores, it says the disk is either not found or in use. Well I connect just this host to a virtual center to trouble shoot, and sure enough the datastores are showing up. Any ideas why it is throwing the error even when it is doing it? I have the host informaiton stored in a CSV file which I parse line by line to configure the host. in the loop, I attach the datastore. If I remove the datastore part, and put it outside the loop, it only attaches to the last host it was connect to. Below is my function that is performing these operations..I think most of the variables are explanitory.

Thanks in advance:

foreach ($line in (Import-Csv $filepath)) 
	{
   		$currentHost = $line.ESX_IP
		$hostname = $line.Hostname
    	$IP = $line.iSCSI_IP
    	$Subnet = $line.iSCSI_Subnet
        $ESXpasswd = $null
		connect-viserver -server $currenthost -user root -password $null
		#set hostname, domain name and dns address
		Get-VmHost -Name $currentHost | Get-VmHostNetwork | Set-VmHostNetwork  -DomainName $Domain -Hostname $hostname -DnsAddress $DnsServer -SearchDomain $DnsServer
		
		#function call to set up vswitch
		$null = vSwitch $capset $capsetnetwork $currentHost 
		if ($capsetnetwork -eq "SIPR")
		{
			$null = enableISCSI $currentHost $NetAppIp $IP $Subnet
			if ($ExistingVirtualCenter -eq "No") 
			{
				Write-Host $currentHost
				Get-VMHostStorage $currentHost -RescanAllHba -RescanVmfs
				$dsBaseName = "ds"
				$lunpaths = Get-ScsiLun | where { $_.Vendor -match "NETAPP"} | Select -First 3
				1..3 | %{ New-Datastore -VMHost $currenthost -Vmfs -Path $lunpaths[$_ - 1].CanonicalName -Name ($dsBaseName + $_) }
			}
		}
		else
		{
			$nic = Get-VirtualSwitch -VMHost $currentHost -Name "vSwitch0"
			Set-VirtualSwitch -VirtualSwitch $nic -Nic vmnic0,vmnic1 
		}
		Set-VMHostAccount -UserAccount root -password $EsxiPassword
    	Disconnect-VIServer -Server * -Force -Confirm:$false

0 Kudos
0 Replies