Public/set-AllegisSnowUsername.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
function set-AllegisSnowUsername { Param([Parameter(Mandatory = $true)] [string]$snowEnv, [pscredential]$snowCred, [ValidateLength(32,32)] [string]$usersysid, [ValidatePattern("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|`"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*`")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])")] [string]$upn ) $uid=$upn.Substring(0,$upn.IndexOf('@')) $snowtable='sys_user' $user = $snowcred.username $pass = $snowcred.GetNetworkCredential().password $method = 'put' $uri = "https://$snowEnv.service-now.com/api/now/table/$($snowtable)/$usersysid" $command="`$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes((""{0}:{1}"" -f '$user', '$pass'))) `$headers = New-Object ""System.Collections.Generic.Dictionary[[String],[String]]"" `$headers.Add('Authorization',('Basic {0}' -f `$base64AuthInfo)) `$headers.Add('Accept','application/json') `$body=`"{'u_nick_name':'$uid','email':'$upn','user_name':'$upn'}`" `$response=Invoke-RestMethod -Headers `$headers -Method '$method' -Uri '$uri' -UseBasicParsing -FollowRelLink -body `$body #-MaximumFollowRelLink 2 return `$response.result | convertto-json " $bytes = [System.Text.Encoding]::Unicode.GetBytes($command) $encodedCommand = [Convert]::ToBase64String($bytes) $results=& 'C:\Program Files\PowerShell\6\pwsh.exe' -encodedcommand $encodedCommand $json=$results | convertfrom-json return $json } |