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 9176953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:13:33+00:00 2026-06-17T17:13:33+00:00

I have this test script to change the Administrator password on a list of

  • 0

I have this test script to change the Administrator password on a list of servers.
I have set the script to log errors if the server can’t be ping’d or account can’t be found etc. However in addtion to this i’d like to capture any other errors that take place and also add those to the log file. I know you can use the “Try and Catch” for error handling but havn’t had any luck so far.

Would someone be kind enough to show how to do it?

Here is the script

    $date = Get-Date
$user = "Administrator"
$newpwd = "MyPassword"

     $servers = gc C:\servers.txt

foreach ($server in $servers) 
{ 


  $ping = new-object System.Net.NetworkInformation.Ping

  $Reply = $null 
  $Reply = $ping.send($server)


  if($Reply.status -like 'Success')
    {

    $Admin=[adsi]("WinNT://" + $server + "/$user, user")

    if($?) 
      {
       $Admin.SetPassword($newpwd)

     if($?)

      {Add-Content -path C:\Audit\logs\servers-reset.csv -Value "$server,  Succsess the $user password was changed. , $date"}

      else
        {Add-Content -path C:\Audit\logs\servers-reset.csv -Value "$server, Error: FAILED to change the password. , $date"}
      }  
      else
      {
      Add-Content -path C:\Audit\logs\servers-reset.csv -Value "$server, Error: The $user user account was not found on the server. , $date"}
      }    
      else
      {
      Add-Content -path C:\Audit\logs\servers-reset.csv -Value "$server, Error: Ping FAILED could not connect. , $date"
      }
  • 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-17T17:13:34+00:00Added an answer on June 17, 2026 at 5:13 pm

    If you want to write exceptions to the log right after they were thrown, you could use a trap. Add something like this to you script:

    trap [Exception] {
    
        #Add message to log
        Add-Content -Path test.csv -Value "$server, $($_.Exception.Message), $(Get-Date)"
    
        #Continue script
        continue; 
    }
    

    That will log all exceptions (not all errors).

    If you want all errors, you can access them using $Error. It’s an arraylist containing every error during your sessions(script). The first item $Error[0] is the latest error. This however, is not something that fits directly into an csv file without formatting it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a ubuntu test server that this simple script works fine on where
I have a batch script that looks like this: Test.bat @echo off :: Starts
I have this script: #!/var/home/cherry/opt/perl use Test::More; use DBI; use Test::mysqld; use Data::Dumper; my
Say I have this code in my page: <script language=javascript> $(document).ready(function() { $(.test).click(function() {
This is a simple script I have written to test command line argument handling:
I have this test [Test] public void SaveInventoryItemLoad_Will_Call_WCF_Service_SaveInventoryItemLoad() { adapter.SaveInventoryItemLoad(new List<InventoryItemLoadProxy>()); itemMasterBusinessClientMock.Verify(x => x.SaveInventoryItemLoad(It.IsAny<List<InventoryItemLoadProxy>>()),
I have this test case def setUp(self): self.user = User.objects.create(username=tauri, password='gaul') def test_loginin_student_control_panel(self): c
We have a problem on Debian server and PHP version 4.4.4-8+etch6. In this script
Below i have purposely made a test script: you can see it live at
I have this small script: http://jsfiddle.net/gmAjC/ <input name=n1 value=test> <br/><span></span> <br/> <span style=background-color:red>after input</span>

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.