Solved

Powershell PSCredential

Posted on 2014-04-03
17
802 Views
Last Modified: 2014-04-28
I am trying to user powershell to run an application as a different user by passing a PScredential object. Below is my code:

$encrypted = "removed for security concerns"
$user = "domain\user"
$password = ConvertTo-SecureString -string $encrypted

$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$password

invoke-command -Credential $cred -FilePath C:\putty.exe

Open in new window


I change the password, domain, and username for security reasons.
0
Comment
Question by:evilmonkey2148
  • 10
  • 7
17 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39976196
Great!
And the problem is...?
0
 

Author Comment

by:evilmonkey2148
ID: 39976233
I get this error:
Invoke-Command : Parameter set cannot be resolved using the specified named par
ameters.
At C:\Users\travism\Desktop\test.ps1:7 char:15
+ invoke-command <<<<  -Credential $cred -FilePath C:\putty.exe
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], Parameter
   BindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Comma
   nds.InvokeCommandCommand
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39976296
Well, you do need -ComputerName if you use -Credential, or PS won't know where to use those credentials. So:
invoke-command -ComputerName stationname -FilePath C:\putty.exe -Credential $cred 

Open in new window

0
 

Author Comment

by:evilmonkey2148
ID: 39976338
Here is my error:
Invoke-Command : Only PowerShell script files are allowed for FilePath paramete
r. Specify a file with .ps1 extension.
Parameter name: filePath
At C:\Users\travism\Desktop\test.ps1:7 char:15
+ invoke-command <<<<  -ComputerName W7-R9D2XGN -FilePath C:\Documents\Tools\pu
tty.exe -Credential $cred
    + CategoryInfo          : NotSpecified: (:) [Invoke-Command], ArgumentExce
   ption
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Co
   mmands.InvokeCommandCommand
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39976372
Yup. Looks like you need a script block if you want to run standalone programs. Something like
invoke-command -ComputerName W7-R9D2XGN -ScriptBlock { C:\putty.exe } -Credential $cred  

Open in new window

0
 

Author Comment

by:evilmonkey2148
ID: 39976416
here is the new error:
[w7-r9d2xgn] Connecting to remote server failed with the following error messag
e : The client cannot connect to the destination specified in the request. Veri
fy that the service on the destination is running and is accepting requests. Co
nsult the logs and documentation for the WS-Management service running on the d
estination, most commonly IIS or WinRM. If the destination is the WinRM service
, run the following command on the destination to analyze and configure the Win
RM service: "winrm quickconfig". For more information, see the about_Remote_Tro
ubleshooting Help topic.
    + CategoryInfo          : OpenError: (:) [], PSRemotingTransportException
    + FullyQualifiedErrorId : PSSessionStateBroken

I removed -ComputerName and got this error:
Invoke-Command : Parameter set cannot be resolved using the specified named par
ameters.
At C:\Users\travism\Desktop\test.ps1:11 char:15
+ invoke-command <<<<  -ScriptBlock { C:\Documents\Tools\putty.exe } -Credentia
l $cred
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], Parameter
   BindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Comma
   nds.InvokeCommandCommand
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39976429
Why don't you do what the nice error message is saying?

Type "winrm quickconfig" and see what happens. I'll bet it will tell you to start the WinRM service :)
0
 

Author Comment

by:evilmonkey2148
ID: 39976512
after running winrm config, I run the script with no errrors but putty never opens
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39976600
You can test what commands you can run and their effect by typing
Enter-PSSession

This will open a new session, and any command that works here will work in Invoke-Command.
Try it and check if you can open putty, notepad etc. I suspect they will open but you won't see their windows.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39976630
Tested and putty will open (check Task Manager) but you won't see any window.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39976676
Found the problem.
You cannot execute GUI programs using invoke-command. Or you can try, but the GUI will not initialize.
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39976705
I just re-read your question and seems like we wasted some time. I only read your code at first and assumed you want to run the program on a remote system ;)

If you just want to run the program as a different user on your own computer, simply use this:
Start-Process C:\putty.exe -Credential $cred

Open in new window

Yup. It's that simple.

HTH,
Dan
0
 

Author Comment

by:evilmonkey2148
ID: 39986234
So here is the latest code i tested:
$encrypted = "01000000d08c9ddf0115d1118c7a00c04fc297eb01000000560cd73cf4b0ee4199ca4d7bf74326290000000002000000000003660000c00000001000000098d3c6a7d1ee59362551921702b6e8240000000004800000a00000001000000065505b936bebc225d6569de07f63fa1d18000000927b75aeecf8a1801607323417bc44ea28805dc4407e8aaa14000000779c3e1f0e85de56438b2c1290cbc35711111111"
$user = "corporate\person"
$password = ConvertTo-SecureString -string $encrypted

$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$password

Start-Process C:\Documents\Tools\putty.exe -Credential $cred

Open in new window


Here is the error:
C:\Users\me\Desktop>powershell test1.ps1
The term 'test1.ps1' is not recognized as the name of a cmdlet, function, scrip
t file, or operable program. Check the spelling of the name, or if a path was i
ncluded, verify that the path is correct and try again.
At line:1 char:10
+ test1.ps1 <<<<
    + CategoryInfo          : ObjectNotFound: (test1.ps1:String) [], CommandNo
   tFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


I feel like this shouldn't be so hard.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39986267
That has nothing to do with the code.
The error simply says the test1.ps1 file does not exist.

If you're sure it's called test1.ps1 and you're sure it's on your desktop and you navigated to your desktop, try this:

powershell .\test1.ps1

The ".\" means "current folder"
0
 

Author Comment

by:evilmonkey2148
ID: 39986282
It is obvious that i am horrible at powershell but i know that directory is valid.

new error:
Start-Process : This command cannot be executed due to the error: The directory
 name is invalid.
At C:\Users\travism\Desktop\test1.ps1:7 char:14
+ Start-Process <<<<  "C:\Documents\Tools\putty.exe" -Credential $cred
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOp
   erationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.C
   ommands.StartProcessCommand
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39986333
Well, Powershell obviously disagrees :)

If you're sure the path is correct, make sure the user you're impersonating has the right to read/execute that folder.
0
 

Author Comment

by:evilmonkey2148
ID: 39991926
Looks like the last code worked. I need to test further, I will close the question when testing is completed.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Set OWA language and time zone in Exchange for individuals, all users or per database.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now