Jump to content

Domain Computer Renaming...


Recommended Posts

Scenario: Rename 400+ computers with a "new and improved" naming convention. 15 characters, first 6 are untouchable, Position 7 is "W" for workstation or "L" for laptop, Position 8 is OS, Position 9 is a "-", Positions 10 & 11 are untouchable, and positions 12-15 are all i have to work with.

My approach has been to use AutoIt to check for existence of a battery ("L" if there's a battery, "W" if not) and check for the OS. Put it all together and rename the computer. So far my attempts as reading the WMI for battery existence have been fruitless. i know there are functions i could use to check the WMI but i want to learn how to do it myself.

Func SetVars()
    $mOldName = @Computername           ;Get current computer name
    $mOSVersion = @OSVersion            ;Get computer operating system
    ;***************************************************************************
    $mNamePart1 = "123456"              ;First part of naming convention
    ;***************************************************************************
    ;Second part of naming convention is either "L"aptop or "W"orkstation
    ;If battery exists then computer is a laptop, if not it's a workstation
    
    $objWMIService = ObjGet("winmgmts:\\" & $mOldName & "\root\CIMV2")
    
        MsgBox(0, "None", "$oWMIservice = " & $oWMIservice)
    
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Battery", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    ;$mBattery = $oWMIService.ExecQuery("Win32_Battery", "Availability")    ;"Win32_Battery", "Availability")
        MsgBox(0, "None", "$colItems =" & $colItems)
        
    $mBattery = $objWMIService.ExecQuery("Win32_Battery", "Availability")
    
        MsgBox(0, "None", "$mBattery = " & $mBattery)
        
    If $mBattery = 1 Then
        $mNamePart2 = "L"
    Else
        $mNamePart2 = "W"
    EndIf
    ;***************************************************************************
    ;Third part of naming convention is "1" for XP "2" for Vista or "3" for Win7
    If $mOSVersion = "WIN_XP" Then
        $mNamePart3 = 1
    ElseIf $mOSVersion = "Win_VISTA" Then
        $mNamePart3 = 2
    Elseif $mOSVersion = "Win_7" Then
        $mNamePart3 = 3
    EndIf
    ;***************************************************************************
    ;Fourth part of naming convention is office name
    $mNamePart4 = "-AL"
    ;***************************************************************************
    ;Fifth part of naming convention is a 4 position number assigned by Admin
    $mNamePart5 = "0000"
    ;***************************************************************************
EndFunc

How do i go about writing the code to check WMI for battery existence? i thought i had it but when i tried on a laptop i didn't get any change.

Thanks,

Dave

Link to comment
Share on other sites

Ok, here's my solution to my problem. Since i have to sit in front of each computer anyway, i'm checking for another program that we have loaded too.

Please look it over and tell me where i can improve.

;********************************************************************************************
; This routine will rename individual computers on the local domain using the Naming
; Convention of Dec 2010.  If the name is too short or too long it will advise the user and
; exit, allowing another attempt at changing the name.
; The routine reads the WMI to get battery information, indicating a laptop;
; reads the WMI to get the computer Serial Number;
; reads the current operating system
; and allows the user to change the Local Designation, according to the latest information
; from the Network Gurus
; It also allows resetting the current computer location and will write the
; ComputerName, SerialNumber, and Location to a text file, incrementing the file as it
; goes along
; Author:  David Wrede
; Date/version: 2011-01-07
; Requires:  AutoIt3
;********************************************************************************************

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <constants.au3>

#RequireAdmin

Opt("GUIOnEventMode", 1)

Global $mNamePart1, $mNamePart2, $mNamePart3, $mNamePart4, $mNamePart5, $mOldName, $mSerNum
Global $mOS, $mBattery, $Output, $wbemFlagReturnImmediately, $wbemFlagForwardOnly
Global $mFile, $mFilePath, $mFileName, $mStringLength, $mLocation, $mFileOpen
Global $mWIMS01, $mWIMS02

SetVars()

#Region ### START Koda GUI section ### Form=e:\apps\autoit3\scripts\form01.kxf
$frmRename = GUICreate("Rename Computer", 500, 453, 601, 147)
GUISetOnEvent($GUI_EVENT_CLOSE, "frmRenameClose")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "frmRenameMinimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "frmRenameMaximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "frmRenameRestore")
;******** Create Form Labels ********
$lblComputer = GUICtrlCreateLabel("Computer", 32, 32, 77, 24)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$lblOS = GUICtrlCreateLabel("OS", 32, 60, 28, 24)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$lblName01 = GUICtrlCreateLabel("Name Part 1", 32, 117, 96, 24)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$lblName02 = GUICtrlCreateLabel("Name Part 2", 32, 145, 96, 24)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$lblName03 = GUICtrlCreateLabel("Name Part 3", 32, 174, 96, 24)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$lblName04 = GUICtrlCreateLabel("Name Part 4", 32, 202, 96, 24)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$lblName05 = GUICtrlCreateLabel("Name Part 5", 32, 231, 96, 24)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$lblBldg_Rm = GUICtrlCreateLabel("Building/Room", 32, 259, 113, 24)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$lblNewname = GUICtrlCreateLabel("New Name", 32, 298, 85, 24)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$lblSerNum = GUICtrlCreateLabel("Serial Number", 32, 89, 110, 24)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
;******** Create Form Fields ********
$txtOldName = GUICtrlCreateInput($mOldname, 184, 25, 275, 28)               ;Show old computer name
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_DISABLE)
$txtOS = GUICtrlCreateInput($mOS, 184, 54, 275, 28)                         ;Show which OS is on the computer
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_DISABLE)
$txtSerNum = GUICtrlCreateInput($mSerNum, 184, 83, 275, 28)                 ;Show the Serial Number from the BIOS
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_DISABLE)
$txtName01 = GUICtrlCreateInput($mNamePart1, 184, 111, 275, 28)             ;Show the first part of the new name
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_DISABLE)
$txtName02 = GUICtrlCreateInput($mNamePart2, 184, 140, 275, 28)             ;Show the second part of the new name
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_DISABLE)
$txtName03 = GUICtrlCreateInput($mNamePart3, 184, 168, 275, 28)             ;Show the third part of the new name
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUICtrlSetState(-1, $GUI_DISABLE)
$txtName04 = GUICtrlCreateInput($mNamepart4, 184, 197, 275, 28)             ;Show  the fourth part of the new name
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$txtName05 = GUICtrlCreateInput("0000", 184, 226, 275, 28)                  ;Create field for last 4 numbers
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$txtBldg_Rm = GUICtrlCreateInput("Input location", 184, 254, 275, 28)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
;$txtNewName = GUICtrlCreateInput("txtNewName", 184, 298, 275, 28)
;GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
;******** Create Action Buttons ********
$btnRename = GUICtrlCreateButton("Rename", 80, 376, 100, 30, $BS_NOTIFY)
GUICtrlSetOnEvent(-1, "btnRenameClick")
$btnCancel = GUICtrlCreateButton("Cancel", 320, 376, 100, 30, $BS_NOTIFY)
GUICtrlSetOnEvent(-1, "btnCancelClick")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func btnCancelClick()
    Exit
