Read pass from a txt file issues

Hello everyone,

I was wondering if someone could help me with an issue that I am facing. I am trying the following code but it gives me an error:

# Do the login, get an access token
$loginInfo = @{
    tenancyName = "default"
$usernameOrEmailAddress = "ABC"
 $password = Get-Content C:\Desktop\Pass.txt | ConvertTo-SecureString -AsPlainText -Force
 $credential = New-Object System.Management.Automation.PsCredential($usernameOrEmailAddress,$password)
}

The error is:
A null key is not allowed in a hash literal. $usernameOrEmailAddress = “ABC”

  •   + CategoryInfo          : InvalidOperation: (System.Collections.Hashtable:Hashtable) [], RuntimeException
      + FullyQualifiedErrorId : InvalidNullKey
    
    

I think something is not ok in the syntax of my code.

Also I want to mention that the password that I have saved in the Txt file is in this format: password123

You have dollar signs in front of your key names of your hastable. It should be password instead of $password and credential instead of $credential and so on. :wink:

So it should be like this:

$loginInfo = @{
    tenancyName = "default"
usernameOrEmailAddress = "ABC"
 password = Get-Content C:\Desktop\Pass.txt | ConvertTo-SecureString -AsPlainText -Force

credential = New-Object System.Management.Automation.PsCredential(usernameOrEmailAddress ,password)

}

if i remove the $ sign it gives this error:

 ... m.Management.Automation.PsCredential(usernameOrEmailAddress ,password ...
+                                                                 ~
Missing argument in parameter list.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingArgument

Hi

$loginInfo = @{
        tenancyName = "default"
        usernameOrEmailAddress = "ABC"
        password = Get-Content "C:\Desktop\Pass.txt" | ConvertTo-SecureString -AsPlainText -force
}
$creds = New-Object System.Management.Automation.PsCredential($loginInfo.usernameOrEmailAddress ,$loginInfo.password)
$creds

Still giving errors. As per I am using an API maybe that is what causes the error cuz the pass keeps appearing like NULL.

When you get errors you should share them COMPLETELY formatted as code. Error messages are an important language feature telling you what’s wrong.

Looks like I did smth wrong in the code that was sent to me. This is the answer that I get.

UserName                     Password
--------                     --------
ABC      System.Security.SecureString
Invoke-RestMethod : {"message":"loginModel must not be null","errorCode":0,"resourceIds":null}
At C:\Users\xax.3\Desktop\uipath\Assign_TO_Folder.ps1:30 char:13
+ $response = Invoke-RestMethod -Method 'Post' -Uri $loginUrl -Body $js ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Looks like the issue is in the conversion of Json now:

$loginInfo = @{
        tenancyName = "default"
        usernameOrEmailAddress = "ABC"
        password = Get-Content "C:\Users\xax.3\Desktop\uipath\Pass.txt" | ConvertTo-SecureString -AsPlainText -force

}
$creds = New-Object System.Management.Automation.PsCredential($loginInfo.usernameOrEmailAddress ,$loginInfo.password)
$creds

$json = $loginInfo | ConvertTo-Json
$response = Invoke-RestMethod -Method 'Post' -Uri $loginUrl -Body $json -ContentType "application/json"

I have to admid that Í don’t have any experiences with using Invoke-Restmethod but shouldn’t the credential informations be passed by the parameter -Credential? It feels wrong to have it in the body.

So I would expect it should like something like this:

$loginInfo = @{
    tenancyName = "default"
    password = Get-Content "C:\Users\xax.3\Desktop\uipath\Pass.txt" | ConvertTo-SecureString -AsPlainText -force

}
$creds = New-Object System.Management.Automation.PsCredential($loginInfo.usernameOrEmailAddress ,$loginInfo.password)

$response = Invoke-RestMethod -Method 'Post' -Uri $loginUrl -Credential $creds -ContentType "application/json"

Thank you for the answer. It is supposed that it is in the body of $loginInfo the information of the credentials(username and password) . I tried once again but the result shows like I receive the json with empty password.
Invoke-RestMethod :

{"message":"loginModel must not be null","errorCode":0,"resourceIds":null}
At C:\Users\xax.3\Desktop\uipath\Assign_TO_Folder.ps1:16 char:13
+ $response = Invoke-RestMethod -Method 'Post' -Uri $loginUrl -Credenti ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
{
    "tenancyName":  "default",
    "usernameOrEmailAddress":  "ABC",
    "password":  {
                     "Length":  12
                 }
}

Does it work when you provide the password in plain text … just for the test?

yes it does in fact my project was all working and doing what it was supposed to when the username and password enters as a simple string, but now that I want to make the password secure it is providing me problems.

i am thinking to solve it in another way:
I did:

Read-Host "Enter password" -AsSecureString | 
  ConvertFrom-SecureString |
  Out-File "C:\Users\xax.3\Desktop\uipath\PW.txt"

to encrypt the password and save it in a txt file.

Now what I wanna do is decrypt it but I am not sure if this is the correct way:

 $loginInfo = @{
    tenancyName = "default"
	usernameOrEmailAddress="ABC"
    password = Get-Content "C:\Users\xax.3\Desktop\uipath\PW.txt" | ConvertTo-SecureString

}

What I receive as error of this is:

ConvertTo-SecureString : Key not valid for use in specified state.
At C:\Users\xax.3\Desktop\uipath\Assign_TO_Folder.ps1:11 char:75
+ ... "C:\Users\xax.3\Desktop\uipath\PW.txt" | ConvertTo-SecureString
+                                                    ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertTo-SecureString], CryptographicException
    + FullyQualifiedErrorId : ImportSecureString_InvalidArgument_CryptographicError,Microsoft.PowerShell.Commands.Conv
   ertToSecureStringCommand

{
    "tenancyName":  "default",
    "usernameOrEmailAddress":  "ABC",
    "password":  { /////still empty the password field//////

                 }

Hi,

Script and created password file must be executed from the same user.
What is an output of $loginInfo.password ? is it empty ??

yes it is empty. in the previous code that I sent, I see I am doing an error. I already have the pass encrypted in the text file, but to decrypt it I am using ConvertTo-SecureString, maybe I dont need the word Secure???

Having no experiences with such a topic I’m unsure what to recommend … but:

That has to be done with the same account. If this should run as scheduled job and you’re using another account to run the script than to develop the script it will not work.

Shouldn’t this be ConvertFrom-SecureString instead of ConvertTo-SecureString? And if the REST API expects the password in plain text I’d add the parameter -AsPlainText

I found a solution, in case anyone else will face the same issue in the future: We encrypt the pass and save it in a file txt with the code :

#ConvertFrom-SecureString |
 # Out-File "C:\Users\xax.3\Desktop\uipath\PW.txt"

than we use the above code to decrypt it so the process of login will start:

$SecurePassword = Get-Content C:\Users\xax.3\Desktop\uipath\PW.txt | ConvertTo-SecureString
$UnsecurePassword = (New-Object PSCredential "user",$SecurePassword).GetNetworkCredential().Password
write-host $UnsecurePassword

$loginInfo = @{
    tenancyName = "default"
    usernameOrEmailAddress="ABC"
    password = $UnsecurePassword

}

And than we call the API

Thank you for all of your suggestions