From ae9b2bc807b08119b5cc49aa49bfad13ece35ee3 Mon Sep 17 00:00:00 2001 From: GamstutzCH <71142640+GamstutzCH@users.noreply.github.com> Date: Wed, 13 Sep 2023 14:52:55 +0200 Subject: [PATCH] Create Sync_Credentials_between_Documents Sample PowerShell script to sync credentials between two or more documents based on their change date --- .../Sync_Credentials_between_Documents | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Automation/PowerShell/Sync_Credentials_between_Documents diff --git a/Automation/PowerShell/Sync_Credentials_between_Documents b/Automation/PowerShell/Sync_Credentials_between_Documents new file mode 100644 index 0000000..acaf5f7 --- /dev/null +++ b/Automation/PowerShell/Sync_Credentials_between_Documents @@ -0,0 +1,95 @@ +# Import the RoyalDocument module +Import-Module royaldocument.powershell + +# Set the Configuration for the Documents store +## Documents passowrd - this is very unsecure do not deploy in PROD! +$Password = ConvertTo-SecureString "PASSWORD" -AsPlainText -Force +##I Have no idea what i need this 4 +$Username = "Automated_Task" +##Initialize Store (this is needed so that the documents can be opened) +$RoyalStore = New-RoyalStore -UserName $Username + +# Specify the base directory where GUID-named subdirectories are located +$baseDirectory = "C:\ProgramData\RoyalServer\DocumentStore\Documents" + +# Get a list of subdirectories with GUID names +$subdirectories = Get-ChildItem -Path $baseDirectory -Directory + +# Initialize an empty array to store the custom objects +## Iam Not a Powershell crack GPT just told me to include this line +$credentialInfoArray = @() +$pending_changes = @() + +# Loop through each subdirectory +foreach ($subdirectory in $subdirectories) { + # Construct the path to the default.rtsz file in the current subdirectory + $documentPath = Join-Path -Path $subdirectory.FullName -ChildPath "default.rtsz" + + # Check if the default.rtsz file exists in the current subdirectory + if (Test-Path -Path $documentPath -PathType Leaf) { + # Open the Royal Document + $RoyalDocument = Open-RoyalDocument -FileName $documentPath -Store $RoyalStore -Password $Password + + # Perform your operations on the document here + $credentials = Get-RoyalObject -Store $RoyalStore -Type RoyalCredential + + foreach ($credential in $credentials) { + #Not shure why i made a extra step to convert the value you could just as well define it later + $securePW = ConvertTo-SecureString -String $credential.Password -AsPlainText -Force + + #Array of Values i find interesting. Check Documentation if you want to use diffrent values + $credentialInfo = [PSCustomObject]@{ + Modified = $credential.Modified + Name = $credential.Name + GUID = $credential.ID + Path = $documentPath + Password = $securePW + } + #I dont know what this line does + $credentialInfoArray += $credentialInfo + } + + # Save the changes to the Royal TS document (this line was for testing and is not used) + #Out-RoyalDocument -Document $RoyalDocument -FileName $documentPath + + # Close the document + Close-RoyalDocument -Document $RoyalDocument + } +} + +# Iterate through the array and compare items with the same "Name" +$uniqueNames = $credentialInfoArray.Name | Sort-Object -Unique +foreach ($name in $uniqueNames) { + # matchi items if name (Display) is the same + $matchingItems = $credentialInfoArray | Where-Object { $_.Name -eq $name } + # upon hit write item in new array + $latestItem = $matchingItems | Sort-Object -Property Modified -Descending | Select-Object -First 1 + $pending_changes += $latestItem +} + +# Loop through all documents again and compare every object type credential to the items in the $pending_changes array +foreach ($subdirectory in $subdirectories) { + #this would be nicer if it was a function since i run the same commands multiple times but i dont know how + $documentPath = Join-Path -Path $subdirectory.FullName -ChildPath "default.rtsz" + if (Test-Path -Path $documentPath -PathType Leaf) { + $RoyalDocument = Open-RoyalDocument -FileName $documentPath -Store $RoyalStore -Password $Password + $credentials = Get-RoyalObject -Store $RoyalStore -Type RoyalCredential + + # Check if credential name is the same with item in pending array and check if IDs match + foreach ($credential in $credentials) { + $matchingItem = $pending_changes | Where-Object { $_.GUID -ne $credential.ID} + if ($matchingItem -ne $null) { + # if every thing is the way we want overwrite password with more recent value (more recent = more better) + $credential.Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($matchingItem.Password)) + Write-Host "Password updated successfully for object $($credential.Name)." + Write-Host "Updated for for object $($documentPath)." + } + } + + # Save the changes to the Royal TS document + Out-RoyalDocument -Document $RoyalDocument -FileName $documentPath + + # Close the document + Close-RoyalDocument -Document $RoyalDocument + } +}