Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Sync_Credentials_between_Documents #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions Automation/PowerShell/Sync_Credentials_between_Documents
Original file line number Diff line number Diff line change
@@ -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
}
}