Jangale V-S <[EMAIL PROTECTED]> writes:

> class Win32_Trustee : Win32_MethodParameterClass
> {
>   string Domain;
>   string Name;
>   uint8 SID[];
>   uint32 SidLength;
>   string SIDString;
> };
> 
> How to get the parameter SID for a user in a domain ?
> 
> I can get parameter SIDString for a user, but not able to get it as uint8
> array !!

If I understand you correctly, you want to convert from a string
representation of a SID to a binary one.

The easiest way, I think, is just to look up the Win32_SID object by
name, like this:

  my $sid = 'S-1-1-0';
  my $sid_obj = Win32::OLE->GetObject ("WinMgmts:Win32_SID.SID=\"$sid\"");

...then $sid_obj->{'BinaryRepresentation'} will return a reference to
the array you seek.

More complete example code is attached.  Call it with two arguments, a
domain and a username, and it will show you the text and binary
representations for the SID of that user.

 - Pat

use warnings;
use strict;
use Win32::OLE;

# Take two arguments: Domain and username.
scalar @ARGV == 2
    or die "Usage: $0 <username>\n";
my ($domain, $username) = @ARGV;

# Bomb out completely if COM engine encounters any trouble.
Win32::OLE->Option ('Warn' => 3);

# Look up user account by domain and username.  See:
# http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_useraccount.asp
my $user = Win32::OLE->GetObject
    ("WinMgmts:Win32_UserAccount.Domain=\"$domain\",Name=\"$username\"");

my $sid = $user->{'SID'};

print "User $domain\\$username has SID $sid\n";

# Now look up SID object using SID string representation.  See:
# http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_sid.asp
my $sid_obj = Win32::OLE->GetObject ("WinMgmts:Win32_SID.SID=\"$sid\"");

print 'Binary representation:';

my @binary_rep = @{$sid_obj->{'BinaryRepresentation'}};
foreach my $i (0 .. ($sid_obj->{'SidLength'} - 1)) { 
    printf " %02x", $binary_rep[$i];
}
print "\n";

Reply via email to