Another option using your original code is to use splatting, which will avoid
you from needing to write out the similar Start-Process line twice:
Function Start-Elevated {
[CmdletBinding()]
Param (
[parameter(Mandatory =
$true,ValueFromPipeline=$true,Position=0)][String]$Executable,
[parameter(Mandatory =
$false,ValueFromRemainingArguments=$true,Position=1)][String]$Parameters
)
$StartProcessParams = @{}
If ($Parameters) {
$StartProcessParams.ArgumentList = $Parameters
}
Start-Process -Verb RunAs -FilePath $Executable @StartProcessParams
}
This worked on these examples:
Start-Elevated 'eventvwr' 'somecomputer'
Start-Elevated 'eventvwr'
I'll be curious to hear from MBS or others if there is a way to adjust the code
MBS provided earlier to account for no parameters since the tuple assignment is
definitely cleaner!
-Aakash Shah
From: [email protected] [mailto:[email protected]] On
Behalf Of Damien Solodow
Sent: Thursday, October 9, 2014 9:20 AM
To: [email protected]
Subject: [powershell] RE: Better way to accomplish this?
Hmm. Problem here is that if I use this to run a command without arguments it
throws an exception.
So if I do 'start-elevated diskmgmt.msc' it throws an 'Start-Process : Cannot
validate argument on parameter 'ArgumentList'. The argument is null or empty.
Provide an argument that is not null or empty, and then try the command again.
DAMIEN SOLODOW
Systems Engineer
317.447.6033 (office)
317.447.6014 (fax)
HARRISON COLLEGE
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Michael B. Smith
Sent: Thursday, October 9, 2014 11:57 AM
To: [email protected]<mailto:[email protected]>
Subject: [powershell] RE: Better way to accomplish this?
I tried to make minimal changes and didn't test. My bad. Here is a much shorter
(and working) solution. Note the dependence on a tuple assignment. Not very
common, but very useful.
function Start-Elevated
{
$executable, $arguments = $args
Start-Process -Verb RunAs -FilePath $executable -ArgumentList
$arguments
}
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Damien Solodow
Sent: Thursday, October 9, 2014 11:39 AM
To: [email protected]<mailto:[email protected]>
Subject: [powershell] RE: Better way to accomplish this?
I forgot about $args. :)
The main issue/concern I had with the way I was doing it was that I had to
mostly duplicate the Process{} in the If..Else and I was hoping there was a
"cleaner" way to do it.
Although.. I tested the function you provided, and if I do "Start-elevated
-executable notepad test.txt" it tells me that A positional parameter cannot be
found that accepts argument 'test.txt'
DAMIEN SOLODOW
Systems Engineer
317.447.6033 (office)
317.447.6014 (fax)
HARRISON COLLEGE
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Michael B. Smith
Sent: Thursday, October 9, 2014 11:24 AM
To: [email protected]<mailto:[email protected]>
Subject: [powershell] RE: Better way to accomplish this?
There is already a built-in $args variable that accomplishes the same thing. To
use the same function, formatted the way I prefer is below.
But the question really is (since formatting is a religious argument, not a
capability argument) - what problems/concerns do you have with the script/link
you posted? What challenge do you need to overcome?
Function Start-Elevated
{
[CmdletBinding()]
Param(
[parameter(Mandatory =
$true,ValueFromPipeline=$true,Position=0)][String]$Executable
)
If( !$args -or $args.Count -eq 0 )
{
Start-Process -Verb RunAs -FilePath $Executable
}
Else
{
Start-Process -Verb RunAs -FilePath $Executable -ArgumentList $args
}
}
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Damien Solodow
Sent: Thursday, October 9, 2014 11:00 AM
To: [email protected]<mailto:[email protected]>
Subject: [powershell] Better way to accomplish this?
I'm creating a function to invoke a specified executable in an elevated session.
The main obstacle right now is around passing parameters (arguments) to the
process I'm launching.
I have a way that works, but I'm wondering if there is a better way to
accomplish it:
https://gist.github.com/dsolodow/6dfcfb3e413ae2fec5a2#file-sudo-function
DAMIEN SOLODOW
Systems Engineer
317.447.6033 (office)
317.447.6014 (fax)
HARRISON COLLEGE
500 North Meridian St
Suite 500
Indianapolis, IN 46204-1213
www.harrison.edu<http://www.harrison.edu/>
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1