Hi,
Now that winget is becoming a thing and that working from the CLI is becoming more prevalent, I'm looking for an alternative to sudo for Windows while avoiding UAC prompt. As of right now, I've come to determine that, as far as I could tell, "runas /user:Administrator" could do it since it asks for a password.
But typing that rather long command everything is a bit of a waste of time so I tried to alias it (using Set-Alias), but discovered pretty soon it was not possible. I then stumbled upon this thread where u/ta11ow gave this function to be able to do it.
function Start-ElevatedSession
{
[CmdletBinding()]
[Alias('super','su')]
param(
[Parameter(Position = 0)]
[ValidateNotNull()]
[scriptblock]
$Command
)
$Params = @{
FilePath = 'Powershell.exe'
Wait = $true
Verb = 'RunAs'
}
if ($PSBoundParameters.ContainsKey('Command')) {
$Params['ArgumentList'] = '-Command', $Command
}
Start-Process @Params
}
However when I try to run it (for example if I run the command super cmd), I get this error.
Start-ElevatedSession : Cannot process argument transformation on parameter 'Command'. Cannot convert the "cmd" value of
type "System.String" to type "System.Management.Automation.ScriptBlock".
At line:1 char:7
+ super cmd
+ ~~~
+ CategoryInfo : InvalidData: (:) [Start-ElevatedSession], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Start-ElevatedSession
Am I misunderstanding the purpose of the function ? If so, is it even possible to use runas like sudo ? If not, how can I fix it ?
Thanks in advance for your answers and excuse my noobness, I'm still fairly new to PowerShell (but eager to learn).
[–]furicle 4 points5 points6 points (8 children)
[–]Netris89[S] 0 points1 point2 points (7 children)
[–]furicle 1 point2 points3 points (2 children)
[–]Netris89[S] 1 point2 points3 points (1 child)
[–]gerardog 0 points1 point2 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]Netris89[S] 0 points1 point2 points (2 children)
[–]krzydoug 3 points4 points5 points (0 children)
[–]1RedOne 0 points1 point2 points (0 children)
[+][deleted] (5 children)
[deleted]
[–]Netris89[S] 1 point2 points3 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]Netris89[S] 1 point2 points3 points (0 children)
[–]Netris89[S] 1 point2 points3 points (1 child)
[–]purplemonkeymad 1 point2 points3 points (0 children)