This script does an export of all the vdswitches on vcenter servers in the $gcvcenter variable.
It saves the export in $workdir after 30 days the oldest files are deleted.
The script runs as a scheduled task.
The user have logon as a batch job (secpol.msc)
The user has read-only access to the vcenter servers.
The credentials for the user are stored in an vicredentialstore file on the same path as the powercli script.
<# This script makes backup of every vdswitch on an vcenter server. Niklas Karlsson 2024-06-12 #> # Set powercli settings Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false -ParticipateInCEIP $true # Set workingfolders $workdir = "C:\VdSwitch_backup\" $creddir = "C:\scripts\" # Set powercliconfig to Multiple Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope Session -Confirm:$false # Create new credential file # New-VICredentialStoreItem -host VCENTER -user USERNAME -password PASSWORD -file "$creddir\vcenter_credfile3.xml" # Get credentials from file. $cred = Get-VICredentialStoreItem -file "$creddir\vcenter_credfile3.xml" # Vcenter servers $gcvcenter = "vcenter1", "vcenter2", "vcenter3" # Connect to vCenter Server Appliance Connect-VIServer $gcvcenter -User $cred.User -password $cred.password $gcvdswitchs = get-VdSwitch # Date $date = Get-Date -Format "yyyyMMdd-HHmm" # Lopp through every vdswitch and make an export. foreach ($gcvdswitch in $gcvdswitchs) { get-VdSwitch $gcvdswitch| Export-VDSwitch -Description "Backup" -Destination "${workdir}${gcvdswitch}_${date}.zip" -Force } # Disconnects från Vcenter Disconnect-viserver $gcvcenter -Confirm:$false # Remove files older than 30 days. Get-ChildItem -Path "$workdir" | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item # Set powercliconfig back to Single Set-PowerCLIConfiguration -DefaultVIServerMode single -Scope Session -Confirm:$false