I have a script I'm trying to write, that for some reason I thought I had working before. I basically am asking a user to connect to a vCenter, find a datastore they want to mount, list the available hosts, select a host and then mount it to that host. However when I use this code:
New-Datastore -VMHost $esxhost -Nfs -Name $datastore.Name -Path $datastore.RemotePath -NfsHost $datastore.RemoteHost
It fails. $esxhost is the host selected by the user and $datastore is the datastore previously selected by the user as well. Is this possible or do we have to hard code in the path and nfshost info?
The $datastore variable holds whatever the user has typed, and there are not Name, RemotePath nor RemoteHost properties.
Instead of using the Format-Table why don't you try using the Out-GridView.
And once the user has selected the datastore and the ESXi node, get the PowerCLI objects for both.
That way the properties are available for your New-Datastore cmdlet.
Something like this
$datastore = Get-Datastore | where {$_.Type -eq "nfs" -and $_.Accessible -eq "true"} |
Select @{N="Cluster";E={$cluster.Name}},
Name,CapacityGB,FreespaceGB,
@{N='UsedSpace';E={$_.FreeSpaceGB/$_.CapacityGB*100}} |
Out-GridView -Title 'Select a datastore' -OutputMode Single
$esxHost = Get-VMHost | where {$_.state -ne "NotResponding"} | sort Name |
Select Name,ConnectionState,PowerState |
Out-GridView -Title "Enter the host to mount the datastore to" -OutputMode Single
$ds = Get-Datastore -Name $datastore.Name
$esx = Get-VMHost -Name $esxHost.Name
New-Datastore -VMHost $esx -Nfs -Name $ds.Name -Path $ds.RemotePath -NfsHost $ds.RemoteHost
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
When it fails, what message are you getting?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
New-Datastore : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that
is not null or empty, and then try the command again.
At C:\Users\username\Desktop\GenScripts\GenMountNFS.ps1:30 char:43
+ New-Datastore -VMHost $esxhost -Nfs -Name $datastore.Name -Path $data ...
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-Datastore], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.NewDa
tastore
Looks like the $datastore variable is empty.
Can you perhaps share the complete script you are using?
Or at least the part that sets up the $datastore variable.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Get-Datastore | where {$_.Type -eq "nfs" -and $_.Accessible -eq "true"} | Select @{N="Cluster";E={$cluster.Name}},Name,CapacityGB,FreespaceGB,@{N='UsedSpace';E={$_.FreeSpaceGB/$_.CapacityGB*100}} | Format-Table
$datastore = Read-Host "Enter the datastore to mount"
Get-VMHost | where {$_.state -ne "NotResponding"} | sort Name | Select Name,ConnectionState,PowerState | Format-Table
$esxhost = Read-Host "Enter the host to mount the datastore to"
The $datastore variable holds whatever the user has typed, and there are not Name, RemotePath nor RemoteHost properties.
Instead of using the Format-Table why don't you try using the Out-GridView.
And once the user has selected the datastore and the ESXi node, get the PowerCLI objects for both.
That way the properties are available for your New-Datastore cmdlet.
Something like this
$datastore = Get-Datastore | where {$_.Type -eq "nfs" -and $_.Accessible -eq "true"} |
Select @{N="Cluster";E={$cluster.Name}},
Name,CapacityGB,FreespaceGB,
@{N='UsedSpace';E={$_.FreeSpaceGB/$_.CapacityGB*100}} |
Out-GridView -Title 'Select a datastore' -OutputMode Single
$esxHost = Get-VMHost | where {$_.state -ne "NotResponding"} | sort Name |
Select Name,ConnectionState,PowerState |
Out-GridView -Title "Enter the host to mount the datastore to" -OutputMode Single
$ds = Get-Datastore -Name $datastore.Name
$esx = Get-VMHost -Name $esxHost.Name
New-Datastore -VMHost $esx -Nfs -Name $ds.Name -Path $ds.RemotePath -NfsHost $ds.RemoteHost
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Awesome. Thanks LucD. One last thing. I'm also writing one to do this in bulk which is fine, however at the end I want to do a write-host where it shows the datastore was mounted on the hosts. I currently have this:
$datastore = Get-Datastore | where {$_.Type -eq "nfs" -and $_.Accessible -eq "true"} | Select @{N="Cluster";E={$cluster.Name}},Name,CapacityGB,FreespaceGB,@{N='UsedSpace';E={$_.FreeSpaceGB/$_.CapacityGB*100}} | Out-GridView -Title 'Select a datastore' -OutputMode Single
$selectedhosts = Get-VMHost | where {$_.state -ne "NotResponding"} | sort Name | Select Name,ConnectionState,PowerState | Out-GridView -Title "Select the hosts to mount the datastore to" -OutputMode Multiple
$ds = Get-Datastore -Name $datastore.Name
$esx = Get-VMHost -Name $selectedhosts.Name
if ($selectedhosts) {
foreach ($esx in $selectedhosts) {
New-Datastore -VMHost $esx.Name -Nfs -Name $ds.Name -Path $ds.RemotePath -NfsHost $ds.RemoteHost #Mounts NFS datastore to the selected hosts
Write-Host "Datastore $ds mounted on hosts $esx"
}
}
But when it puts the message it shows something like Datastore mydatastore mounted on hosts @{Name=hostname; ConnectionState=Maintenance; PowerState=PoweredOn}
When I run it on the script to just mount to one host it shows 'Datastore mydatastore mounted on host hostname'.
What change needs to be made so it shows that in the bulk mount script?
That's because the the $ds and $esx variables hold the full objects.
And the PS output engine tries to display all the properties in that format.
You can specify explicitly which property.
Something like this
Write-Host "Datastore $($ds.Name) mounted on hosts $($esx.Name)"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the tip LucD. Works like a charm.