#158079 - 2006-03-02 11:06 AM
ChangeVLKey() - Test Function
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1893
Loc: Hilversum, The Netherlands
|
I've created a function that will change the Volume License Key on a local Computer. Any input on this function would be apreciated.
Code:
Function ChangeVLKey($VOL_PROD_KEY) Dim $obj, $objects, $x ;Key is without hyphens (ABCDEFGHIJKLMNOPQRSTUVWXY) $objects = GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("win32_WindowsProductActivation") If @Error <> 0 Exit @Error EndIf For Each $obj in $objects $x = $obj.SetProductKey($VOL_PROD_KEY) $ChangeVLKey = @error Next Exit @error EndFunction
[edit] Edited per Shawn's & NTDOC's suggestions. [/edit]
Edited by apronk (2006-03-09 08:43 AM)
|
Top
|
|
|
|
#158081 - 2006-03-02 03:00 PM
Re: ChangeVLKey() - Test Function
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1893
Loc: Hilversum, The Netherlands
|
Ah ok, I thought you'd have to actually put "return" in there for it to return the value (Just mIRC scripting habit I guess ). Thanks
|
Top
|
|
|
|
#158084 - 2006-03-02 03:13 PM
Re: ChangeVLKey() - Test Function
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1893
Loc: Hilversum, The Netherlands
|
It works good, the way you said it should be. I wanted the function to return an error code when the function completes it's task, 0 for success should be handled ok now by "$changeVLKey = @error" as well as an error code when it fails it's task.
for instance: Code:
$gimme_answer = ChangeVLKey("ABCDEFGHIJKLMNOPQRSTUVWX") ? $gimme_answer
|
Top
|
|
|
|
#158085 - 2006-03-02 03:17 PM
Re: ChangeVLKey() - Test Function
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Ok thats fair - then I would make the following changes ...
Code:
Function ChangeVLKey($VOL_PROD_KEY) Dim $obj, $objects, $result ;Key is without hyphens (ABCDEFGHIJKLMNOPQRSTUVWXY)
$objects = GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("win32_WindowsProductActivation") $ChangeVLKey = @error For Each $obj in $objects $result = $obj.SetProductKey($VOL_PROD_KEY) $ChangeVLKey = @error Next EndFunction
Basically adding this line after the GetObject:
$ChangeVLKey = @error
This statement kills two birds with one stone
1) It initializes the return code to "something", which it wasn't doing before
2) It will catch a failure on the GetObject and return the code.
|
Top
|
|
|
|
#158086 - 2006-03-02 03:17 PM
Re: ChangeVLKey() - Test Function
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1893
Loc: Hilversum, The Netherlands
|
I could do: Code:
If @error <> 0 exit @error Else $ChangeVLKey = $result EndIf
Edited by apronk (2006-03-02 03:18 PM)
|
Top
|
|
|
|
#158088 - 2006-03-02 03:34 PM
Re: ChangeVLKey() - Test Function
|
Benny69
Moderator
   
Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
|
gave your script a test run and i got back a -2147352567 what does that mean?
|
Top
|
|
|
|
#158090 - 2006-03-02 03:47 PM
Re: ChangeVLKey() - Test Function
|
Benny69
Moderator
   
Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
|
if its ubiquitous what is the point of checking for an error?
|
Top
|
|
|
|
#158093 - 2006-03-02 03:58 PM
Re: ChangeVLKey() - Test Function
|
Benny69
Moderator
   
Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
|
apronk got it right i think because i tried it with a retail lic. if that is the case then can the script be modified to work with retail?
|
Top
|
|
|
|
#158095 - 2006-03-02 05:07 PM
Re: ChangeVLKey() - Test Function
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1893
Loc: Hilversum, The Netherlands
|
I've edited the function in the first post to get better error msg's. Try using it as this: Code:
$msg = ChangeVLKey("ABCDEFGHIJKLMNOPQRSTUVWXY") ? $msg
If the function exit's with an error it is because of the GetObject. If the function complete's you get the msg that staes wether it was placed or not.
|
Top
|
|
|
|
#158096 - 2006-03-02 05:15 PM
Re: ChangeVLKey() - Test Function
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
As the final thing you should Code:
Exit @ERROR
or Code:
Exit $ChangeVLKey
So that any error is also preserved in @ERROR when the function returns.
Code:
Function ChangeVLKey($VOL_PROD_KEY) Dim $obj, $objects, $result ;Key is without hyphens (ABCDEFGHIJKLMNOPQRSTUVWXY) $objects = GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("win32_WindowsProductActivation") If Not @ERROR For Each $obj in $objects $result = $obj.SetProductKey($VOL_PROD_KEY) Next EndIf $ChangeVLKey = @error Exit $ChangeVLKey EndFunction
|
Top
|
|
|
|
#158097 - 2006-03-02 05:15 PM
Re: ChangeVLKey() - Test Function
|
Benny69
Moderator
   
Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
|
ok, the error is not because it can't get the object.
|
Top
|
|
|
|
#158098 - 2006-03-02 05:20 PM
Re: ChangeVLKey() - Test Function
|
Benny69
Moderator
   
Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
|
ok, this is what i am running: Code:
$Return = ChangeVLKey("VP6K44HMRQ86W8YH78DXD2YBQ"); HP1 ;$Return = ChangeVLKey("W789C42CGQGPXHRHV4DJCMG4Y"); HP2 ;$Return = ChangeVLKey("HPMV67JGMXWMJXRHT3JYGYB4Q"); HP3
? $Return Get $a
Function ChangeVLKey($VOL_PROD_KEY) Dim $obj, $objects, $result ;Key is without hyphens (ABCDEFGHIJKLMNOPQRSTUVWXY) $objects = GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("win32_WindowsProductActivation") If Not @ERROR For Each $obj in $objects $result = $obj.SetProductKey($VOL_PROD_KEY) $ChangeVLKey = @error ; ? "ActivationRequired: " + $obj.ActivationRequired ; ? "Caption: " + $obj.Caption ; ? "Description: " + $obj.Description ; ? "IsNotificationOn: " + $obj.IsNotificationOn ; ? "ProductID: " + $obj.ProductID ; ? "RemainingEvaluationPeriod: " + $obj.RemainingEvaluationPeriod ; ? "RemainingGracePeriod: " + $obj.RemainingGracePeriod ; ? "ServerName: " + $obj.ServerName ; ? "SettingID: " + $obj.SettingID Next EndIf $ChangeVLKey = @error Exit $ChangeVLKey EndFunction
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 438 anonymous users online.
|
|
|