I have the following function, which retrieves the current user's SID, displays it in a MessageBox
and then returns the SID value:
function Get-UserSid {
$objUser = New-Object System.Security.Principal.NTAccount($username)
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
MsgBox $strSID.Value
$strSID.Value
}
This appears to work fine at first, but if I call this function from elsewhere, e.g.:
function SecondFunction {
$usersid = Get-UserSid
MsgBox $usersid
}
The SID suddenly has "OK" prepended to it:
Does anyone know why this happens? I assume it has something to do with the "OK" button in the MessageBox
being copied to the return value - but why would it do this?
MsgBox Function:
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
function MsgBox {
param (
[string]$message
)
[System.Windows.Forms.MessageBox]::Show($message)
}
Your MsgBox function is placing the results of the [System.Windows.Forms.MessageBox]::Show($message)
command on the pipeline.
You can either assign it to a variable and ignore it
function Get-UserSid {
$objUser = New-Object System.Security.Principal.NTAccount($username)
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
#Ignore this...
$ignore = MsgBox $strSID.Value
#return the SID
$strSID.Value
}
or pipe it to Out-Null
function Get-UserSid {
$objUser = New-Object System.Security.Principal.NTAccount($username)
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
#Ignore this...
MsgBox $strSID.Value | Out-Null
#return the SID
$strSID.Value
}