Mark Minasi's Reader Forum
Mark Minasi's Reader Forum
Home | Profile | Register | Active Topics | Active Polls | Members | Search | FAQ | Minasi Forum RSS Feed
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 HALP! Questions on Windows and Windows Server
 Scripting
 Change Server Local Administrator name
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

philrobeson
Here To Stay

USA
256 Posts
Status: offline

Posted - 06/03/2011 :  1:31:21 PM  Show Profile  Reply with Quote
Due to a random generator, we never know the Administrator username on a server. However, our policy prior to taking a snapshot is to change the name and password to ADMIN and Password1, which is where my problem is...I wrote a powershell script that shows me the name of that account, but I do not know how to write over that name and make it ADMIN.
This is the part of the script that finds the account name:

Get-ADComputer $strComputer | ForEach {
$Accounts = Get-WmiObject -Computer $_.Name -Namespace root\CIMV2 -Query "SELECT * FROM

Win32_UserAccount WHERE LocalAccount = TRUE"
ForEach ($Account In $Accounts)
{
If ($Account.SID -match "S-1-5-.*-500")
{
$Admin = $Account.Name
Break
}
}
Write-Host $Admin
}

Any help would be appreciated.

Phil Robeson
MIS/M

philrobeson
Here To Stay

USA
256 Posts
Status: offline

Posted - 06/06/2011 :  07:40:47 AM  Show Profile  Reply with Quote
Well, no one helped, but that is ok...after much trial-n-error, I figured it out, but it is causing an undesirable output that I need help with. In the below code, all the lines down to "$strComputer = "$x"" are used to produce a windows input form so you can type in a computer name. However, though the last three lines of code changes the Administrator user account to ADMIN, it also displays this on the screen:

__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0

Does anyone know how to stop this output from being displayed? Thanks in advance, here is my code:

###########################################################
# Change Local Administrator Name Script #
# Created by Phil Robeson #
# Date created: June 3, 2011 #
# Comments to: phil@robeson.biz #
###########################################################

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Computer Name"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Enter the computer name:"
$objForm.Controls.Add($objLabel)

$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

$strComputer = "$x"

Get-ADComputer $strComputer | ForEach {
$AdminAccount = Get-WmiObject -Computer $_.Name -Namespace root\CIMV2 -Query "SELECT * FROM Win32_UserAccount WHERE LocalAccount = TRUE AND SID LIKE 'S-1-5%-500'"
$AdminAccount.rename("ADMIN")}


Phil Robeson
MIS/M

Edited by - philrobeson on 06/06/2011 07:43:59 AM
Go to Top of Page

jhicks
Here To Stay

USA
283 Posts
Status: offline

Posted - 06/09/2011 :  4:44:52 PM  Show Profile  Visit jhicks's Homepage  Reply with Quote
When you run the rename method pipe it to Out-Null,or even better is to assign it to a variable

$change=$AdminAccount.rename("ADMIN")

Now you can check the return value and anything other than 0 indicates a problem.
if ($change.returnValue -ne 0) {
Write-Warning "Failed to rename account. Return value is $($change.returnValue)"
}

Jeffery Hicks
Windows PowerShell MVP

http://jdhitsolutions.com.blog
http://twitter.com/JeffHicks
http://www.ScriptingGeek.com
Now Available: Managing Active Directory with Windows PowerShell: TFM 2nd ed.
Go to Top of Page

philrobeson
Here To Stay

USA
256 Posts
Status: offline

Posted - 06/10/2011 :  07:29:35 AM  Show Profile  Reply with Quote
Much thanks, Jeff!

Phil Robeson
MIS/M
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Mark Minasi's Reader Forum © 2002-2011 Mark Minasi Go To Top Of Page
This page was generated in 0.12 seconds. Snitz Forums 2000