Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell PSCredential

Posted on 2014-04-03
17
Medium Priority
?
862 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
17 Comments
 
LVL 35

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 35

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
Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

 

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 35

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 35

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 35

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 35

Expert Comment

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

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 35

Accepted Solution

by:
Dan Craciun earned 2000 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 35

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 35

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

618 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