Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 913
  • Last Modified:

Powershell PSCredential

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
evilmonkey2148
Asked:
evilmonkey2148
  • 10
  • 7
1 Solution
 
Dan CraciunIT ConsultantCommented:
Great!
And the problem is...?
0
 
evilmonkey2148Author Commented:
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
 
Dan CraciunIT ConsultantCommented:
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
Building an Effective Phishing Protection Program

Join Director of Product Management Todd OBoyle on April 26th as he covers the key elements of a phishing protection program. Whether you’re an old hat at phishing education or considering starting a program -- we'll discuss critical components that should be in any program.

 
evilmonkey2148Author Commented:
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
 
Dan CraciunIT ConsultantCommented:
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
 
evilmonkey2148Author Commented:
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
 
Dan CraciunIT ConsultantCommented:
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
 
evilmonkey2148Author Commented:
after running winrm config, I run the script with no errrors but putty never opens
0
 
Dan CraciunIT ConsultantCommented:
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
 
Dan CraciunIT ConsultantCommented:
Tested and putty will open (check Task Manager) but you won't see any window.
0
 
Dan CraciunIT ConsultantCommented:
Found the problem.
You cannot execute GUI programs using invoke-command. Or you can try, but the GUI will not initialize.
0
 
Dan CraciunIT ConsultantCommented:
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
 
evilmonkey2148Author Commented:
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
 
Dan CraciunIT ConsultantCommented:
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
 
evilmonkey2148Author Commented:
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
 
Dan CraciunIT ConsultantCommented:
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
 
evilmonkey2148Author Commented:
Looks like the last code worked. I need to test further, I will close the question when testing is completed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now