PoshCode Logo PowerShell Code Repository

Convert-StringSID by tojo2000 33 months ago (modification of post by tojo2000 view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1072"></script>download | new post

Converts a string containing the SDDL SID format (e.g. ‘S-1-5-21-39260824-743453154-142223018-195717’) to a Win32_SID WMI object. Also adds a property with the base64 encoded binary SID to match the format used by some AD backup utilities.

  1. function Convert-StringSID {
  2. <#
  3. .Synopsis
  4.   Takes a SID string and outputs a Win32_SID object.
  5.  
  6. .Parameter sidstring
  7.   The SID in SDDL format. Example: S-1-5-21-39260824-743453154-142223018-195717
  8.  
  9. .Description
  10.   Takes a SID string and outputs a Win32_SID object.
  11.   Note: it also adds an extra property, base64_sid, the base64 representation
  12.         of the binary SID.
  13.  
  14. .Example
  15. PS> Convert-StringSID 'S-1-5-21-39260824-743453154-142223018-195717'
  16.  
  17. .Example
  18. PS> $list_of_sids |
  19.       Convert-StringSID |
  20.       %{Write-Output "$($_.ReferenceDomainName)\$($_.AccountName)"}
  21. MYDOMAIN\somename
  22. MYDOMAIN\anotheraccount
  23.  
  24. .Notes
  25.   NAME:      Convert-StringSID
  26.   AUTHOR:    tojo2000
  27. #Requires -Version 2.0
  28. #>
  29.   param([Parameter(Position = 0,
  30.                    Mandatory = $true,
  31.                    ValueFromPipeline = $true]
  32.         [string]$sidstring)
  33.  
  34.   BEGIN {}
  35.  
  36.   PROCESS{
  37.     [wmi]$obj = 'Win32_SID.SID="{0}"' -f $sidstring
  38.     $encoded = [System.Convert]::ToBase64String($obj.BinaryRepresentation)
  39.     $obj |
  40.       Add-Member -MemberType NoteProperty -Name base64_sid -Value $encoded
  41.     Write-Output $obj
  42.   }
  43.  
  44.   END{}
  45. }

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