#158897 - 2006-03-13 09:43 AM
Help converting VBS code to KiXtat
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11623
Loc: CA
|
Seem to keep having trouble converting this to working KiXtart code.
Can someone convert it to working KiXtart code please.
I keep getting an error passing the flag in the last array when I set it in parameter. Code:
Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000 strComputer = "atl-win2k-01" Set objUser = GetObject("WinNT:// " & strComputer & "/kenmyer ") objUserFlags = objUser.Get("UserFlags") objPasswordExpirationFlag = objUserFlags OR ADS_UF_DONT_EXPIRE_PASSWD objUser.Put "userFlags", objPasswordExpirationFlag objUser.SetInfo
|
Top
|
|
|
|
#158898 - 2006-03-13 09:57 AM
Re: Help converting VBS code to KiXtat
|
Lonkero
KiX Master Guru
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
Code:
$ADS_UF_DONT_EXPIRE_PASSWD = &10000 $strComputer = "atl-win2k-01" $objUser = GetObject("WinNT:// " + $strComputer + "/kenmyer") $objUserFlags = $objUser.Get("UserFlags") $objPasswordExpirationFlag = $objUserFlags | $ADS_UF_DONT_EXPIRE_PASSWD $objUser.Put("userFlags", $objPasswordExpirationFlag) $objUser.SetInfo
I think...
|
Top
|
|
|
|
#158902 - 2006-03-13 01:59 PM
Re: Help converting VBS code to KiXtat
|
Benny69
Moderator
Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
|
doc give this a try: Code:
$objPasswordExpirationFlag = $objUserFlags + $ADS_UF_DONT_EXPIRE_PASSWD
|
Top
|
|
|
|
#158903 - 2006-03-13 02:36 PM
Re: Help converting VBS code to KiXtat
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
doc give this a try: Code:
$objPasswordExpirationFlag = $objUserFlags + $ADS_UF_DONT_EXPIRE_PASSWD
No Doc, don't try that. Stick with the bitwise "|", it's fine.
|
Top
|
|
|
|
#158906 - 2006-03-13 07:20 PM
Re: Help converting VBS code to KiXtat
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11623
Loc: CA
|
Quote:
Quote:
doc give this a try: Code:
$objPasswordExpirationFlag = $objUserFlags + $ADS_UF_DONT_EXPIRE_PASSWD
No Doc, don't try that. Stick with the bitwise "|", it's fine.
Richard, what's up with that? Benny's code worked fine too.
|
Top
|
|
|
|
#158907 - 2006-03-13 07:25 PM
Re: Help converting VBS code to KiXtat
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
If the bit was already set then adding would change other bits as the two values would be summed. You only want to adjust the single bit.
|
Top
|
|
|
|
#158910 - 2006-03-13 09:27 PM
Re: Help converting VBS code to KiXtat
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11623
Loc: CA
|
LOL, well you see I removed that signature already
|
Top
|
|
|
|
#158912 - 2006-03-14 10:07 AM
Re: Help converting VBS code to KiXtat
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Benny asked the same question, so a quick explanation for anyone else who comes across this thread and who is not binary-flag-aware.
It is common practice to store similar flags as bits (on or off) in a single datum.
Using an eight bit field as an example, you have eight possible flags. Their positions and values are: Code:
76543210 -------- 00000001 - 1 00000010 - 2 00000100 - 4 00001000 - 8 00010000 - 16 00100000 - 32 01000000 - 64 10000000 - 128
You can explicitly set the flags in the field just by adding the values together, so to set the flags at positions 1 and 6 you would use 2+64 = 66. However this also sets all the other flags to zero which may not be what you want.
If you use the bitwise OR which is "|" in KiXtart, then only the flags that you specify will be set - the other flags will be left unchanged.
You cannot simply add the flag values to the existing value. If you do this then (as Les says) the flag will "carry" to the next higher position. This is easy to see with an example.
Say you try to set flag 1 four times using addition. This is what would happen: Code:
76543210 -------- 00000000 - Start, add 2 (the value of flag 1) 00000010 - SUCCESS! flag #1 is set. Now add it again. 00000100 - Oh-oh! The flag is unset, and worse flag #2 is set. 00000110 - Success! We've set flag #1 again 00001000 - Oh-oh! Flag#1 is unset and so is flag #2, but now flag #3 is set!
As you can see, it's all turned into a horrible mess.
Even when you are setting bit-fields explicitly it is good programming practice to only use bitwise operators to manipulate them. Not only does this serve as a reminder of what you are changing, but it helps to avoid costly hard to find errors if the field use should later change.
To complete the story, you can safely use simple addition and subtraction to change bit settings, but only if you check the setting first: Code:
; To set the flag: If Not ($objUserFlags & $ADS_UF_DONT_EXPIRE_PASSWD) $objUserFlags=$objUserFlags + $ADS_UF_DONT_EXPIRE_PASSWD EndIf ; To clear a flag If $objUserFlags & $ADS_UF_DONT_EXPIRE_PASSWD $objUserFlags=$objUserFlags - $ADS_UF_DONT_EXPIRE_PASSWD EndIf
|
Top
|
|
|
|
#158915 - 2006-03-14 01:14 PM
Re: Help converting VBS code to KiXtat
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Richard, I think it would be better to be consistent and use the bit-wise NOT (~) operator when turning off a flag instead of subtracting. Code:
; To clear a flag: If $objUserFlags & $ADS_UF_DONT_EXPIRE_PASSWD $objUserFlags=$objUserFlags & ~$ADS_UF_DONT_EXPIRE_PASSWD EndIf
Code:
; To set the flag: If Not ($objUserFlags & $ADS_UF_DONT_EXPIRE_PASSWD) $objUserFlags=$objUserFlags | $ADS_UF_DONT_EXPIRE_PASSWD EndIf
|
Top
|
|
|
|
#158916 - 2006-03-14 01:31 PM
Re: Help converting VBS code to KiXtat
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
Richard, I think it would be better to be consistent and use the bit-wise NOT (~) operator when turning off a flag instead of subtracting.
Yes, the last examples were demonstrations of how to manipulate the bits using simple math instead of bitwise operations, they are not how I would recommend that the operations are performed.
Using the bitwise operations means that you do not need the conditionals at all.
I hope that clarifies things.
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 507 anonymous users online.
|
|
|