Sneha
Sneha

Reputation: 133

Need to retain leading zeros in powershell script

I have the script where I am trying to put my AWS credentials in a remote server using powershell script.

Issue I am facing : Leading zeros of the AWS AccountID (one of the powershell script argument) is not getting retained. I am new to PS script. Need guidance on retaining the zeros of the accountID.

Example -- AccountID -- 000009876543 - Here leading zeros are not getting retained and an entry without zeros gets created in the server. However, 900000876543 is handed successfully.

Script -

$user = $args[7]  #userid   
$pass = $args[8]  #password
$pair = "$($user):$($pass)" #pair

$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair) 
$base64 = [System.Convert]::ToBase64String($bytes)

$basicAuthValue = "Basic $base64"

$headers = @{ Authorization = $basicAuthValue }


$c1=$args[0]
$path=$args[1]
$cert=$args[2]
$p1=$args[3]
$awsacct=$args[4] #account_id
$awsak=$args[5]   #access_key 
$awssk=$args[6]   #secret_key 



$body9="{`"project`": { `"href`": `"/tenants/$c1/projects/$p1`"},`"account`": {`"href`": `"/tenants/$c1/accounts/$awsacct`"},`"credential`":{ `"type`": `"key`",`"accessKey`": `""+$awsak+"`", `"secretKey`": `""+$awssk+"`"}}"

$r=Invoke-WebRequest -uri "$path/tenants/$c1/credentials/$awsak" -Headers $headers -Certificate $cert -ContentType application/json -Method PUT -Body $body9

$r.Content

Upvotes: 2

Views: 2645

Answers (2)

Theo
Theo

Reputation: 61068

As commented, if you cannot send the arguments using Orchestrator as string, you can always manipulate the integer value inside the code. In your case, the account_id value is sent to the script as $args[4] and in the process it is converted to integer, removing any padding leading zeros.

Since in your organization you use these id's as 12 digit numbers, you can recreate that by using the String.PadLeft method:

$awsacct = $args[4].ToString().PadLeft(12,"0")

or by using the -f format operator:

$awsacct = '{0:000000000000}' -f $args[4]

or for short:

$awsacct = '{0:d12}' -f $args[4]

Using any of the above will result in

      9876543 --> 000009876543
 900000876543 --> 900000876543

Upvotes: 0

JosefZ
JosefZ

Reputation: 30113

Either supply a string value e.g. '000009876543' ("000009876543") instead of numeric value 000009876543, or cast/convert a supplied value to string, see the following self-explained code snippet:

if ( $args.Count -lt 1 ) {
    Write-Verbose 'Supply at least one parameter' -Verbose
    exit
}
Remove-Variable val_* -ErrorAction SilentlyContinue
$val_a = $args[0]               # unchanged
$val_b = [string]$args[0]       # dynamically typed variable
[string]$val_c = $args[0]       # strongly typed variable
$val_d = $args[0] -as [string]  # convert the input object, see about_Type_Operators
$val_e = "$($args[0])"          # wrong: removes leading zeroes

Get-Variable val_* | 
    Select-Object -Property Name,
                            Value,
                            @{ L='type'; E={ $_.Value.GetType().Name } },
                            @{ L='test'; E={ $_.Value * 2 } }

Output:

PS D:\PShell> D:\PShell\SO\59486037.ps1 '000123'

Name  Value  type   test
----  -----  ----   ----
val_a 000123 String 000123000123
val_b 000123 String 000123000123
val_c 000123 String 000123000123
val_d 000123 String 000123000123
val_e 000123 String 000123000123

PS D:\PShell> D:\PShell\SO\59486037.ps1 000123

Name   Value type           test
----   ----- ----           ----
val_a 000123 Int32           246
val_b 000123 String 000123000123
val_c 000123 String 000123000123
val_d 000123 String 000123000123
val_e    123 String       123123

Upvotes: 1

Related Questions