Advertisement
jmeg8r

Set-Scratch

Jan 30th, 2019
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ?# original source by vmnomad - https://community.spiceworks.com/scripts/show/3686-automating-configuration-of-a-scratch-location-for-esxi-with-powercli
  2. # modified by @vMiklm - miklm.com - 19 January 2018
  3. # very slight modification by @JamesCruce - astgl.com in using the $Cluster variable in the exported csv file for vmhosts that need to be rebooted $exportfilename =  "$Cluster-Servers_to_reboot.csv"
  4. # very slight modiviation by @JamesCruce - astgl.com in adding a Disonnect-VIServer command at the end of script
  5. # Example use:
  6. # PS> .\set-scratch.ps1 $vCenter $Cluster $Datastore $Folder
  7. # PS> .\set-scratch.ps1 vcenter.local clusteresx08 SharedNFS01 scratch/clusteresx08
  8.  
  9. #Collect the vCenter, Cluster, Scratch_datastore & Folder
  10. Param([String]$vCenter, [String]$Cluster, [String]$Datastore, [String]$Folder)
  11.  
  12. #Function to use multiple colors in one command
  13. function Write-Color([String[]]$Text, [ConsoleColor[]]$Color) {
  14.     for ($i = 0; $i -lt $Text.Length; $i++) {
  15.         Write-Host $Text[$i] -Foreground $Color[$i] -NoNewLine
  16.     }
  17.     Write-Host
  18. }
  19.  
  20. #defining array variables
  21. $vmhost_array =@()
  22. $dir = @()
  23. $reboot_servers = @()
  24.  
  25. #Validating input
  26. if (!$vCenter){
  27.   Write-Color  -Text "Please provide valid vCenter name using ","'-vCenter' ","option, exiting.." -Color Gray,Red,Gray
  28.   exit
  29. }
  30.  
  31. if (!$Cluster){
  32.   Write-Color  -Text "Please provide valid cluster name using ","'-Cluster' ","option, exiting.." -Color Gray,Red,Gray
  33.   exit
  34. }
  35.  
  36. if (!$Datastore){
  37.   Write-Color  -Text "Please provide valid Datastore name using ","'-Datastore' ","option, exiting.." -Color Gray,Red,Gray
  38.   exit
  39. }
  40.  
  41. if (!$Folder){
  42.   Write-Color  -Text "Please provide valid scratch folder name using ","'-Folder' ","option, exiting.." -Color Gray,Red,Gray
  43.   exit
  44. }
  45.  
  46. #Getting the path of the script
  47. $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
  48.  
  49. #Connecting to vCenter server
  50. Write-Color -Text "`nConnecting to ", $vCenter -Color Gray, Red
  51. Connect-VIserver $vCenter | Out-Null
  52.  
  53. #Validating connection to vCenter
  54. if(!$DefaultVIServers){
  55.   Write-Color -Text "`nConnection to ",$vCenter," failed, exiting.." -Color Yellow,Red,Yellow
  56.   exit
  57. } elseif($DefaultVIServer.name -ne $vCenter){
  58.     Write-Color -Text "Connected to wrong vCenter ", $DefaultVIServer.name ", exiting.." -Color Yellow,Red,Yellow
  59.   } else {
  60.   Write-Color -Text "Connection to vCenter ", $vCenter, " succeeded" -Color Green,Red,Green
  61.   Write-Host
  62. }
  63.  
  64. #Getting the list of the ESXi hosts in the $Cluster
  65. $vmhost_array = get-cluster -name $cluster | Get-VMhost
  66.  
  67. #Mounting datastore to be used as a scratch location
  68. New-PSDrive -Name "DS" -Root \ -PSProvider VimDatastore -Datastore (Get-VMHost -Name $vmhost_array[0] | Get-Datastore $datastore) | out-null
  69. Set-Location DS:\$folder
  70.  
  71. #Collect the list of the folders
  72. $dir = dir
  73.  
  74. #Check if the scratch folders exist for the ESXi hosts and create missing folders
  75. Foreach ($vmhost in $vmhost_array)
  76. {
  77. If ($dir.name -contains $vmhost.name){
  78.   Continue
  79.   }
  80. else{
  81.   Write-Color -Text "`n Creating scratch folder for ", $vmhost -Color Green,Red
  82.   mkdir $vmhost | out-null
  83.   }
  84. }
  85.  
  86. #Getting Scratch datastore [was getting UUID, didn't work, vCenter will convert it anyway -MM]
  87. $dstorep = Get-VMhost $vmhost | Get-Datastore $Datastore
  88.  
  89. #Check if the ESXi host is already configured with correct scratch location
  90. Foreach ($vmhost in $vmhost_array){
  91.   $row = '' | Select Server_Name
  92.   $configured_scratch = (Get-VMhost $vmhost | Get-AdvancedSetting -Name "ScratchConfig.ConfiguredScratchLocation").value
  93.   $current_scratch = (Get-VMhost $vmhost | Get-AdvancedSetting -Name "ScratchConfig.CurrentScratchLocation").value
  94.   $correct_scratch = "/vmfs/volumes/"+$dstorep+"/"+$folder+"/"+$vmhost
  95. # Write-Host "`n Correct scratch is" $correct_scratch
  96. # Write-Host "`n Configured Scratch on" $vmhost "is" $configured_scratch
  97. # Write-Host "`n Current Scratch on" $vmhost "is" $current_scratch
  98.   If (($configured_scratch -eq $correct_scratch) -and ($current_scratch -eq $correct_scratch)) {
  99.     Write-Color -Text "`n ESXi host ", $vmhost, " was already configured with the correct scratch location" -Color Green,Red,Green
  100.   } elseif($configured_scratch -eq $correct_scratch) {
  101.   Write-Color -Text "`n The ESXi host", $vmhost, " was already configured correctly, `n but it hasn't been restared after the configuration change" -Color Yellow,Red,Yellow
  102.   $row.Server_Name = $vmhost.Name
  103.   $reboot_servers += $row
  104.   } else {
  105.     Get-VMhost $vmhost | Get-AdvancedSetting -Name "ScratchConfig.ConfiguredScratchLocation" | Set-AdvancedSetting -Value "$correct_scratch" -Confirm:$false | out-null
  106.     Write-Host -Fore:Red "`n ESXi host" $vmhost "is configured with the correct scratch location"
  107.     $row.Server_Name = $vmhost.Name
  108.     $reboot_servers += $row
  109.   }
  110. }
  111.  
  112. #Provide output with the list of ESXi servers to be rebooted for the configration change to take effect
  113. Write-Host -Fore:Green "`n The configuration of the scratch location for ESXi servers in cluster" $cluster "is complete"
  114. Write-Host -Fore:Green "`n The following ESXi hosts have to be rebooted for the configuration change to take effect:"
  115. foreach ( $server in $reboot_servers ) {
  116. Write-Host -Fore:Red `n $server.Server_Name
  117. }
  118.  
  119. #Exporting the list of ESXi hosts to be rebooted
  120. $exportfilename =  "$Cluster-Servers_to_reboot.csv"
  121. $exportfilepath = Join-Path -Path $scriptPath -ChildPath $exportFileName
  122. $reboot_servers | Export-Csv -Path $exportFilePath -NoTypeInformation -Force
  123. Write-Host "`n This list of these servers has also been exported to" $exportfilepath
  124.  
  125. #Change location back to original
  126. Set-Location $scriptPath
  127.  
  128. Disconnect-VIServer $VCenter -Force -Confirm:$False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement