I have the following script which queries a remote users SID:
$target = Read-Host "Enter the address of the target machine"
$global:user = Read-Host "Enter the username to be added to the ACL"
$cred = Get-Credential
$sid = Invoke-Command -ComputerName $target -Credential $cred -ScriptBlock {
function get-sid
{
Param ( $DSIdentity )
$ID = new-object System.Security.Principal.NTAccount($DSIdentity)
return $ID.Translate( [System.Security.Principal.SecurityIdentifier] ).toString()
}
get-sid $global:user
}
$sid
However, for some reason the $global:user is not accessible inside the function. It throw this error:
A constructor was not found. Cannot find an appropriate constructor for type System.Security.Principal.NTAccount.
+ CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand
+ PSComputerName : target-win
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : target-win
If I remove the variable in the function and simply put get-sid "TheActualUserName"
then the script works and the sid is returned. What am I missing? Is 'global' not being used? Why not.