From ce06c5372bd93327076a538f46eac7b9145ccc27 Mon Sep 17 00:00:00 2001 From: Kamen Nikolov Date: Tue, 21 Nov 2023 16:52:47 +0200 Subject: [PATCH] Added git sync script and fixed an issue in the remove old snapshots script in the PowerActions folder --- .../Example - RemoveOldClusterSnapshots.ps1 | 2 +- PowerActions/Git Sync.ps1 | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 PowerActions/Git Sync.ps1 diff --git a/PowerActions/Example - RemoveOldClusterSnapshots.ps1 b/PowerActions/Example - RemoveOldClusterSnapshots.ps1 index 698a15d..66bf771 100644 --- a/PowerActions/Example - RemoveOldClusterSnapshots.ps1 +++ b/PowerActions/Example - RemoveOldClusterSnapshots.ps1 @@ -11,7 +11,7 @@ $vms = Get-VM -Location $cluster foreach ($vm in $vms) { $snaphostsToBeRemoved = Get-Snapshot -VM $vm | where {$_.Created -lt $date} 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 } } \ No newline at end of file diff --git a/PowerActions/Git Sync.ps1 b/PowerActions/Git Sync.ps1 new file mode 100644 index 0000000..6015448 --- /dev/null +++ b/PowerActions/Git Sync.ps1 @@ -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 + } +} \ No newline at end of file