psPAS loop not working, how do I loop through a test file?

33 views Asked by At

I have this ps1 script that works for a single safe.

$AccountsDetails = New-Object System.Collections.ArrayList

$accounts = Get-PASAccount - safeName RESEARCH_MAINT


if ($accouonts -eq $null -or $accounts.Count -eq 0) {
    Write-Host "No accounts retrieve from the safe."
    }

foreach ($account in $accounts) {
        $AccountsDetails.add((Get-PASAccountDetail -id $account.id)) | Out-Null
        }
        
        $LockedAccountsInfo = @()
        
foreach ($AccountDetail in $AccountsDetails.Details) {
        if ($AccountDetail.LockedBy -notin "",$null) {
            $LockedAccountsInfo += @{
                Username = $AccountDetail.RequiredProperties.Username
                safe = $AccountDetail.safeName
                LockedBy = $AccountDetail.LockedBy
                }
            }
        }
if ($LockedAccountsInfo.Count -eq 0) {
    write-host "No locked accounts were found"
    return
    } else {
            write-host "$(LockedAccountsInfo.Count) locked account(s) found."

$filePath = "c:\test.txt"
foreach ($lockedAccount in $LockedAccountsInfo) {
    $line = "Username: $($LockedAccount.Username), LockedBy: $($LockedAccount.LockedBy), Safe: $($LockedAccount.Safe)"
    $line | Out-File -FilePath $filePath -Append -Encoding UTF8
    
Write-Host "Locked accounts information has been saved to $filePath."

Instead of finding just one safe, I have a txt file with a list of safes that I want it to loop through. I've modified it to be this script instead

"# Initialize an ArrayList to store details of all accounts from all safes
$AllAccountsDetails = New-Object System.Collections.ArrayList

# Specify the path to the .txt file containing the list of safes
$safesFilePath = "c:\path\to\safes.txt"

# Read the list of safes from the .txt file
$safes = Get-Content -Path $safesFilePath

# Iterate through each safe listed in the file
foreach ($safeName in $safes) {
    # Retrieve accounts from the current safe
    $accounts = Get-PASAccount -safeName $safeName

    # Check if the accounts retrieval returned null or an empty collection
    if (-not $accounts -or $accounts.Count -eq 0) {
        # Inform the user no accounts were retrieved from the current safe
        Write-Host "No accounts retrieved from safe: $safeName."
    } else {
        # Loop through each account retrieved and add its details to the ArrayList
        foreach ($account in $accounts) {
            $null = $AllAccountsDetails.Add((Get-PASAccountDetail -id $account.id))
        }
    }
}

# Initialize an array to hold information about locked accounts
$LockedAccountsInfo = @()

# Iterate over the account details to find any locked accounts
foreach ($AccountDetail in $AllAccountsDetails) {
    # Check if the account is locked by someone
    if ($AccountDetail.LockedBy -notin @("", $null)) {
        # If locked, add the relevant details to the LockedAccountsInfo array
        $LockedAccountsInfo += @{
            Username = $AccountDetail.RequiredProperties.Username
            Safe     = $AccountDetail.SafeName
            LockedBy = $AccountDetail.LockedBy
        }
    }
}

# Check if any locked accounts were found
if ($LockedAccountsInfo.Count -eq 0) {
    # Inform the user if no locked accounts were found
    Write-Host "No locked accounts were found."
} else {
    # Display the count of locked accounts found
    Write-Host "$($LockedAccountsInfo.Count) locked account(s) found."

    # Specify the file path where the information will be saved
    $filePath = "c:\path\to\lockedAccountsInfo.txt"
    # Loop through each locked account and append its details to the file
    foreach ($lockedAccount in $LockedAccountsInfo) {
        $line = "Username: $($lockedAccount.Username), LockedBy: $($lockedAccount.LockedBy), Safe: $($lockedAccount.Safe)"
        # Use Add-Content for appending to ensure each line is added without overwriting existing content
        Add-Content -Path $filePath -Value $line -Encoding UTF8
    }

    # Inform the user that the locked accounts information has been successfully saved
    Write-Host "Locked accounts information has been saved to $filePath."
}
"

I'm getting an error. that i can't figure out. "Invoke-PASRestMethod : [400] The format of the given filter is not supported. At line: 190 char: 13 $$result = Invoke-PASRestMethod -Uri $URI -Method GET -WebSession $$..."

Modify first code to loop through a text file for the safes and get the locked account in each safe.

0

There are 0 answers