Added git sync script and fixed an issue in the

remove old snapshots script in the PowerActions folder
This commit is contained in:
Kamen Nikolov
2023-11-21 16:52:47 +02:00
parent 20871aeffa
commit ce06c5372b
2 changed files with 45 additions and 1 deletions

View File

@@ -11,7 +11,7 @@ $vms = Get-VM -Location $cluster
foreach ($vm in $vms) { foreach ($vm in $vms) {
$snaphostsToBeRemoved = Get-Snapshot -VM $vm | where {$_.Created -lt $date} $snaphostsToBeRemoved = Get-Snapshot -VM $vm | where {$_.Created -lt $date}
if ($null -ne $snaphostsToBeRemoved) { if ($null -ne $snaphostsToBeRemoved) {
Write-Host "Removing snapshots: " + $snaphostsToBeRemoved + " of VM: " + $vm Write-Host "Removing snapshots: '$snaphostsToBeRemoved' of VM: '$vm'"
Remove-Snapshot $snaphostsToBeRemoved -Confirm:$false Remove-Snapshot $snaphostsToBeRemoved -Confirm:$false
} }
} }

44
PowerActions/Git Sync.ps1 Normal file
View File

@@ -0,0 +1,44 @@
#Intall git
tdnf install -y git
#Clone the repo
git clone -n --depth=1 --filter=tree:0 https://github.com/vmware/PowerCLI-Example-Scripts.git
cd PowerCLI-Example-Scripts
git sparse-checkout set --no-cone PowerActions
git checkout
cd PowerActions
#Select the content library in which you want to store the scirpts from the repo
$contentLibraryName = 'Power Actions'
$contentLibrary = Get-ContentLibrary $contentLibraryName
#Get all the files that we have cloned from the repo
$files = Get-ChildItem -Path . -File
foreach ($file in $files) {
$name = $file.BaseName
#Check if the item for this file already exists in the content library
$item = Get-ContentLibraryItem -Name $name -ContentLibrary $contentLibrary -ErrorAction SilentlyContinue
if ($item) {
#If the item exists, check if it is up to date
#Create a folder to store the current content library item
if (-not (Test-Path -Path ./cl_versions -PathType Container))
{
New-Item -Path ./cl_versions -ItemType Directory
}
#Download the item from the content library
$clFile = Export-ContentLibraryItem -ContentLibraryItem $item -Destination ((Get-Location).Path + "/cl_version") -Force
#Compare if it's the same as the file we have downloaded from the repo
$compResult = Compare-Object -ReferenceObject (Get-Content $file.FullName) -DifferenceObject (Get-Content ($clFile.FullName+"/"+$file.Name))
if ($compResult) {
#If the item is not up to date, update it
Write-Host "Updating $name"
Set-ContentLibraryItem -ContentLibraryItem $item -Files $file.FullName
} else {
Write-Host "$name is up to date"
}
} else {
#If the item does not exist, create it
New-ContentLibraryItem -Name $name -Files $file.FullName -ContentLibrary $contentLibrary
}
}