Page 1 of 1 1
Topic Options
#177971 - 2007-07-16 11:13 AM help convert vbs in kix
roko Offline
Lurker

Registered: 2006-09-05
Posts: 1
Hi all,

i need help to convert the following vbs script in kix. can anyone please help me.

' ----------------------------------------------------------------------------
'
' This file is part of the Microsoft ADS Samples.
'
' Copyright (c) Microsoft Corporation. All rights reserved.
'
'
' ----------------------------------------------------------------------------


' This sample shows creating a device. Also setting the admin MAC address (in
' class DeviceHWAddrs) and creating a device variable (class DeviceVariables).


Option Explicit
On Error Resume Next
Err.Clear


Dim objSvc ' WMI root object
Dim objDev ' to store devices class object
Dim objDevInst ' to store device instance object
Dim objDevhwaddr ' to store device hwaddr classes object
Dim objDevhwaddrInst ' to store device hwaddr instance object
Dim objDevvar ' to store device device variables class object
Dim objDevvarInst ' to store device variable instance object


' get root microsoft ads object
Set objSvc = GetObject("winmgmts:\\.\root\microsoftADS")
Call CheckForErrors("Failed: getobject root\microsoftads")



' get devices object
Set objDev = objSvc.Get("Devices")
Call CheckForErrors("Failed: get Devices object")

' spawn device instance class
Set objDevInst = objDev.SpawnInstance_()
Call CheckForErrors("Failed: spawning Device instance object")

' set device properties
objDevInst.Name = "sample1.example.com"
objDevInst.Description = "Sample device"

' create device instance
objDevInst.Put_()
Call CheckForErrors("Failed: Put_() couldn't create device")




' get device hwaddr object
Set objDevhwaddr = objSvc.Get("DeviceHWAddrs")
Call CheckForErrors("Failed: get DevicesHWAddrs object")

' create device hwaddr instance
Set objDevhwaddrInst = objDevhwaddr.SpawnInstance_()
Call CheckForErrors("Failed: spawning DeviceHwAddrs instance object")

' set device hwaddrs properties
objDevhwaddrInst.DeviceName = "sample1.example.com"
objDevhwaddrInst.type = 2
objDevhwaddrInst.hwaddr = "1234567890ab"

' create device hwaddr instance
objDevhwaddrInst.Put_()
Call CheckForErrors("Failed: Put_() couldn't create device hwaddr")





' get device variables object
Set objDevvar = objSvc.Get("DeviceVariables")
Call CheckForErrors("Failed: get DevicesVariables object")

' create device variable instance
Set objDevvarInst = objDevvar.SpawnInstance_()
Call CheckForErrors("Failed: get DevicesVariable instance object")

' set device varaible properties
objDevvarInst.DeviceName = "sample1.example.com"
objDevvarInst.Namespace = "user"
objDevvarInst.Name = "rack"
objDevvarInst.Value = "4"

' create device variable instance
objDevvarInst.Put_()
Call CheckForErrors("Failed: Put_() couldn't create device variable")


'********************************************************************
'* Sub: CheckForErrors
'*
'* Purpose: Check if there are any errors returned from the previous
'* function call.
'*
'* Input: [in] strMsg error string to display if there is an error
'*
'* Output: displays the error string to the user
'*
'********************************************************************

Sub CheckForErrors(ByVal strMsg)
Dim strErrDesc ' to store err description
Dim intErrNum ' to store err number
Dim objWbemErr ' swbemlasterror object

If Err.Number Then
intErrNum = Err.number
strErrDesc = Err.Description

If strErrDesc = "" And Err.Number Then
Set objWbemErr = CreateObject("WbemScripting.SWbemLastError")
strErrDesc = objWbemErr.Description
strErrDesc = Replace(strErrDesc, vbCRLF, " ")
End If
Wscript.Echo(strMsg & " failed: " & strErrDesc & " (0x" & CStr(Hex(intErrNum)) & ")")
WScript.Echo("---------")
Wscript.Quit(2) ' 2 is for error, 1 is warning
End If

End Sub

Top
#177973 - 2007-07-16 11:38 AM Re: help convert vbs in kix [Re: roko]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Next time use the [ code ] (without spaces) tags
here you go:

 Code:
; ----------------------------------------------------------------------------
;
; This file is part of the Microsoft ADS Samples.
;
; Copyright (c) Microsoft Corporation. All rights reserved.
;
;
; ----------------------------------------------------------------------------


; This sample shows creating a device. Also setting the admin MAC address (in 
; class DeviceHWAddrs) and creating a device variable (class DeviceVariables).

