PoshCode Archive  Artifact [6d364afe73]

Artifact 6d364afe7352debf550ef64c0acb38ce31fecd2d12d90ebb078f4c12860f9db8:

  • File Scratchfolder-creation.ps1 — part of check-in [77606850d8] at 2018-06-10 14:10:21 on branch trunk — 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. (user: Leon Scheltema size: 1511)

# 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: Leon Scheltema
# license: CC0
# x-poshcode-id: 6205
# x-derived-from-id: 6206
# x-archived: 2016-05-19T09:19:56
# x-published: 2016-02-08T18:29: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
}