EndFunc

Func btnRenameClick()
    $mNamePart4 = GUICtrlRead($txtName04)                                   ;Read unit designation
    $mNamePart5 = GUICtrlRead($txtName05)                                   ;Get unique number from Admin
    $mNewName = $mNamePart1 & $mNamePart2 & $mNamePart3 & $mNamePart4 & $mNamePart5
    $mStringLength = StringLen($mNewName)                                   ;Get length of new computer name
    If $mStringLength < 15  Then                                            ;If new name is not 15 characters, advise user and exit
        MsgBox(16,"Incorrect Length", "Computer Name is too short " & @CRLF & _ 
        "Computer Name must be 15 characters" & @CRLF & @CRLF & "Please try again.")
        Exit
    ElseIf $mStringLength > 15 And <> "Input location" Then
        MsgBox(16, "Incorrect Length", "Computer Name is too long " & @CRLF & _ 
        "Computer Name must be 15 characters" & @CRLF & @CRLF & "Please try again.")
        Exit
    EndIf
    
    $txtNewName = GUICtrlCreateInput($mNewName, 184, 298, 275, 28, -1 , 0)
    GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
    GUICtrlSetColor(-1, 0x000000)
    GUICtrlSetState(-1, $Gui_Disable)

    $mLocation = GUICtrlRead($txtBldg_Rm)                                   ;Get computer location
    If StringLen($mLocation) > 0 Then
        If $mLocation <> "Input location" Then
            RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\lanmanserver\parameters", "srvcomment", "REG_SZ", $mLocation)
            RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters", "srvcomment", "REG_SZ", $mLocation)
        EndIf
    EndIf

    $mFile = FileOpen($mFileName, 1)  ;$mFilePath & $mFileName, 1)
    If $mFile = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

    ReadWIMS()

    FileWrite($mFileName, $mOldName & "    " & $mNewName & "    " & $mSerNum & "    " & $mLocation & "    " & $mWIMS01 & "    " & $mWIMS02 & @CRLF)
    FileClose($mFileName)

    If FileExists("C:\Windows\System32\netdom.exe") Then
        Run(@comspec & " /k netdom renamecomputer " & $mOldName & " /NewName:" & $mNewName & " /Force /REBoot:10", @SystemDir, $STDERR_CHILD + $STDOUT_CHILD)
    Else
        ;FileCopy("J:\CER\Rename\netdom.exe", 
        FileCopy("J:\CER\Rename\netdom.exe", "C:\Windows\System32\netdom.exe")
        Run(@comspec & " /k netdom renamecomputer " & $mOldName & " /NewName:" & $mNewName & " /Force /REBoot:10", @SystemDir, $STDERR_CHILD + $STDOUT_CHILD)
    EndIf

;Requires some work on the NETDOM thing
;Run(@ComSpec & " /c netdom renamecomputer " & $Currentname & " /NewName:" & $NewName & " /ud:wsusad\administrator /PasswordD:user@123 /Force /REBoot:10")

EndFunc

Func frmRenameClose()
    Exit
EndFunc

Func SetVars()
    $mFileName = "\\mafb-san-1\msg\ces\cer\rename\renames.txt"
    $mOldName = @ComputerName
    $mNamePart1 = "52NZAS"                                          ;First part of naming convention
    
    $mBattery = WMIQuery("Availability", "Win32_Battery")

    If $mBattery = 2 Then
        $mNamePart2 = "L"
    Else
        $mNamePart2 = "W"
    EndIf
    ;***********************************************************************
    
    $mOS = @OSVersion                                               ;Third part of naming convention
    If $mOS = "WIN_XP" Then                                         ;If OS is XP then 1
        $mNamePart3 = "1-"
    ElseIf $mOS = "Win_VISTA" Then                                  ;IF OS is Win Vista then 2
        $mNamePart3 = "2-"
    ElseIf $mOS = "Win_7" Then                                      ;If OS is Win 7 then 3
        $mNamepart3 = "3-"
    EndIf
    
    $mNamePart4 = "CE"                                              ;Fourth part of naming convention
    
    ;******** Get Serial Number from BIOS ********
    $Output=""
    $Output = $Output & "Computer: " & @ComputerName  & @CRLF
    $Output = $Output & "==========================================" & @CRLF
    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_BIOS", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) then
        For $objItem In $colItems
            $Output = $Output & "Serial Number: " & $objItem.SerialNumber & @CRLF
            $mSerNum = $objItem.SerialNumber
            $Output=""
        Next
    ;Else
    ;   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_Battery" )
    Endif
EndFunc

Func WMIQuery($mWhat, $mWhere)
    Dim $strQuery, $objEnumerator, $value, $wbemFlagReturnImmediately, $wbemFlagForwardOnly, $Output, $objItem
    $Output = ""
    $strQuery = "Select " & $mWhat & " FROM " & $mWhere
    $objWMIService = ObjGet("winmgmts:\\" & @computername & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery($strQuery, "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
        For $objItem In $colItems
            if @error = 0 and $colItems <> "" Then
                $Output = $Output & $mWhat & $objItem & "." & $mWhere
            Else
                MsgBox(0, "Error", "Error " & @error & "has occurred")
            EndIf
        Next
    Else
        MsgBox(0, "WMI Output", "No WMI Objects found for class: " & $mWhere)
    EndIf
EndFunc

Func ReadWIMS()
    $mWIMS01 = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Sansoft\VUPort Windows Front-end\Authentication", "Activation")
    If StringLen($mWIMS01) = 0 Then
        $mWIMS01 = "No WIMS"
    Else
        $mPart1 = StringMid($mWIMS01, 1, 4)
        $mPart2 = StringMid($mWIMS01, 5, 4)
        $mPart3 = StringMid($mWIMS01, 9, 4)
        $mPart4 = StringMid($mWIMS01, 13, 4)
        $mWIMS01 = $mPart1 & "-" & $mPart2 & "-" & $mPart3 & "-" & $mPart4
    EndIf
    
    $mWIMS02 = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Sansoft\VUPort Windows Front-end\Authentication", "SerialNumber")
    If StringLen($mWIMS02) = 0 Then
        $mWIMS02 = "No WIMS"
    Else
        $mPart1 = StringMid($mWIMS02, 1, 4)
        $mPart2 = StringMid($mWIMS02, 5, 4)
        $mPart3 = StringMid($mWIMS02, 9, 4)
        $mPart4 = StringMid($mWIMS02, 13, 4)
        $mWIMS02 = $mPart1 & "-" & $mPart2 & "-" & $mPart3 & "-" & $mPart4

    EndIf
EndFunc

For some reason my WMIQuery() function would only work on the battery not on the computer name. i fashioned it after a KIXcript and probably didn't get the syntax correct.

It would not write the text file until in put it in UNC format ("\\Share-Name\Directory\Directory\FileName"), i don't know why.

i would like to put the file to write in an .INI file so it could be changed easier.

Thanks for looking.

Dave

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...