I have an array named $ExpiredAdmin that I’m building withing a forEach loop. I want to add column $property.SamAccountName to this existing array, but with a different column name? Is this possible to do?
clear-host
$timestamp = (Get-Date -format “yyyy-MM-dd_HH-mm-ss”)
$path = (Get-Item -Path "." -Verbose).FullName
$OutputFile = $path + "" + $timestamp + “_Cap_Locked_Expired.xlsx”
Write-Host “OutputFile =” $OutputFile
#$exists = Test-Path $OutputFile
#If ($exists -like “True”)
#{Remove-Item $OutputFile else New-Item $OutputFile -ItemType File}
Remove-Item $OutputFile -ErrorAction Ignore
Import-module ActiveDirectory -ErrorAction stop
$LockedOut = Search-ADAccount -UsersOnly -LockedOut |
Get-ADUser -Properties DisplayName,
Mail,
LastlogonDate,
Enabled,
AccountLockoutTime,
LastBadPasswordAttempt,
BadPwdCount,
LockedOut,
Company,
Description | Where-Object {$.Company -like “Cap” -or $.Description -like “Cap” -or $_.Description -like “Local Admin Account” }
$LockedOut = $LockedOut | select SamAccountName, DisplayName, Company, Description, LockedOut, AccountLockoutTime, BadPwdCount, LastBadPasswordAttempt, Enabled, LastLogonDate, Mail
Get a list of expired user accounts
$ExpiredUsers = Get-ADUser -filter {Company -like “Cap” -or Description -like “Cap”} -SearchBase “OU=Accounts,DC=mycompany,DC=com” -properties SamAccountName, Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, Company, Description | where {$.Enabled -eq “True”} | where {$.PasswordNeverExpires -eq $false} | where {$_.passwordexpired -eq $true}
$ExpiredUsers = $ExpiredUsers | select SamAccountName, Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, Company, Description
$ExpiredAdmWk = Get-ADUser -filter {SamAccountName -like “*-adm” -and SamAccountName -ne “svc-adm”} -SearchBase “OU=Accounts,DC=mycompany,DC=com” -properties SamAccountName, Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, Company, Description | where {$.Enabled -eq “True”} | where {$.PasswordNeverExpires -eq $false} | where {$_.passwordexpired -eq $true}
$ctr = 0
$ExpiredAdmin = @()
foreach ($property in $ExpiredAdmWk)
{
Get the Account Info for the non-admin account (SSO without the -adm)
$ctr++
#Write-Host $ctr $property.SamAccountName $property.SamAccountName.Substring(0,$property.SamAccountName.IndexOf(“-adm”))
try {
$user = Get-ADUser -Identity $property.SamAccountName.Substring(0,$property.SamAccountName.IndexOf(“-adm”)) -Properties SamAccountName, Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, Company, Description
}
catch
{
Write-Host “Error processing” $property.SamAccountName $property.SamAccountName.Substring(0,$property.SamAccountName.IndexOf(“-adm”))
}
if ($user.Company -like “Cap*” -or $user.Description -like “Cap*”)
{
$ExpiredAdmin += $user
#Write-Host “** Cap User **” $property.SamAccountName $user.SamAccountName -ForegroundColor Yellow
}
}
$ExpiredAdmin