This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
Sometimes you look way to far to get something,
I was searching for getting a SecurityPrincipal, from AD (see former posts)
I could not find it (I can get the Owner of the AD-Object as securityPrincipal, but that is Domain\Administrators.)
the trick was just declare them :
$Account = new-object system.security.principal.ntaccount("User")
or
$Account = new-object system.security.principal.ntaccount ("Domain", "User")
(this will not fail if user does not exists, error only comes when You use it)
to get the SID for this Account :
$SID = $Account.translate([system.security.principal.securityidentifier])
(if you get no error your user DOES exist ;-) so this is a good way to check if you have a valid user.
so now I can realy use my set_Owner function :
MSH C:\> $Account = new-object system.security.principal.ntaccount("administrators")
MSH C:\> (gi test.txt).set_owner($Account)
MSH C:\> (gi test.txt).get_owner($Account)
Value-----BUILTIN\Administrators
MSH C:\> $nt = new-object system.security.principal.ntaccount("foo")
MSH C:\> (gi test.txt).set_owner($nt)
Exception calling "Set_Owner" with "1" argument(s): "Exception calling "SetOwner" with "1" argument(s): "Some or all identity references could not be translated.".".At line:1 char:24+ (gi test.txt).set_owner( <<<< $nt)
gr /\/\o\/\/
PS if you are admin you can GiveOwnership to any account you want with this.