Score:0

can't automate the authentication process with PowerShell on Linux

sz flag

On Debian 11, I have installed PowerShell 7.3.0. I am testing if I can run commands on a remote Windows computer. This is a simple example to get the remote computer hostname.

#!/usr/bin/env pwsh

$username = "Administrator"
$password = "passgoeshere"
$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $secureStringPwd

Invoke-Command -ComputerName 192.168.xxx.xxx -Credential $cred -ScriptBlock { hostname }

My problem is that when I run this script, I still get a prompt to enter a username and password.

PowerShell credential request
Enter your credentials.
User:

It's not a problem with the credentials because when I enter them manually, it gives me the hostname of the remote computer.

I think it has something to do with the way PowerShell on Linux reads the credentials.

What can I do to get the script not to prompt for user/pass?

edit: the issue was a typo

Jeff Schaller avatar
nf flag
I don't suppose it's a simple typo of `$user` for `$username`?
aristosv avatar
sz flag
yes, I found that out later on. just a typo.
Jeff Schaller avatar
nf flag
Is it still a problem and it's a typo to fix in the question, or the problem went away after changing the variable?
in flag
Please don't edit the solution into your question, that way the question stays as unsolved in the system forever. Instead, post your solution as an answer and accept it. Even when it just was a typo. Or ask the user who provided the solution to post it as an answer, so you can accept it.
Score:0
sz flag

there was a typo in the script

Score:0
mn flag

Your script it's ok. Change variable $username to $user

I used "Invoke-VMSscript"

$user = "root"

$password = "P@ssword"

$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force

$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $secureStringPwd

Invoke-VMScript -VM My_VM -ScriptText "hostname" -GuestCredential $cred -ScriptType Bash

ScriptOutput
---------------------------------------------|  My_VM
|  
---------------------------------------------
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.