| Author |
Topic  |
|
|
philrobeson
Here To Stay
 
USA
256 Posts
Status: offline |
Posted - 06/03/2011 : 1:31:21 PM
|
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
|
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 |
 |
|
|
jhicks
Here To Stay
 
USA
283 Posts
Status: offline |
Posted - 06/09/2011 : 4:44:52 PM
|
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. |
 |
|
|
philrobeson
Here To Stay
 
USA
256 Posts
Status: offline |
Posted - 06/10/2011 : 07:29:35 AM
|
| Much thanks, Jeff! |
Phil Robeson MIS/M |
 |
|
| |
Topic  |
|
|
|