-
Notifications
You must be signed in to change notification settings - Fork 75
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 Automated AD User Password Change.ps1 #84
Open
GamstutzCH
wants to merge
1
commit into
royalapplications:master
Choose a base branch
from
GamstutzCH:patch-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
Automation/PowerShell/Automated AD User Password Change.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<# | ||
Description: | ||
This PowerShell script generates a random password for Active Directory (AD) users stored in a Royal TS document and sets the password using the Set-ADAccountPassword cmdlet. The script uses the royaldocument.powershell and ActiveDirectory modules to access the Royal TS document and AD, respectively. | ||
|
||
The script performs the following tasks: | ||
- Imports the ActiveDirectory and royaldocument.powershell modules | ||
- Defines the IP address of a domain controller | ||
- Defines a function to generate a random 20-character string | ||
- Sets the configuration for the Documents store using the New-RoyalStore cmdlet from the royaldocument.powershell module | ||
- Specifies the base directory where GUID-named subdirectories are located | ||
- Gets a list of subdirectories with GUID names | ||
- Loops through each subdirectory and opens the default.rtsz file in the current subdirectory using the Open-RoyalDocument cmdlet | ||
- Gets the credentials from the Royal TS document using the Get-RoyalObject cmdlet | ||
- Finds the Domain Administrator credential object and stores the login details | ||
- Loops through each credential object and checks if the CustomField3 property is equal to "msad" | ||
- If the CustomField3 property is equal to "msad", generates a random 20-character string and sets the AD user password using the Set-ADAccountPassword cmdlet from the ActiveDirectory module | ||
- Stores the new password in the credential object and saves the changes to the Royal TS document using the Out-RoyalDocument cmdlet | ||
- Closes the document using the Close-RoyalDocument cmdlet | ||
#> | ||
|
||
Import-Module ActiveDirectory | ||
Import-Module royaldocument.powershell | ||
# Define DC | ||
$ComputerName = "192.168.13.2" | ||
|
||
# Function to generate a random 20-character string | ||
function Generate-RandomString { | ||
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/*-+.' | ||
$randomString = "" | ||
for ($i = 0; $i -lt 20; $i++) { | ||
$randomString += $characters[(Get-Random -Minimum 0 -Maximum $characters.Length)] | ||
} | ||
return $randomString | ||
} | ||
|
||
|
||
# Set the Configuration for the Documents store | ||
$Password = ConvertTo-SecureString "PASSWORD" -AsPlainText -Force | ||
$Username = "automated_task" | ||
$RoyalStore = New-RoyalStore -UserName $Username | ||
|
||
# Specify the base directory where GUID-named subdirectories are located | ||
$baseDirectory = "C:\RoyalServer\DocumentStore\Documents" | ||
|
||
# Get a list of subdirectories with GUID names | ||
$subdirectories = Get-ChildItem -Path $baseDirectory -Directory | ||
|
||
|
||
# Construct the path to the default.rtsz file in the current subdirectory | ||
$documentPath = "C:\RoyalServer\DocumentStore\Documents\ID\default.rtsz" | ||
|
||
|
||
# Open the Royal Document | ||
$RoyalDocument = Open-RoyalDocument -FileName $documentPath -Store $RoyalStore -Password $Password | ||
|
||
# Get the credentials from the Royal TS document | ||
$credentials = Get-RoyalObject -Store $RoyalStore -Type RoyalCredential | ||
|
||
# Find the Domain Administrator credential object and store the login details | ||
$domainAdminCredential = $credentials | Where-Object { $_.Name -eq "Domain Administrator" } | ||
$domainAdminCredential = New-Object System.Management.Automation.PSCredential ($domainAdminCredential.UserName, ($domainAdminCredential.Password | ConvertTo-SecureString -AsPlainText -Force)) | ||
|
||
# Loop through each credential object | ||
foreach ($credential in $credentials) { | ||
if ($credential.CustomField3 -eq "msad") { | ||
Write-Host "found" | ||
# Generate a random 20-character string | ||
$newPassword = Generate-RandomString | ||
$secureNewPassword = ConvertTo-SecureString -String $newPassword -AsPlainText -Force | ||
|
||
# Set the AD user password | ||
$sessionOption = New-PSSessionOption -SkipCACheck -SkipCNCheck | ||
try { | ||
Invoke-Command -ComputerName $ComputerName -Credential $domainAdminCredential -UseSSL -SessionOption $sessionOption -ScriptBlock { | ||
param($adUserName, $secureNewPassword) | ||
Set-ADAccountPassword -Identity $adUserName -NewPassword $secureNewPassword -Reset | ||
} -ArgumentList $credential.Name, $secureNewPassword | ||
|
||
# Store the new password in the credential object | ||
$credential.Password = $newPassword | ||
Write-Host "Password updated successfully for AD user $($credential.Name)." | ||
} catch { | ||
Write-Host "Failed to update password for AD user $($credential.Name): $_" | ||
Write-Host "Failed to update password for AD user $($credential.Name): $_" | ||
} | ||
} | ||
} | ||
|
||
# Save the changes to the Royal TS document | ||
Out-RoyalDocument -Document $RoyalDocument -FileName $documentPath | ||
|
||
# Close the document | ||
Close-RoyalDocument -Document $RoyalDocument | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Write-Host
seems redundant