Sign in to follow this  
Followers 0
dnix

rename computer not working (first post / be gentle)

3 posts in this topic

Hello all. I am trying to rewrite an existing VBscript in AutoIT that renames workstations during a new build process. The script seemes to work fine on an XP machine, but does not work on a Vista machine. I am not getting any errors, the script runs and just doesnt work. Heres the code:

; - New computer name exists in buildomatic.txt file line #10 formatted like so:

; [newpcname]computername

;

; Set error handler

$fErrors = ObjEvent("AutoIt.Error","err_handle")

$wbemFlagReturnImmediately = 0x10

$wbemFlagForwardOnly = 0x20

$colItems = ""

$strComputer = "localhost"

;$newname = "loudwn5510t60b"

;

; Open BuildoMatic.txt file

$File = FileOpen("C:\Windows\Options\Build\Buildomatic.txt", 0)

if $File = -1 Then

MsgBox(0,"Error","BuildoMatic.txt File not found!" & @CRLF & "Script cannot continue.")

Exit

EndIf

; Read the three lines we need

$ClientIDLine = FileReadLine($File,10)

; Close the file

FileClose($File)

; If we have an error of any sort, it's because the file didn't read correctly somehow

if @error = -1 Then

MsgBox(0,"Error","Buildomatic.txt is missing data for ClientID or is corrupt" & @CRLF & "Script cannot continue.")

Exit

EndIf

; Check to make sure the line that should contain Tech ID data really does...

if not StringInStr($ClientIDLine,"[newpcname]") Then

MsgBox(0,"Error","Buildomatic.txt is missing data for ClientID or is corrupt" & @CRLF & "Script cannot continue.")

Exit

EndIf

; Break out the Client ID from the raw input line

$PosTI = StringInStr($ClientIDLine,"]") + 1

$newname = StringMid($ClientIDLine,$PosTI)

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then

For $objComputer in $colItems

$err = $objComputer.Rename($newName)

Next

Else

Msgbox(0,"<!> Build Error <!>","COMPUTER NOT RENAMED - No WMI Objects Found for class: " & "Win32_ComputerSystem" )

Endif

Func err_handle()

$HexNumber = hex($fErrors.number,8)

Msgbox(0,"","Error Intercept !" & @CRLF & _

"Error Number is: " & $HexNumber & @CRLF & _

"Line Number is: " & $fErrors.scriptline & @CRLF & _

"Global Description is: " & $fErrors.description & @CRLF & _

"Windows Description is: " & $fErrors.windescription )

SetError(1)

Endfunc

Share this post


Link to post
Share on other sites



; If we have an error of any sort, it's because the file didn't read correctly somehow
if @error = -1 Then
MsgBox(0,"Error","Buildomatic.txt is missing data for ClientID or is corrupt" & @CRLF & "Script cannot continue.")
Exit
EndIf

Checking @error after FileClose is not common and -1 is not what FileClose is document as a value that @error can have. So I think it is redundant code as you already checked at the point of using FileOpen to check the handle. Your next check seems better so that block of code above is redundant?

Since this works in XP, then try adding #requireadmin at the top of the script and try it again as you may not have admin powers under Vista perhaps when executing it and I would consider you need admin powers.

And consider using tags. Use the tag at the start of your code and the other tag at the end of the code.

:)

Share this post


Link to post
Share on other sites

It actually ended up being something I cant exactly find any documentation for. Apparently with Vista you have to pass in a password and account name parm with the WMI rename verb:

In Vista this doesnt work:

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
For $objComputer in $colItems
 $err = $objComputer.Rename($newname)
Next

But this does work:

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
For $objComputer in $colItems
 $err = $objComputer.Rename($newname,"<passwordhere>","<accountnamehere>")
Next

I was only able to track this down when I added a msgbox to display the return value of $err, at which point I started seeing errors indictaing password was incorrect. Oddly enough both versions work on XP.

>.<

Share this post


Link to post
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
Sign in to follow this  
Followers 0