My approach to scripting is to make things a reusable as possible. Hence, I always start things off from with the following global config file and script template.
CONFIG.ps1
$hosts = @("vcenter.local")
$VBRServer = "veeam.local"
$OUTPUTDIR = "c:\Shares\LOGS"
#$CREDS = Get-Credential
#####$CREDS | Export-Clixml c:\Shares\Scripts\admin.creds
$CREDS = Import-Clixml -Path c:\Shares\Scripts\admin.creds
$SMTPServer = "mail.local"
$SMTPPort = 25
$SMTPFrom = "tasker@mail.local"
$SMTPTo = "root@mail.local"
$VMExcludelist = @("vm1.local", "vm2.local", "VMware vCenter Server")
$CLUSTERExcludeList = @()
$Post = $True
Base-Template.ps1
##
## Written By Brian Vogel bfvogel@gmail.com
## MIT License
##
##
param (
[Parameter(Mandatory=$true)][string]$CHANGEME,
[switch]$Script = $false,
[switch]$Automatic = $false
)
### Script Config Vars
##
$ScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
try {
. ("$ScriptDirectory\Config.ps1")
}
catch {
Write-Host "Error while loading supporting PowerShell Scripts"
exit 1
}
## Module Load
if (!(Get-Module -Name VMware.VimAutomation.Core) -and (Get-Module -ListAvailable -Name VMware.VimAutomation.Core)) {
Write-Output "loading the VMware COre Module..."
if (!(Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
# Error out if loading fails
Write-Error "`nERROR: Cannot load the VMware Module. Is the PowerCLI installed?"
}
$Loaded = $True
}
elseif (!(Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -and !(Get-Module -Name VMware.VimAutomation.Core) -and ($Loaded -ne $True)) {
Write-Output "loading the VMware Core Snapin..."
if (!(Add-PSSnapin -PassThru VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
# Error out if loading fails
Write-Error "`nERROR: Cannot load the VMware Snapin or Module. Is the PowerCLI installed?"
Write-Host "Try: 'Install-Module VMware.PowerCLI'"
}
}
Connect-VIServer $hosts -Credential $CREDS | out-null
########START Script########################
########END Script##########################
Disconnect-VIServer * -confirm:$false