New-Pssession with -credential $variable error

I’m trying to create a new Ps session in my script using a function output at the username for the -credential switch but I’m getting this error:

New-Pssession : Cannot process argument transformation on parameter 'Credential'. userName

Can you not use command output for that value?

Thanks!

How are you executing the command ? please post that particular line of code and read documentation from Get-Help New-PSSession -Online.

[quote quote=137579]How are you executing the command ? please post that particular line of code and read documentation from Get-Help New-PSSession -Online.

[/quote]

The function I found here

I’m calling it like:

$adminuser = get-pswho | ft user

new-pssession -computername "computer" -credential $adminuser

Using Get-Help we can see the -Credential parameter expects a PSCredential Object. It sounds like you are not providing that as an argument for -Credential, but rather a string with a user name?

 

[pre]

Get-Help New-PSSession -Parameter credential

[/pre]

-Credential <PSCredential>
Specifies a user account that has permission to perform this action. The default is the current user.

Type a user name, such as User01, Domain01\User01, or User@Domain.com, or enter a PSCredential object, such as one returned by the Get-Credential cmdlet.

When you type a user name, this cmdlet prompts you for a password.

Required? false
Position? named
Default value None
Accept pipeline input? True (ByPropertyName)
Accept wildcard characters? false

You would want to use Get-Credential to build a PSCredential Object.

[pre]

$cred = Get-Credential
New-PSSession -ComputerName HostName -Credential $cred

[/pre]

 

That function doesnt seem necessary at face value, however I also do not understand the full purpose of your script. A couple of things though, you shouldn’t save formatted objects into variables. Instead format the output when its called, what I am not understanding is the function appears to retrieve info about your computer and the current logged on user. The -Credential parameter defaults to the user running the PS session, it just seems redundant to me, however the below snippet should function to prompt for the password for the username returned from $adminuser

[pre]

$adminuser = get-pswho | select-Object -expandProperty User
new-pssession -computername “computer” -credential $adminuser

[/pre]

[quote quote=137595]That function doesnt seem necessary at face value, however I also do not understand the full purpose of your script. A couple of things though, you shouldn’t save formatted objects into variables. Instead format the output when its called, what I am not understanding is the function appears to retrieve info about your computer and the current logged on user. The -Credential parameter defaults to the user running the PS session, it just seems redundant to me, however the below snippet should function to prompt for the password for the username returned from $adminuser

PowerShell
5 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 7.22469px; left: 45px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
$adminuser = get-pswho | select-Object -expandProperty User
new-pssession -computername "computer" -credential $adminuser
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote] This is exactly it! I did not realize | ft user was including other info not just the value of the attribute.

I’m running that function (get-pswho) to get the domain\user of the person running the script. I googled for that and did not see anything like get-credential. I could have included a prompt for the username but the intention is to only run this as Admin so I thought it’d save a itty bit of time for the user.

Oh and thank you Logan.

“This is exactly it! I did not realize | ft user was including other info not just the value of the attribute.”

I don’t think you understand what was being said here.
whenever you format output, you immeidately lose the object, and wind up with a string of text.

To understand this run the following commands to see what output you are recieving and what type of ojbect is being returned.

get-pswho | ft user|get-member

get-pswho |get-member

the first command will show you that you have a string after you perform the ft (this is an alias for format-table)
not sure to be honest the object type your function is designed to return, but it will have appropriate properties that can be addressed to build a pscredential object.
but this is a fundamental point of powershell, it is object oriented.

[pre]

$adminuser = get-pswho

new-pssession -computername “computer” -credential $adminuser.user

[/pre]