Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7959801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:37:49+00:00 2026-06-04T04:37:49+00:00

What is the best way to analyze powershell cmdlets in production ? Suppose you

  • 0

What is the best way to analyze powershell cmdlets in production ? Suppose you write a script which does the following –

  1. Write lof of registry values
  2. Register COM Dlls
  3. Make IIS AppPools
  4. Start Windows Services

&…something goes wrong in between, then what are the best practices to inform user such that root issue can be traced and debugged ?

Suppose, user credentials fail for AppPool creation for some reason
and I want to stop processing at that time plus I want to rollback
what I had done earlier.

Is verbose mode + Logging an elegant way to collect each details at every step ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:37:50+00:00Added an answer on June 4, 2026 at 4:37 am

    I wrote a Write-Log function that I posted on poshcode ( http://poshcode.org/3270 ) that I use for production level PowerShell programs. Alternatively you could use Start-Transcript which logs almost everything displayed on the console to a file. There are a couple gotchas about Start-Transcript –

    1. It won’t log external program output unless you force it through the host output API by piping it to Out-Host such as ping.exe localhost | Out-Host.
    2. It’s not supported on all hosts. For example PowerGUI doesn’t support it so you have to add a host check using $host.

    The pattern I usually use for error handling is to wrap everything in a try/catch. The exception object will be available as $_ in the catch block. It will contain everything about the error that happened, the message, the line and column number etc…

    I also set the $ErrorActionPreference to Stop so that all cmdlets throw a terminating error so the script won’t continue. It looks like this:

    $ErrorActionPreference = "Stop"
    try {
        # Write lof of registry values
        New-Item -Path HKCU:\Software\MyTest -ItemType Directory
        New-ItemProperty -Path HKCU:\Software\MyTest -Name MyTestValue -Value Test
    
        # Register COM Dlls
        regsrv32 my.dll
        if ($LASTEXITCODE -ne 0) { throw "Failed to register my.dll" }
    
        # Make IIS AppPools
        IIS:\>New-WebAppPool NewAppPool
    
        # Start Windows Services
        Start-Service -Name MyService
    } catch {
        Write-Log ("Script failed. The error was: '{0}'." -f $_)
    }
    

    Rolling back is not easy… The only thing that might be easy to roll back are the registry operations because the registry supports transactions (assuming Vista or beyond). You can just create a transaction (like a database) and roll it back if an error occurs. The rest of the operations you mentioned will need specific code to roll them back. You could add the rollback code to the catch block like this:

    } catch {
        # Undo the registry transaction.
        # Unregister the DLL.
        # Delete the App pool if it exists.
        # Stop the windows service.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to analyze crashes on Linux? We expect to build
the best way to explain is with example so: this is the model public
The best way I can think of to ask this is by example... In
The best way of describing this is I have a table of people with
The best way to store images into MySQL is by storing the image location
What is the best way to maintain upgrade scripts between versions of a product?
What is the best way to view the HTTP response headers from a request
What's the best way in C# to set up a utility app that can
What is the best way to implement a three item hashMap? For example, I
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.