Announcement

Collapse
No announcement yet.

Convert MS Scripting to PBWin9

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Ralph Tuenker
    replied
    Thanks José for looking up the code again.

    Leave a comment:


  • José Roca
    replied
    I did a mistake in the declare for ADsGetObject

    Code:
    DECLARE FUNCTION ADsGetObject LIB "Activeds.dll" ALIAS "ADsGetObject" ( _
       BYVAL lpszPathName AS DWORD _                        ' __in  LPCWSTR lpszPathName
     , BYREF riid AS GUID _                                 ' __in  REFIID  riid
     , BYREF ppObject AS ANY _                              ' __out VOID**  ppObject
     ) AS LONG                                              ' HRESULT
    and used as follows:

    Code:
       strComputer = UCODE$("WinNT://atl-ws-01")
       hr = ADsGetObject(STRPTR(strComputer), $IID_IADsContainer, colAccounts)

    Leave a comment:


  • Ralph Tuenker
    replied
    Thanks to José for giving me the right hints!

    I made a little sample for creating a user, adding the user to a group, removing the user from group and deleting the user from local computer.
    Note: You cannot use the Dot (.) for the local computer when dealing with groups, because the resulting ADsPath becomes invalid.

    The solution is slow, but works. If you are familiar with the API, then your program will run x times faster!

    You will find complete code samples in the source code forum:
    http://www.powerbasic.com/support/pb...381#post318381

    Code:
    #COMPILER PBWIN 9
    #COMPILE EXE
    #DIM ALL
    $IID_IADsContainer = GUID$("{001677D0-FD16-11CE-ABC4-02608C9E7553}")
    $IID_IADsGroup = GUID$("{27636B00-410F-11CF-B1FF-02608C9E7553}")
    $IID_IADsUser = GUID$("{3E37E320-17E2-11CF-ABC4-02608C9E7553}")
    
    DECLARE FUNCTION ADsGetObject LIB "Activeds.dll" ALIAS "ADsGetObject" ( _
    	BYVAL lpszPathName AS DWORD _                       	 ' __in  LPCWSTR lpszPathName
    	, BYREF riid AS GUID _                                 ' __in  REFIID  riid
    	, BYREF ppObject AS ANY _                              ' __out VOID**  ppObject
    	) AS LONG                                              ' HRESULT
    
    DECLARE Function ADs_CreateUser (strComputer AS STRING, strUser AS STRING, strPassword AS STRING) AS LONG
    DECLARE FUNCTION ADs_AddUserToGroup (strComputer AS STRING, strUser AS STRING, strGroup AS STRING) AS LONG
    DECLARE FUNCTION ADs_DeleteUser (strComputer AS STRING, strUser AS STRING) AS LONG
    DECLARE FUNCTION ADs_RemoveUserFromGroup (strComputer AS STRING, strUser AS STRING, strGroup AS STRING) AS LONG
    
    FUNCTION PBMAIN () AS LONG
    
    	IF ADs_CreateUser ("testcomp", "test", "12345678") = 0 THEN
    		IF ADs_AddUserToGroup ("testcomp", "test", "Administrators") = 0 THEN
    			MSGBOX "User created and added to group"
    		ELSE
    			MSGBOX "Could not add user to group"
    		END IF
    	ELSE
    		MSGBOX "Could not create user"
    	END IF
    
    	IF ADs_RemoveUserFromGroup ("testcomp", "test", "Administrators") = 0 THEN
    		MSGBOX "User removed from group"
    	ELSE
    		MSGBOX "Could not remove user from group"
    	END IF
    
    	IF ADs_DeleteUser ("testcomp", "test") = 0 THEN
    		MSGBOX "User deleted"
    	ELSE
    		MSGBOX "Could not delete user"
    	END IF
    
    END FUNCTION
    
    Function ADs_CreateUser (strComputer AS STRING, strUser AS STRING, strPassword AS STRING) AS LONG
    	LOCAL hr AS LONG
    	LOCAL colAccounts AS Dispatch
    	LOCAL objUser AS Dispatch
    	LOCAL v1, v2, v3 AS VARIANT
    	LOCAL strADsPath AS STRING
    
    	v1 = "user" : v2 = strUser : v3 = strPassword
    	
    	strADsPath = UCODE$("WinNT://" & strComputer & "")
    	hr = ADsGetObject(STRPTR(strADsPath), $IID_IADsContainer, colAccounts)
    	IF hr <> 0 THEN
    		FUNCTION = hr
    		EXIT FUNCTION
    	END IF
    	
    	OBJECT CALL colAccounts.Create(v1, v2) TO objUser
    	IF ISFALSE(ISOBJECT(objUser)) THEN
    		FUNCTION = -1
    		EXIT FUNCTION
    	END IF
    
    	OBJECT CALL objUser.SetPassword(v3)
    	IF ISFALSE(ISOBJECT(objUser)) THEN 
    		FUNCTION = -2
    		EXIT FUNCTION
    	END IF
    	
    	OBJECT CALL objUser.SetInfo
    	IF ISFALSE(ISOBJECT(objUser)) THEN
    		FUNCTION = -3
    		EXIT FUNCTION
    	END IF
    	
    	SET objUser = NOTHING
    	SET colAccounts = NOTHING
    	
    	FUNCTION = 0
    
    END FUNCTION
    
    
    FUNCTION ADs_AddUserToGroup (strComputer AS STRING, strUser AS STRING, strGroup AS STRING) AS LONG
    	LOCAL hr AS LONG
    	LOCAL objGroup AS Dispatch
    	LOCAL objUser AS Dispatch
    	LOCAL v1 AS VARIANT
    	LOCAL strADsPath AS STRING
    
    	strADsPath = UCODE$("WinNT://" & strComputer & "/" & strGroup & ",group")
    	hr = ADsGetObject(STRPTR(strADsPath), $IID_IADsGroup, objGroup)
    	IF hr <> 0 THEN
    		FUNCTION = hr
    		EXIT FUNCTION
    	END IF
    
    	strADsPath = UCODE$("WinNT://" & strComputer & "/" & strUser & ",user")
    	hr = ADsGetObject(STRPTR(strADsPath), $IID_IADsUser, objUser)
    	IF hr <> 0 THEN
    		FUNCTION = hr
    		EXIT FUNCTION
    	END IF
    
    	OBJECT GET objUser.ADsPath TO v1
    	IF ISFALSE(ISOBJECT(objGroup)) THEN 
    		FUNCTION = -1
    		EXIT FUNCTION
    	END IF
    	
    	OBJECT CALL objGroup.Add(v1)
    	IF ISFALSE(ISOBJECT(objGroup)) THEN 
    		FUNCTION = -1
    		EXIT FUNCTION
    	END IF
    
    	SET objUser = NOTHING
    	SET objGroup = NOTHING
    	
    	FUNCTION = 0
    
    END FUNCTION
    
    FUNCTION ADs_RemoveUserFromGroup (strComputer AS STRING, strUser AS STRING, strGroup AS STRING) AS LONG
    	LOCAL hr AS LONG
    	LOCAL objGroup AS Dispatch
    	LOCAL objUser AS Dispatch
    	LOCAL v1 AS VARIANT
    	LOCAL strADsPath AS STRING
    
    	strADsPath = UCODE$("WinNT://" & strComputer & "/" & strGroup & ",group")
    	hr = ADsGetObject(STRPTR(strADsPath), $IID_IADsGroup, objGroup)
    	IF hr <> 0 THEN
    		FUNCTION = hr
    		EXIT FUNCTION
    	END IF
    
    	strADsPath = UCODE$("WinNT://" & strComputer & "/" & strUser & ",user")
    	hr = ADsGetObject(STRPTR(strADsPath), $IID_IADsUser, objUser)
    	IF hr <> 0 THEN
    		FUNCTION = hr
    		EXIT FUNCTION
    	END IF
    
    	OBJECT GET objUser.ADsPath TO v1
    	IF ISFALSE(ISOBJECT(objGroup)) THEN 
    		FUNCTION = -1
    		EXIT FUNCTION
    	END IF
    	
    	OBJECT CALL objGroup.Remove(v1)
    	IF ISFALSE(ISOBJECT(objGroup)) THEN 
    		FUNCTION = -1
    		EXIT FUNCTION
    	END IF
    
    	SET objUser = NOTHING
    	SET objGroup = NOTHING
    	
    	FUNCTION = 0
    
    END FUNCTION
    
    
    FUNCTION ADs_DeleteUser (strComputer AS STRING, strUser AS STRING) AS LONG
    	LOCAL hr AS LONG
    	LOCAL objComp AS Dispatch
    	LOCAL v1, v2 AS VARIANT
    	LOCAL strADsPath AS STRING
    
    	v1 = "user" : v2 = strUser
    	
    	strADsPath = UCODE$("WinNT://" & strComputer & "")
    	hr = ADsGetObject(STRPTR(strADsPath), $IID_IADsContainer, objComp)
    	IF hr <> 0 THEN
    		FUNCTION = hr
    		EXIT FUNCTION
    	END IF
    
    	OBJECT CALL objComp.Delete (v1, v2)
    	IF ISFALSE(ISOBJECT(objComp)) THEN
    		FUNCTION = -1
    		EXIT FUNCTION
    	END IF
    
    	SET objComp = NOTHING
    	
    	FUNCTION = 0
    
    END FUNCTION
    Last edited by Ralph Tuenker; 6 Jul 2009, 07:41 AM. Reason: Declaration and usage corrected, see posting of José Roca. Added link to Source Code forums.

    Leave a comment:


  • José Roca
    replied
    Using direct interface calls and my include files, it will be:

    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "AdsHlp.inc"
    
    FUNCTION PBMAIN () AS LONG
    
       LOCAL hr AS LONG
       LOCAL strComputer AS STRING
       LOCAL colAccounts AS IADsContainer
       LOCAL objUser AS IADsUser
       
       strComputer = "atl-ws-01"
       hr = ADsGetObject("WinNT://" & strComputer & "", $IID_IADsContainer, colAccounts)
       objUser = colAccounts.Create(UCODE$("user"), UCODE$("Admin2"))
       objUser.SetPassword UCODE$("09iu%4et")
       objUser.SetInfo
    
    END FUNCTION

    Leave a comment:


  • José Roca
    replied
    Something like this (I don't have Active Directory installed, so I can't test):

    Code:
    $IID_IADsContainer = GUID$("{001677D0-FD16-11CE-ABC4-02608C9E7553}")
    
    DECLARE FUNCTION ADsGetObject LIB "Activeds.dll" ALIAS "ADsGetObject" ( _
       BYREF lpszPathName AS ASCIIZ _                       ' __in  LPCWSTR lpszPathName
     , BYREF riid AS GUID _                                 ' __in  REFIID  riid
     , BYREF ppObject AS ANY _                              ' __out VOID**  ppObject
     ) AS LONG                                              ' HRESULT
    
    FUNCTION PBMAIN () AS LONG
    
       LOCAL hr AS LONG
       LOCAL strComputer AS STRING
       LOCAL colAccounts AS Dispatch
       LOCAL objUser AS Dispatch
       LOCAL v1, v2 AS VARIANT
       
       strComputer = "atl-ws-01"
       hr = ADsGetObject("WinNT://" & strComputer & "", $IID_IADsContainer, colAccounts)
       v1 = "user" : v2 = "Admin2"
       OBJECT CALL colAccounts.Create(v1, v2) TO objUser
       v1 = "09iu%4et"
       OBJECT CALL objUser.SetPassword(v1)
       OBJECT CALL objUser.SetInfo
    
    END FUNCTION

    Leave a comment:


  • Peter Redei
    replied
    Try this

    strComputer = "atl-ws-01"
    Set colAccounts = GetObject(UCODE$("WinNT://" & strComputer ))
    Set objUser = colAccounts.Create(UCODE$("user"), UCODE$("Admin2"))
    objUser.SetPassword UCODE$("09iu%4et")
    objUser.SetInfo

    Peter Redei

    Leave a comment:


  • Ralph Tuenker
    started a topic Convert MS Scripting to PBWin9

    Convert MS Scripting to PBWin9

    You will find complete code samples in the source code forum:
    http://www.powerbasic.com/support/pb...381#post318381


    I like to convert MS Scriptings (VBScript) into PBWin and reading PB docs everything seems to be so easy. But I have no success up to now. Can somebody give me a sample how to convert the following into late binding:

    strComputer = "atl-ws-01"
    Set colAccounts = GetObject("WinNT://" & strComputer & "")
    Set objUser = colAccounts.Create("user", "Admin2")
    objUser.SetPassword "09iu%4et"
    objUser.SetInfo

    Thx.
    Last edited by Ralph Tuenker; 6 Jul 2009, 07:42 AM. Reason: Added link to Source Code Forums.
Working...
X