Dim $objSvc ; WMI root object
Dim $objDev ; to store devices class object
Dim $objDevInst ; to store device instance object
Dim $objDevhwaddr ; to store device hwaddr classes object
Dim $objDevhwaddrInst ; to store device hwaddr instance object
Dim $objDevvar ; to store device device variables class object
Dim $objDevvarInst ; to store device variable instance object

; get root microsoft ads object
Set $objSvc = GetObject("winmgmts:\\.\root\microsoftADS")
CheckForErrors("Failed: getobject root\microsoftads")

; get devices object 
$objDev = $objSvc.Get("Devices")
CheckForErrors("Failed: get Devices object")

; spawn device instance class 
$objDevInst = $objDev.SpawnInstance_()
CheckForErrors("Failed: spawning Device instance object")

; set device properties 
$objDevInst.Name = "sample1.example.com"
$objDevInst.Description = "Sample device"

; create device instance
$objDevInst.Put_()
CheckForErrors("Failed: Put_() couldn't create device")

; get device hwaddr object 
$objDevhwaddr = $objSvc.Get("DeviceHWAddrs")
CheckForErrors("Failed: get DevicesHWAddrs object")

; create device hwaddr instance
$objDevhwaddrInst = $objDevhwaddr.SpawnInstance_()
CheckForErrors("Failed: spawning DeviceHwAddrs instance object")

; set device hwaddrs properties 
$objDevhwaddrInst.DeviceName = "sample1.example.com"
$objDevhwaddrInst.type = 2
$objDevhwaddrInst.hwaddr = "1234567890ab"

; create device hwaddr instance 
$objDevhwaddrInst.Put_()
CheckForErrors("Failed: Put_() couldn't create device hwaddr")

; get device variables object
$objDevvar = $objSvc.Get("DeviceVariables")
CheckForErrors("Failed: get DevicesVariables object")

; create device variable instance
$objDevvarInst = $objDevvar.SpawnInstance_()
CheckForErrors("Failed: get DevicesVariable instance object")

; set device varaible properties 
$objDevvarInst.DeviceName = "sample1.example.com"
$objDevvarInst.Namespace = "user"
$objDevvarInst.Name = "rack"
$objDevvarInst.Value = "4"

; create device variable instance
$objDevvarInst.Put_()
CheckForErrors("Failed: Put_() couldn't create device variable")


;********************************************************************
;* Function: CheckForErrors
;*
;* Purpose: Check if there are any errors returned from the previous
;* function call.
;*
;* Input: [in] strMsg error string to display if there is an error
;*
;* Output: displays the error string to the user
;*
;********************************************************************

Function CheckForErrors($strMsg)
  Dim $strErrDesc ; to store err description
  Dim $intErrNum ; to store err number
  Dim $objWbemErr ; swbemlasterror object

  If @ERROR
    $intErrNum = @ERROR
    $strErrDesc = @SERROR

    If $strErrDesc = "" And @ERROR
      $objWbemErr = CreateObject("WbemScripting.SWbemLastError")
      $strErrDesc = $objWbemErr.Description
      ;$strErrDesc = Replace($strErrDesc, @CRLF, " ") ;Replace isn't a function within kixtart.
    EndIf

    ? $strMsg + " failed: " + strErrDesc + "$intErrNum")
    ? "---------"
    Exit 2 ; 2 is for error, 1 is warning 

  EndIf
EndFunction


Edited by apronk (2007-09-13 01:48 PM)
Edit Reason: BillBarnard's corrections :)

Top
#180495 - 2007-09-13 01:01 AM Re: help convert vbs in kix [Re: Arend_]
BillBarnard Offline
Starting to like KiXtart

Registered: 2007-03-14
Posts: 141
Loc: Leighton Buzzard, Bedfordshire...
Will Call CheckForErrors("Failed: text") work?
Call is for calling external kix files, not user functions.
Better to use:-
$ = CheckForErrors("Failed: text")
Also @ERRROR in the function has an extra R.
_________________________
Bill

Top
#180525 - 2007-09-13 01:48 PM Re: help convert vbs in kix [Re: BillBarnard]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Thx Bill, edited my post with your suggestions \:\)
Translated it so quickly I forgot to look at "Call" \:\)

Top
#180550 - 2007-09-13 05:59 PM Re: help convert vbs in kix [Re: Arend_]
BillBarnard Offline
Starting to like KiXtart

Registered: 2007-03-14
Posts: 141
Loc: Leighton Buzzard, Bedfordshire...
Glad to be of help.
_________________________
Bill

Top
Page 1 of 1 1


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

Who's Online
0 registered and 1539 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.058 seconds in which 0.027 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