Page 1 of 3 123>
Topic Options
#158079 - 2006-03-02 11:06 AM ChangeVLKey() - Test Function
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1889
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
#158080 - 2006-03-02 02:50 PM Re: ChangeVLKey() - Test Function
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Looks really good to me - well written. Just one comment in regards to syntax though ... this last line:

Return $ChangeVLKey

You might was well just remove the $ChangeVLKey altogether - the Return statement doesn't take arguments. The only reason that $ChangeVLKey didn't dump its contents to the console is because the script returned before it had a chance to.

Top
#158081 - 2006-03-02 03:00 PM Re: ChangeVLKey() - Test Function
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1889
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
#158082 - 2006-03-02 03:04 PM Re: ChangeVLKey() - Test Function
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
well hold the phone now - as it stands, your returning the error code here:

$ChangeVLKey = @error

What is it you figure you want to return ? If you want to return the error code, I would make a slight addition to what you have. As well, it is possible to return a string AND an error code (in @ERROR), all depends on what you want to do.

Top
#158083 - 2006-03-02 03:11 PM Re: ChangeVLKey() - Test Function
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Exit @Error to return it.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#158084 - 2006-03-02 03:13 PM Re: ChangeVLKey() - Test Function
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1889
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 Offline
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_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1889
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
#158087 - 2006-03-02 03:20 PM Re: ChangeVLKey() - Test Function
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1889
Loc: Hilversum, The Netherlands
Aha, yes I agree. That would be the best option.
Edited the first post to reflect your modification.

Top
#158088 - 2006-03-02 03:34 PM Re: ChangeVLKey() - Test Function
Benny69 Offline
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?
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#158089 - 2006-03-02 03:43 PM Re: ChangeVLKey() - Test Function
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Thats the ubiquitous COM message that means "an error has occurred".
Top
#158090 - 2006-03-02 03:47 PM Re: ChangeVLKey() - Test Function
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
if its ubiquitous what is the point of checking for an error?
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#158091 - 2006-03-02 03:50 PM Re: ChangeVLKey() - Test Function
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1889
Loc: Hilversum, The Netherlands
Well either it didn't get the object or it couln't be set.
Please keep in mind that it changes Volume License Keys. Not Retail or OEM Keys.

Top
#158092 - 2006-03-02 03:55 PM Re: ChangeVLKey() - Test Function
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Well, i would seem that checking for errors did indeed serve its purpose - you got an error (for some reason) and I assume apronk isn't getting one. Problem is - whats the cause of your error?
Top
#158093 - 2006-03-02 03:58 PM Re: ChangeVLKey() - Test Function
Benny69 Offline
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?
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#158094 - 2006-03-02 05:06 PM Re: ChangeVLKey() - Test Function
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1889
Loc: Hilversum, The Netherlands
That is a good question, but to rule out the first case could you place an "? $ChangeVLKey" after the first "$ChangeVLKey = @error" ?

If that one is 0 then we know for sure that it is because of the Retail version that this function isn't working.

Top
#158095 - 2006-03-02 05:07 PM Re: ChangeVLKey() - Test Function
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1889
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 Offline
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 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
ok, the error is not because it can't get the object.
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#158098 - 2006-03-02 05:20 PM Re: ChangeVLKey() - Test Function
Benny69 Offline
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

_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
Page 1 of 3 123>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 61 anonymous users online.
Newest Members
jtpk2022, Rayvenhaus, Insecurity, KGSOFT, fobrien
17791 Registered Users

Generated in 0.073 seconds in which 0.025 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org