# encoding: ascii
# api: powershell
# title: Scratchfolder creation
# description: Script creates unique scratch folders on the provided datastore for all ESXi hosts in csv file and sets the advanced host settings to correspond to this location.
# version: 0.1
# type: script
# author: Kimberly Anderso
# license: CC0
# x-poshcode-id: 6237
# x-derived-from-id: 6238
# x-archived: 2016-08-26T03:22:57
# x-published: 2016-02-27T05:48:00
#
#
# Script to create scratch location directory per host
# By Leon Scheltema AVANCE ICT Groep Nederland

# Begin variables
$Datastore = ""
# End variables

#connect to all host from csv sequentially

Import-CSV file.csv | ForEach-Object {
$hostname = $_.vmhost

connect-viserver $hostname -user root -password ""

#Mount a datastore read/write as a PSDrive
New-PSDrive -Name "mounteddatastore" -Root \ -PSProvider VimDatastore -Datastore (Get-Datastore $Datastore)

#Access the new PSDrive
Set-Location mounteddatastore:

#Create a uniquely-named directory for this ESXi host
New-Item ".locker-$hostname" -ItemType directory

#Unmount a datastore read/write as a PSDrive
CD G:
Remove-PSDrive Mounteddatastore

#Change the Scratch location by specifying the full path to the directory created earlier
Set-VMHostAdvancedConfiguration -Name "ScratchConfig.ConfiguredScratchLocation" -Value "/vmfs/volumes/$Datastore/.locker-$hostname"

# Disconnect from vCenter server
Disconnect-VIServer -server $hostname -Force -Confirm:$false
}
