Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Change password Powershell

Posted on 2009-05-01
1
Medium Priority
?
4,424 Views
Last Modified: 2012-05-06
I found this nice little script that will help me change my passwords.  It seems to work, but I do not get any output.

PS C:\ChangeAdmin> .\change.ps1
PS C:\ChangeAdmin>

It appears to be written to dump stuff to excel (there is no excel installed on the server I am running the script from), but I can't find any excel file.

In addition to the excel format, how would I make this dump all the actions to the screen in addition to the excel file, so I can check the work and modify if needed?
# Credit to http://myitforum.com/cs2/blogs/yli628/archive/2007/08/23/powershell-script-to-change-administrator-password-on-a-list-of-machines.aspx
 
$erroractionpreference = "SilentlyContinue"
 
$a = New-Object -comobject Excel.Application
$a.visible = $True
 
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
 
$c.Cells.Item(1,1) = "Machine Name"
$c.Cells.Item(1,2) = "Password Changed"
$c.Cells.Item(1,3) = "Report Time Stamp"
 
$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True
 
$intRow = 2
 
foreach ($strComputer in get-content C:\MachineList.Txt)
{
$c.Cells.Item($intRow,1)  = $strComputer.ToUpper()
 
# Using .NET method to ping test the servers  This is very cool!
$ping = new-object System.Net.NetworkInformation.Ping
 
$Reply = $ping.send($strComputer)
 
 
if($Reply.status -eq "success")
{
# This is the Key Part
$admin=[adsi]("WinNT://" + $strComputer + "/administrator, user")
 
$admin.psbase.invoke("SetPassword", "Whatever1")
 
#$admin.psbase.CommitChanges() - I am surprised that I don't have to do this!
 
# If this is for AD account, we could use PasswordLastchanged attribute. But WinNT provider does not #support the PasswordLastChanged attribute!
 
# I was trying to use passwordage attribute value but somehow I found it give you the value for last time, #may be because there is a delay for this attribute to propagate. So I made an executive decision to test #if passwordage is $null  so this may not be 100% accurate.
 
$pwage = $admin.passwordage
 
If($pwage -ne $null)
{
$c.Cells.Item($intRow,2).Interior.ColorIndex = 4
$c.Cells.Item($intRow,2) = "Yes"
}
Else
{
$c.Cells.Item($intRow,2).Interior.ColorIndex = 3
$c.Cells.Item($intRow,2) = "No"
}
}
Else
{
$c.Cells.Item($intRow,2).Interior.ColorIndex = 3
$c.Cells.Item($intRow,2) = "Not Pingable"
}
 
$c.Cells.Item($intRow,3) = Get-Date
 
$Reply = ""
$pwage = ""
$intRow = $intRow + 1
}
$d.EntireColumn.AutoFit()
 
#cls

Open in new window

0
Comment
Question by:loftyworm
1 Comment
 
LVL 4

Accepted Solution

by:
peter_field earned 2000 total points
ID: 24288203
try the code in the code snippet below

You will need Excel installed to use the Excel COM object, so I removed all reference to it.

This will now collect the output in a variable, at the end of the script it will print the variable to the screen and export it to a CSV file, which is probably as good as an Excel file, except you wont have colours.

I haven't tested it, so hopefully it works for you. If there are any errors have a go at fixing them, otherwise post back and I'll actually test/fix the code.
# Credit to http://myitforum.com/cs2/blogs/yli628/archive/2007/08/23/powershell-script-to-change-administrator-password-on-a-list-of-machines.aspx
 
$erroractionpreference = "SilentlyContinue"
 
$results = @()
 
foreach ($strComputer in get-content C:\MachineList.Txt)
{
    $entry = New-Object PSObject
    $entry | Add-Member -MemberType NoteProperty -Name MachineName -Value $strComputer.ToUpper()
 
    # Using .NET method to ping test the servers  This is very cool!
    $ping = new-object System.Net.NetworkInformation.Ping
 
    $Reply = $ping.send($strComputer)
 
 
    if($Reply.status -eq "success")
    {
        # This is the Key Part
        $admin=[adsi]("WinNT://" + $strComputer + "/administrator, user")
 
        $admin.psbase.invoke("SetPassword", "Whatever1")
 
        #$admin.psbase.CommitChanges() - I am surprised that I don't have to do this!
 
        # If this is for AD account, we could use PasswordLastchanged attribute. But WinNT provider does not #support the PasswordLastChanged attribute!
 
        # I was trying to use passwordage attribute value but somehow I found it give you the value for last time, #may be because there is a delay for this attribute to propagate. So I made an executive decision to test #if passwordage is $null  so this may not be 100% accurate.
 
        $pwage = $admin.passwordage
 
        If($pwage -ne $null)
        {
            $entry | Add-Member -MemberType NoteProperty -Name PasswordChanged -Value "Yes"
        }
        Else
        {
            $entry | Add-Member -MemberType NoteProperty -Name PasswordChanged -Value "No"
        }
    }
    Else
    {
        $entry | Add-Member -MemberType NoteProperty -Name PasswordChanged -Value "Not Pingable"
    }
 
    $entry | Add-Member -MemberType NoteProperty -Name ReportTimeStamp -Value Get-Date
 
    $results += $entry
 
    $Reply = ""
    $pwage = ""
}
 
$results
$results | Export-Csv "c:\temp\results.csv"

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question