PowerShell Script error while setting credentials using Set-Credential command - powershell

We have multiple powershell script where we set credentials, this was working since last 2 years without any issue and suddenly below code start throwing an error. It is performing operations but still throwing error. Not able to figure out what causing the error. Please help if anyone has seen this before.
$UserName='<>'
$Password='<>'
# ************* SET CREDENTIALS ******************************************
$Password = ConvertTo-SecureString $password -AsPlainText -Force
$global:Cred = New-Object System.Management.Automation.PSCredential($username,$Password)
Set-CdmCredential -Domain (Get-WmiObject Win32_ComputerSystem).Domain -Credential $Cred

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.

The underlying connection was closed: The connection was closed unexpectedly for neo4j powershell script

I have a powershell script I am trying to use to run queries on a neo4j database. Following the steps here: https://github.com/RamblingCookieMonster/PSNeo4j.
The issue is that when I run the code with proper creds and URL, I get a connection closed error. I am having trouble believing it is the library this happens with a 'Invoke-WebRequest' command too. Assuming that my creds and url are correct, is there something I am missing?
Code:
Import-Module PSNeo4j
$Password = ConvertTo-SecureString -String "dummypswd" -AsPlainText -Force
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'username', $password
Set-PSNeo4jConfiguration -Credential $Cred -BaseUri 'http://localhost:7687'
# Did we connect?
Get-Neo4jUser

Jenkins Deployment - Powershell SCP Script

Good evening,
I'm trying to write a Powershell script that will connect to a remote server via SCP and upload or download files/folders. Ultimately this is the script that I would like Jenkins to run.
So far I'm using Posh-SSH and having good success. The only issue is, no matter what I have tried so far, it will always prompt me for my credentials. This, obviously, makes it not entirely automatic.
I have attached a few things I've tried. Hopefully someone can help me out with this!
The basic command I'm testing with:
get-scpfolder -computername '111.111.111.111' -credential $credential
-remotefile "/var/myFolder" -localfile 'C:\Users\Me\destFolder'
Again, this works, but it requires me to enter my credentials.
I saw this command online:
$Password = "pass"
$User = "admin"
$ComputerName = "111.111.111.111"
$Command = "get-scpfolder -computername $ComputerName -credential $Credentials -localfolder 'C:\Users\Me' -remotefolder '/var/destFolder"
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)
$SessionID = New-SSHSession -ComputerName $ComputerName -Credential $Credentials #Connect Over SSH
Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command # Invoke Command Over SSH
However this returns ExitStatus 1 and nothing happens. I have tried a few variations of the $Command including the credentials or not, for example, and I can't get any of it to work.

PowerShell Computer StartUp-Script - Switch user-context?

Through Microsoft Group Policy I did define to run a Powershell-Script on Computer Start-Up. Also I have the requirement to run a Powershell-Script as Scheduled Task without saving credentials.
On both scenarios I have the same problem ...
I want to run a Citrix Powershell-Command (PSSnapIn) like:
Set-BrokerMachine -MachineName "domain.local\$env:COMPUTERNAME" -AdminAddress "RemoteServer.domain.local" -InMaintenanceMode $True
Manual: https://citrix.github.io/delivery-controller-sdk/Broker/Set-BrokerMachine/
Of course only users who have the permission could run those Citrix-commands. I would be able to give a domain-user the permission to run the command "Set-BrokerMachine", but in the mentioned scenarios the PowerShell-scripts run in context of the system-user.
I did simulate the system-user by PSExec:
Error running as System-User
My scripts do other things of course and I want to keep them running as System-User, but now I am looking for a clean solution to get those Citrix-commands running.
If possible, I don't want to save credentials in my scripts.
EDIT #1:
I would be able to workaround with the following code:
$Username = "MySpecialUser"
$Password = 'MyPassword'
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential $Username, $SecurePassword
$Result = Invoke-Command -Session ( New-PSSession -ComputerName "RemoteServer.domain.local" -Credential $Credential ) -ScriptBlock {
Add-PSSnapin Citrix*
Set-BrokerMachine -MachineName "domain.local\$args" -InMaintenanceMode $True
} -ArgumentList $env:COMPUTERNAME -HideComputerName
Remove-PSSession -InstanceId $Result.RunspaceId
I don't like this because:
The code has to contain credentials (ofc I could encrypt it ...)
I have to create a permission-system for this special user in Citrix
I have to put the special-user into a local-group on every server, to allow the remote-administration (security-risk)
I don't like to use PSSession
...
Is there a better/cleaner solution? Any ideas?

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