Solved

Powershell PSCredential

Posted on 2014-04-03
17
815 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no backā€¦
By default the complete memory dump option is disabled in windows . If we want to enable the complete memory dump for a diagnostic purpose, we have a solution for it. here we are using the registry method to enable this.
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

839 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