PoshCode Logo PowerShell Code Repository

ConvertTo-Hex by S-1-5-21-2398571 4 years ago
View followups from luisasd, dsfdsf, S-1-5-21-1608768, leonk and S-1-5-21-1176097 | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2038"></script>download | new post

This scConvertTo-Hex S-1-5-21-357043131-537017027-1947940980-1289ript will convert a security identifier (SID) in string format to its hexadecimal equivalent. e.g.

010500000000000515000000BB0B4815C33A022074381B7409050000

  1. # Ported from C# technique found here: http://forums.asp.net/p/1298956/2529558.aspx
  2. param ( [string]$SidString )
  3.  
  4. # Create SID .NET object using SID string provided
  5. $sid = New-Object system.Security.Principal.SecurityIdentifier $sidstring
  6.  
  7. # Create a byte array of the proper length
  8. $sidBytes = New-Object byte[] $sid.BinaryLength
  9.  
  10. #Convert to bytes
  11. $sid.GetBinaryForm( $sidBytes, 0 )
  12.  
  13. # Iterate through bytes, converting each to the hexidecimal equivalent
  14. $hexArr = $sidBytes | ForEach-Object { $_.ToString("X2") }
  15.  
  16. # Join the hex array into a single string for output
  17. $hexArr -join ''

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me