VijayKumarMyada
Enthusiast
Enthusiast

NFS mount point script

Jump to solution

am trying below script to add nfs mount point to the esxi hosts which am mentioniong in ds.csv file

instead in csv am looking to ask on which host mount point to be added.

$Dspath=c:\temp\ds.csv

$Dsconf= Import-csv $Dspath

write-host "enter esxi host name"

foreach($ds in $Dsconf)

{

New-Datastore -Nfs -Name $ds.name -path $ds.path -nfshost $ds.nfshost

}

pastedImage_0.png

Tags (2)
30 Replies
LucD
Leadership
Leadership

Which log file are you talking about?

Or just sending a message to a file, that will be the log?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
VijayKumarMyada
Enthusiast
Enthusiast

yeah,just sending a message to the file and that will be the log.

0 Kudos
LucD
Leadership
Leadership

Try something like this

$Dspath = "c:\temp\ds1.csv"

$logFile = 'C:\Temp\log.txt'


$Dsconf = Import-csv $Dspath


Get-Cluster -Name "Staging" | Get-VMHost |

ForEach-Object -Process {

   foreach ($ds in $Dsconf)

   {

   New-Datastore -VMHost $_ -Nfs -Name $ds.name -path $ds.path -nfshost $ds.nfshost

   Write-Output "$(Get-Date) - Created NFS datastore $($ds.Name) on $($_.Name). NFSHost: $($ds.nfshost) Export: $($ds.path)" |

   Out-File -FilePath $logFile -Append

   }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

VijayKumarMyada
Enthusiast
Enthusiast

log is writing well, when i enter host name which is not pinging it does not have the log info.

what needs to be add to capture the info, or user test-connection to check the alive status if it is pinging proceed further and not write in log it is not rechable.

0 Kudos
LucD
Leadership
Leadership

You mean something like this?

$Dspath = "c:\temp\ds1.csv"

$logFile = 'C:\Temp\log.txt'


$Dsconf = Import-csv $Dspath


Get-Cluster -Name "Staging" | Get-VMHost |

ForEach-Object -Process {

   if('DisConnected','NotResponding' -contains $_.VMhostState){

   Write-Output "$(Get-Date) - $($_.Name) not reachable" |

   Out-File -FilePath $logFile -Append

   }

   else{

   foreach ($ds in $Dsconf)

   {

   New-Datastore -VMHost $_ -Nfs -Name $ds.name -path $ds.path -nfshost $ds.nfshost

   Write-Output "$(Get-Date) - Created NFS datastore $($ds.Name) on $($_.Name). NFSHost: $($ds.nfshost) Export: $($ds.path)" |

   Out-File -FilePath $logFile -Append

   }

   }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
VijayKumarMyada
Enthusiast
Enthusiast

yeah , that is for cluster it is working fine, looking for individual host when run the script and enter the host name there it has to check the connection is live/pinging and proceed further, else write an error not reachable.

Dspath = "c:\temp\ds1.csv"

$logFile = 'C:\Temp\log.txt'

$Dsconf = Import-csv $Dspath

$esxName = Read-Host "enter esxi host name"

$Status=test-connection $esxname 

IF($Stauts is not rechable)

{

Write-Output "$(Get-Date) - $($_.Name) not reachable" |

   Out-File -FilePath $logFile -Append

}

Else{

foreach ($ds in $Dsconf)

{

   New-Datastore -VMHost $esxName -Nfs -Name $ds.name -path $ds.path -nfshost $ds.nfshost

   Write-Output "$(Get-Date) - Created NFS datastore $($ds.Name) on $($_.Name). NFSHost: $($ds.nfshost) Export: $($ds.path) to:$esxname" | Out-File -FilePath c:\temp\ds1.txt -Append

}

}

0 Kudos
LucD
Leadership
Leadership

That is mostly similar, just test for the connection of that one ESXi node.

$Dspath = "c:\temp\ds1.csv"

$logFile = 'C:\Temp\log.txt'


$Dsconf = Import-csv $Dspath

$esxName = Read-Host "enter esxi host name"


$esx = Get-VMHost -Name $esxName


IF ('DisConnected', 'NotResponding' -contains $esx.VMhostState) {

   Write-Output "$(Get-Date) - $($_.Name) not reachable" |

   Out-File -FilePath $logFile -Append

}

Else {

   foreach ($ds in $Dsconf) {

   New-Datastore -VMHost $esxName -Nfs -Name $ds.name -path $ds.path -nfshost $ds.nfshost

   Write-Output "$(Get-Date) - Created NFS datastore $($ds.Name) on $($_.Name). NFSHost: $($ds.nfshost) Export: $($ds.path) to:$esxname" | Out-File -FilePath c:\temp\ds1.txt -Append

   }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
VijayKumarMyada
Enthusiast
Enthusiast

i tried to put the host in disconnected state and attempted to execute the script, in the log file i see message

07/10/2019 13:48:44 - Created NFS datastore SSD_01 on . NFSHost: 10.111.50.203 Export: /SSD_01 to:esxi01, instead esxi01 not reachable.

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

I can't see the complete script you ran.
Can you check the content of $esx after you ran the script?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
VijayKumarMyada
Enthusiast
Enthusiast

Here is the output

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

Try replacing $esx.VMHostState with $esx.ConnectionState

If that doesn't work, try $esx.State


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos