Skip to content
All articles
Automation 10 min read26 Sept 2026

PowerShell Automation: Common Problems & Solutions

Troubleshooting production PowerShell automation — silent failures, deprecated modules, stored credentials, non-idempotent scripts and the engineering discipline that makes runbooks reliable instead of fragile.

PowerShellAutomationMicrosoft GraphAzure AutomationTroubleshooting

Automation problems are engineering discipline problems

PowerShell automation fails in production for predictable reasons: errors are silently swallowed, scripts depend on deprecated modules, credentials are stored in plaintext, and non-idempotent logic creates duplicates on re-run. None of these are PowerShell bugs — they are engineering gaps that turn a working script into a fragile runbook. The fix is to treat PowerShell like production code: structured error handling, managed identities, idempotent logic, and version control.

This article covers the problems I see most in MSP and enterprise automation: silent failures, the MSOnline/AzureAD module deprecation, credential storage, non-idempotent bulk operations, and the runbook reliability patterns that resolve each. For the broader best-practice framework, see the companion guide on PowerShell automation and scripting for enterprise IT.

Problem 1: Silent failures — errors swallowed and never logged

A scheduled runbook that “succeeds” but did nothing is worse than one that fails loudly, because nobody investigates. The cause is almost always a missing `$ErrorActionPreference = "Stop"` combined with a cmdlet that throws a non-terminating error, or a try/catch that catches and discards the exception without logging it.

The fix is two-fold: set `$ErrorActionPreference = "Stop"` at the top of modules so non-terminating errors become terminating and enter the catch block, and log every caught exception to a structured sink (CSV, log file, or a webhook into the ticketing system) before re-throwing or exiting. A runbook that fails should always leave an auditable trace.

PowerShell — Structured error handling that does not swallow failures
$ErrorActionPreference = "Stop"
function Invoke-LoggedAction {
  param([string]$Action, [scriptblock]$Work)
  try {
    & $Work
    [PSCustomObject]@{ Time = (Get-Date); Action = $Action; Result = "OK" } |
      Export-Csv -Path "$env:TEMP\ops.log" -Append -NoTypeInformation
  } catch {
    [PSCustomObject]@{ Time = (Get-Date); Action = $Action; Result = $_.Exception.Message } |
      Export-Csv -Path "$env:TEMP\ops.log" -Append -NoTypeInformation
    throw  # re-throw so the runbook reports failure
  }
}

Problem 2: Deprecated MSOnline and AzureAD modules

The MSOnline and AzureAD PowerShell modules are deprecated and will stop working against Microsoft Graph. Scripts that still call `Connect-MsolService` or `Connect-AzureAD` are a ticking time bomb — they will fail with no code change on your side when Microsoft retires the endpoints.

The migration path is the Microsoft Graph PowerShell SDK (`Microsoft.Graph` module). Most cmdlets have a direct Graph equivalent: `Get-MsolUser` becomes `Get-MgUser`, `Get-AzureADGroup` becomes `Get-MgGroup`. Audit your scripts for the deprecated modules now and migrate before the retirement forces an emergency rewrite.

Deprecated cmdletMicrosoft Graph equivalentNotes
Get-MsolUserGet-MgUserUse -Filter for server-side filtering
Get-AzureADGroupGet-MgGroupConsent to Group.Read.All scope
Set-MsolUserLicenseSet-MgUserLicenseGroup-based licensing preferred
Connect-MsolServiceConnect-MgGraphUse app-only cert for runbooks

Problem 3: Stored credentials and service-account passwords

A runbook that stores a service-account password in plaintext (or in a weakly protected variable) is a security incident waiting to happen. If the runbook is exfiltrated or the automation account compromised, the credential is exposed.

The fix is managed identities in Azure Automation — the runbook authenticates to Graph, Azure, and Key Vault using the automation account’s managed identity, with no password in the code or configuration. Where a managed identity is not possible, store the secret in Azure Key Vault and retrieve it at runtime with the managed identity. Never hardcode or store plaintext credentials in a runbook.

  • Use a managed identity in Azure Automation — no stored passwords
  • Store any required secrets in Key Vault, retrieve at runtime
  • Use app-only certificate auth for unattended Graph access
  • Rotate service-account credentials on a schedule, not on incident

Problem 4: Non-idempotent scripts that duplicate on re-run

A joiner-mover-leaver script that creates a user, assigns licences, and opens a ticket will create duplicates if it runs twice for the same person — and scheduled runbooks re-run. The fix is idempotency: check whether the target state already exists before creating it, so a re-run is a no-op rather than a duplicate.

The pattern is a guard clause at the top of each operation: if the user exists, skip creation; if the licence is already assigned, skip assignment; if the ticket exists, skip creation. Idempotent scripts are safe to re-run, safe to retry after a failure, and safe to schedule — non-idempotent scripts are none of these.

PowerShell — Idempotent new-hire provisioning guard
function New-ProvisionedUser {
  param([string]$GivenName, [string]$Surname, [string]$Dept)
  $upn = ("{0}.{1}@corp.contoso.com" -f $GivenName, $Surname).ToLower()
  # Idempotency guard — skip if the user already exists
  if (Get-MgUser -Filter "userPrincipalName eq '$upn'" -ErrorAction SilentlyContinue) {
    Write-Verbose "User $upn exists — skipping creation"
    return
  }
  New-MgUser -DisplayName "$GivenName $Surname" `
    -GivenName $GivenName -Surname $Surname -UserPrincipalName $upn `
    -MailNickname "$GivenName.$Surname" -AccountEnabled
  # Group-based licensing handles the licence assignment
}

Trade-offs and next steps

Automation design balances speed against safety. A quick script that works once is fast to write but fragile in production; an engineered runbook with error handling, idempotency, and managed identities takes longer to build but runs unattended for years. The right balance is to engineer the high-frequency, high-risk automations (joiner-mover-leaver, bulk licence changes, reporting) and leave one-off tasks as ad-hoc scripts.

When problems recur, the next step is usually to mature the engineering practice: move scripts into Git for versioning, add a -WhatIf review before any bulk destructive operation, and centralise logging so a failed runbook leaves an auditable trace. For the full best-practice framework, see the PowerShell automation and scripting enterprise guide.

  • Engineer high-frequency, high-risk automations; leave one-offs as ad-hoc scripts
  • Version scripts in Git, not in a shared folder
  • Always run -WhatIf before a bulk destructive operation
  • Centralise logging so every runbook leaves an auditable trace

Lessons learned

  • Set $ErrorActionPreference = "Stop" and log every caught exception
  • Migrate off MSOnline/AzureAD to the Microsoft Graph SDK now
  • Use managed identities — never store plaintext credentials in a runbook
  • Make every script idempotent so re-runs are safe

Written by

Jamshed Alam

Global MSP & IT Infrastructure Consultant with 20 years across Exchange, Microsoft 365, Azure, security and automation. Available for project and contract work worldwide.

Share
JA

Jamshed Alam

Global MSP | IT Infrastructure Consultant | Cloud & Automation Expert · Lahore, Pakistan

EmailLinkedInUpworkGuru© 2026 Jamshed Alam