Start-Process with Credential [SOLVED]

by smadon at 2013-02-27 09:12:50

Hi all,

I would like to run a program with admin right.
So I write theses lines, but I have a elevation right error.
How can I give the elevation right to run my program ???


$Username = "Administrator"
$Password = "MYPASSWORD"
$MyProg = "C:/MyFolder/MyProgr.exe"

$Credential = New-Object System.Management.Automation.PSCredential ("$UserName",(ConvertTo-SecureString -String $Password -AsPlainText -Force))
Start-Process -filePath $MyProg -Credential $Credential


Error Message

Start-Process : This command cannot be executed due to the error: The requested operation requires elevation.


Thanks for your help
Smadon
by jonhtyler at 2013-02-27 09:22:20
Please see the following article…this should help you…

http://social.technet.microsoft.com/For … 7bfccd39ea
by smadon at 2013-02-28 04:05:17
Hi,
Thanks jonhtyler for the link.

I added the code :


$Username = "Administrator"
$Password = "MYPASSWORD"
$MyProg = "C:/MyFolder/MyProgr.exe"

$Credential = New-Object System.Management.Automation.PSCredential ("$UserName",(ConvertTo-SecureString -String $Password -AsPlainText -Force))
Start-Process powershell -Credential $Credential -ArgumentList ‘-NoNewWindows -noprofile -command &{start-process $MyProg -wait -verb runas -RedirectStandardError C:\temp\temp2.txt}’


And I still have the error message about the Elevation,

Any Idea ???
Smadon
by jonhtyler at 2013-02-28 04:15:31
I don’t know right off. Let me play with it when I get set up a little later this morning, and I will see if I can reproduce the error with a little testing.
by yesffan at 2013-02-28 07:53:42
You’re mixing up parameters for Powershell.exe and parameters for the Start-Process cmdlet. Try this code:

$Username = "Administrator"
$Password = "MYPASSWORD"
$MyProg = "C:/MyFolder/MyProgr.exe"

$Credential = New-Object System.Management.Automation.PSCredential ("$UserName",(ConvertTo-SecureString -String $Password -AsPlainText -Force))
$argList = @(‘-noprofile’, "-command "&{ $MyProg }" ")
Start-Process powershell -Credential $Credential -ArgumentList $argList -Wait -RedirectStandardError C:\temp\temp2.txt}’


$argList contains the parameters for Powershell (the process you’re starting)

Fernando
by smadon at 2013-02-28 22:28:50
Hi all,

Thanks Fernando for your helpm it works :slight_smile:

But I have one last problem with my variable
$MyProg is normaly in C:\Program Files (x86)\MyFolder\MyProg.exe


$MyProg = "${Env:ProgramFiles(x86)}" + "\MyFolder\MyProgr.exe"


When I run the script, I have this error Message
[quote]
The term ‘C:\Program’ is not recognized as the name of a cmdlet, function, scri
pt file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:14
+ &{ C:\Program <<<< Files (x86)\MyFolder\MyProg.exe }
+ CategoryInfo : ObjectNotFound: (C:\Program:String) , CommandN
otFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
[/quote]

What should i do to resolve the space problem ???
Thanks for your help
by happysysadm at 2013-03-01 01:26:24
Hi there,
Have you tried testing path validity?
[code2=powershell]test-path $MyProg[/code2]
Also, even if it does not solve your problem, try to use join-path instead of string concatenation.
Carlo
by AlexBrassington at 2013-03-01 02:16:22
At a guess you might passing the program files string without the quotation marks that tell powershell that it’s a single string. You might want to manually format the argument value.
by yesffan at 2013-03-01 07:10:04
Try this:

$Username = "Administrator"
$Password = "MYPASSWORD"
$MyProg = "C:/MyFolder/MyProgr.exe"

$Credential = New-Object System.Management.Automation.PSCredential ("$UserName",(ConvertTo-SecureString -String $Password -AsPlainText -Force))
$argList = @(‘-noprofile’, "-command &quot;&amp;{ &amp; '$MyProg' }" ")
Start-Process powershell -Credential $Credential -ArgumentList $argList -Wait -RedirectStandardError ‘C:\temp\temp2.txt’


That way, Powershell will interpret what’s in single quotes as an OS command.

Fernando.
by smadon at 2013-03-01 08:38:31
PERFECT :slight_smile:

Thank you guys for quick reply and your help :slight_smile: