153 lines
5.5 KiB
PowerShell
153 lines
5.5 KiB
PowerShell
Add-Type -AssemblyName System.Windows.Forms
|
|
|
|
# Create the form
|
|
$form = New-Object System.Windows.Forms.Form
|
|
$form.Text = "Instructions"
|
|
$form.Size = New-Object System.Drawing.Size(800, 550)
|
|
$form.StartPosition = "CenterScreen"
|
|
|
|
# Create a label to display the instructions
|
|
$label = New-Object System.Windows.Forms.Label
|
|
$label.Text = @"
|
|
This script will read a CSV file with users in samaccountname format in a column named 'username' and will set the -ChangePasswordAtLogon property to $true.
|
|
|
|
Please select the CSV file.
|
|
|
|
Clicking Accept will proceed with the operation. Make sure you have confirmed the CSV file is correct before proceeding.
|
|
"@
|
|
$label.AutoSize = $true
|
|
$label.MaximumSize = New-Object System.Drawing.Size(560, 0) # Set maximum width and allow height to adjust
|
|
$label.Location = New-Object System.Drawing.Point(10, 80)
|
|
$form.Controls.Add($label)
|
|
|
|
# Define author information
|
|
$authorName = "Zak Bearman"
|
|
$department = "Wintel Platforms"
|
|
$year = "2025"
|
|
|
|
# Create a label to display the author
|
|
$label2 = New-Object System.Windows.Forms.Label
|
|
$label2.Text = "Written by $authorName - $department $year."
|
|
$label2.AutoSize = $true
|
|
$label2.MaximumSize = New-Object System.Drawing.Size(560, 0) # Set maximum width and allow height to adjust
|
|
$label2.Location = New-Object System.Drawing.Point(160, 420)
|
|
$form.Controls.Add($label2)
|
|
|
|
# Create a TextBox to display the CSV content
|
|
$textBox = New-Object System.Windows.Forms.TextBox
|
|
$textBox.Multiline = $true
|
|
$textBox.ScrollBars = "Vertical"
|
|
$textBox.ReadOnly = $true
|
|
$textBox.Size = New-Object System.Drawing.Size(200, 500)
|
|
$textBox.Location = New-Object System.Drawing.Point(568, 5)
|
|
$form.Controls.Add($textBox)
|
|
|
|
# Create a button to browse for the CSV file
|
|
$browseButton = New-Object System.Windows.Forms.Button
|
|
$browseButton.Text = "Browse"
|
|
$browseButton.Location = New-Object System.Drawing.Point(250, 240)
|
|
$script:csvPath = $null
|
|
$browseButton.Add_Click({
|
|
$script:csvPath = $null
|
|
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
|
|
$openFileDialog.Filter = "CSV files (*.csv)|*.csv"
|
|
if ($openFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
|
|
$script:csvPath = $openFileDialog.FileName
|
|
$textBox.Text = Get-Content -Path $script:csvPath -Raw
|
|
}
|
|
})
|
|
$form.Controls.Add($browseButton)
|
|
|
|
# Create an Accept button
|
|
$acceptButton = New-Object System.Windows.Forms.Button
|
|
$acceptButton.Text = "Accept"
|
|
$acceptButton.Location = New-Object System.Drawing.Point(200, 300)
|
|
$acceptButton.Add_Click({
|
|
if (-not $script:csvPath) {
|
|
[System.Windows.Forms.MessageBox]::Show("No CSV file selected. Please select a CSV file to proceed.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
return
|
|
}
|
|
$form.Tag = "Accept"
|
|
$form.Close()
|
|
})
|
|
$form.Controls.Add($acceptButton)
|
|
|
|
# Create a Cancel button
|
|
$cancelButton = New-Object System.Windows.Forms.Button
|
|
$cancelButton.Text = "Cancel"
|
|
$cancelButton.Location = New-Object System.Drawing.Point(300, 300)
|
|
$cancelButton.Add_Click({
|
|
$form.Tag = "Cancel"
|
|
$form.Close()
|
|
})
|
|
$form.Controls.Add($cancelButton)
|
|
|
|
# Show the form
|
|
$form.ShowDialog()
|
|
|
|
# Check the form result
|
|
if ($form.Tag -eq "Cancel") {
|
|
Write-Output "Operation cancelled by the user."
|
|
exit
|
|
}
|
|
|
|
# Import the CSV file
|
|
$userList = Import-Csv -Path $script:csvPath
|
|
|
|
# Check if the path exists, if not create it
|
|
$destinationPath = "C:\temp\useroutput\done"
|
|
if (-not (Test-Path -Path $destinationPath)) {
|
|
New-Item -ItemType Directory -Path $destinationPath | Out-Null
|
|
}
|
|
|
|
# Get today's date
|
|
$todaysDate = Get-Date -Format "yyyyMMdd"
|
|
|
|
# Set the output CSV file path
|
|
$outputCsvPath = "$destinationPath\users-done-$todaysDate.csv"
|
|
|
|
# Loop through each user in the CSV
|
|
foreach ($user in $userList) {
|
|
$samAccountName = $user.username
|
|
|
|
try {
|
|
# Get the user object by SamAccountName
|
|
$userObject = Get-ADUser -Filter {SamAccountName -eq $samAccountName}
|
|
|
|
if ($null -eq $userObject) {
|
|
throw "User '$samAccountName' does not exist."
|
|
}
|
|
|
|
# Set the password to expired
|
|
Set-ADUser -Identity $samAccountName -ChangePasswordAtLogon $true
|
|
|
|
Write-Output "Password for user '$samAccountName' has been set to expired."
|
|
}
|
|
catch {
|
|
Write-Output "Failed to set password for user '$samAccountName': $_"
|
|
}
|
|
}
|
|
catch {
|
|
Write-Output "Failed to set password for user '$samAccountName': $_"
|
|
}
|
|
# Export the updated user list to the new CSV file
|
|
$userList | Export-Csv -Path $outputCsvPath -NoTypeInformation
|
|
|
|
Write-Output "The updated user list has been exported to $outputCsvPath."
|
|
|
|
# Rename the source CSV file
|
|
$originalFileName = [System.IO.Path]::GetFileNameWithoutExtension($script:csvPath)
|
|
$originalFileExtension = [System.IO.Path]::GetExtension($script:csvPath)
|
|
$directory = [System.IO.Path]::GetDirectoryName($script:csvPath)
|
|
$newFileName = "$originalFileName-done-$todaysDate$originalFileExtension"
|
|
$newFilePath = [System.IO.Path]::Combine($directory, $newFileName)
|
|
|
|
Rename-Item -Path $script:csvPath -NewName $newFileName
|
|
|
|
Write-Output "The original CSV file has been renamed to $newFilePath."
|
|
|
|
# Open the folder containing the renamed CSV file
|
|
Start-Process -FilePath "explorer.exe" -ArgumentList "/select,`"$newFilePath`""
|
|
|
|
# Clear all variables from active memory
|
|
Remove-Variable -Name form, label, label2, textBox, browseButton, acceptButton, cancelButton, csvPath, userList, destinationPath, todaysDate, outputCsvPath, originalFileName, originalFileExtension, directory, newFileName, newFilePath -ErrorAction SilentlyContinue |