How to pass Windows credential in a PowerShell script? - powershell

I am writing a PS script to open a URL automatically in a Chrome browser. I created a credential object and pass it to Start-Process as below.
$username = Read-Host 'What is your username?'
$password = Read-Host 'What is your password?' -AsSecureString
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList #($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
Start-Process -FilePath Chrome -ArgumentList $url -Credential $credentials
I expect the Chrome browser to be opened with the URL using the credential object. But it's throwing the error as below.
Start-Process : This command cannot be executed due to the error: Logon failure: unknown user name or bad password.
Note: The URL works fine if I pass the Windows security credentials to the URL manually, therefore my credentials are good. Feel something is wrong in passing the Windows security credentials. Not sure what's wrong.

$userName = "Pandiyan"
$secpasswd = ConvertTo-SecureString "mypasswordiscool" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secpasswd
$url = "localhost/atomicscope"
Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -ArgumentList $url -Credential $mycreds
You should also specify the path to chrome and try this again. Instead of readhost provide the credentials directly and it should fire chrome right away. If there is a user waiting to be typing in the console its fine. Else there's no use of automating this with powershell

You already read the password as a secure string, so you don't need to do that again when creating the credential object. ConvertTo-SecureString would only be required if $password contained a plaintext password.
This will do what you want:
$credentials = New-Object Management.Automation.PSCredential $username, $password

Related

Cant use -Credential when using "Start-Process" in Powershell

I want to execute a .exe File with the Start-Process Command while using specific credentials.
However I cant get it to work for me:
$username = "<Domain\Username>"
$password = "<Password>"
$secPassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentialps = New-Object System.Management.Automation.PSCredential ($username, $secPassword)
Start-Process -FilePath "<path to exe>\Test.exe" -Credential $credentialps -NoNewWindow -ArgumentList "<Arguments>"
This outputs the following error:
Start-Process : This command cannot be run due to the error: The parameter is incorrect.
I Also tried just using $credential = Get-Credential, this gives you back an PSCredentials Object, but it gave me the same error.
What am I doing wrong? Sorry if something's missing, Iam new to Powershell :)
Edit: After removing the Credentials parameter the script runs perfectly, so there shouldnt be a problem regarding the FilePath or Argumentlist.

Start-Process as another user with Credentials and ArgumentList

I am currently facing the issue of running an .exe as another user. I followed the documentation and all the given suggestion in the internet, but still cannot run it successfully as another user with arguments.
Need some advise on how to resolve this. I am using powershell v4. Much help appreciated. Thank you.
$username = 'wintel\approveduser'
$password = 'password123'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
Start-Process -FilePath D:\testing\CICD.exe -Credential $credential -ArgumentList "-i -b CICDbranch -m Release -r -f $currentPath\Release\export.zip" -RedirectStandardOutput ".\stdout.txt"

Add username and password to Powershell script

I'm trying to create a powershell script to allow my kids to reboot my Raspberry Pi from their Windows computer if need be. I've tested everything and have gotten it to work, but the only hitch is that it's prompting for a username and password. I realize the line that's doing it is:
New-SSHSession -ComputerName "myPi" -Credential (Get-Credential)
I've done some searching, but I can't seem to figure out if it's possible to replace the "(Get-Credential)" section to automatically enter the username/password.
And yes, I'm aware of the security risks. They could do much more damage to the Windows machine than they could ever do on the Pi, and the settings on the Pi are very easily restored, so no worries from my end.
Something like this should work:
$user = "someuser"
$pass = ConvertTo-SecureString -String "somepassword" -AsPlainText -Force
$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass
New-SSHSession -ComputerName "myPi" -Credential $creds
You could also call a file that has the password encrypted in it. Note this can only be decrypted by the account it was generated on on the computer it was generated on.
$pass = "Password"
$Username = "Username"
$outfile = "c:\filelocation.xml"
$secureStringPwd = $pass | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($Username,$secureStringPwd)
$credential | Export-CliXml -Path $OutFile
Addressing Bill.
Correct, hard coding the password in the script is bad practice. Below is how I would change the first portion.
The above came from a custom script that's purpose was to create many cred accounts off a input json is why I wrote it that way.
$outfile = "c:\filelocation.xml"
Get-Credential | export-clixml -path $OutFile
You then can call the file in your script like so but this has to be done on the same user and computer that the creds file was generated on.
$Creds = Import-Clixml -Path "c:\file.xml"
New-SSHSession -ComputerName "myPi" -Credential $creds
Good point Edited -argumentlist.
Another option could be to do a 1 time setup with get-credential then convert the password to plaintext using convertfrom-securestring and then in the file you can take your password plaintext secure string and so something similar to the other answers:
$user = "someuser"
$pass = "YOUR LONG PASSWORD GUID FROM ABOVE" | convertTO-securestring
$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass
New-SSHSession -ComputerName "myPi" -Credential $creds
This lets you do a one time setup, but avoids having multiple files or having your password appear in a readable way in the script.
If you go this way you need to do the setup FROM the account that will run the script ON the machine that will run the script, because it uses those for the encryption as far as I know.

FTP upload fails to azure using PSFTP Powershell Module

I am using a powershell module provided by microsoft called PSFTP. When the script runs on any developer Windows 10 powershell it works fine, but on the build server it fails with an error message (530) Not logged in. Why might it be failing with that message on Windows Server 2016 only? It's the same script with same credentials.
PSFTP source is here https://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb
$username = "azureusername"
$password = ConvertTo-SecureString "azurepassword" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$ftpserver = "ftp://*****ftp.azurewebsites.windows.net"
$fileToDelete = "/site/wwwroot/bin"
Set-FTPConnection -Credentials $cred -Server $ftpserver -Session MyFTPSession -UsePassive
$Session = Get-FTPConnection -Session MyFTPSession
Remove-FTPItem -Session $Session -Path $fileToDelete
According to your description, I test in my Windows Server 2016, if I use the username and password that download from Portal Get publish profile. I get the same error log with you.
I set the username and password on Azure Portal.
It works for me fine.

PowerShell Invoke-Webrequest / provide password and skip promt

I made a script accessing an API which requires authentication. The this is working if I enter the credentials (email+pw) into the prompt:
$cred = Get-Credential
$web = Invoke-WebRequest -Uri $srcURI -Credential $cred
But this does not allow me to automate this setup, as a prompt is always coming up asking for Username/PW.
I tried several different ways, but none seems to be working:
For me the most logical was:
$username="user"
$password="password1"
$PScredOBJ = New-Object -TypeName System.Management.Automation.PSCredential($username,$password)
$cred2 = Get-Credential -Credential $PScredOBJ
Result: Still an empty prompt showing up.
Anybody knows how to handle this?
Thx
This has been covered in a lot of places, a google search would give an answer. Here's the first one I found using "automate ps credential" as my search terms. To reiterate:
$password = "password" | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential "username",$password