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 Automated AD User Password Change.ps1 #84

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
94 changes: 94 additions & 0 deletions Automation/PowerShell/Automated AD User Password Change.ps1
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): $_"
}
}
Comment on lines +84 to +86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Second identical Write-Host seems redundant
  • Could you please fix the indentation of the curly braces

}

# Save the changes to the Royal TS document
Out-RoyalDocument -Document $RoyalDocument -FileName $documentPath

# Close the document
Close-RoyalDocument -Document $RoyalDocument