Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - How can I run scipts with Admin privilege?

Reply
 
Old 02-05-2009   #1 (permalink)
DavieH


 
 

How can I run scipts with Admin privilege?

I can open PS shells via "Run as administrator" but that option is greyed out
for PS scripts and their shortcuts. I have set "unrestricted" access for
execution policy!??

DavieH
===
Windows 7, PS v2.

My System SpecsSystem Spec
Old 02-05-2009   #2 (permalink)
Karl Mitschke


 
 

Re: How can I run scipts with Admin privilege?

Hello DavieH,
Quote:

> I can open PS shells via "Run as administrator" but that option is
> greyed out for PS scripts and their shortcuts. I have set
> "unrestricted" access for execution policy!??
>
> DavieH
> ===
> Windows 7, PS v2.
Even on Windows 7, I doubt if Microsoft defaults to allowing you to run scripts
by double clicking on them - thus, you can't "run a script with admin privleges"
withut first opening the shell or ISE.


My System SpecsSystem Spec
Old 02-05-2009   #3 (permalink)
RichS [MVP]


 
 

RE: How can I run scipts with Admin privilege?

By design PowerShell does not run scripts by double clicking them. This is
extremely unlikely to change in future versions of PowerShell. There is no
way to elevate privileges from within a PowerShell script.

The only way to run scripts with admins privileges is to start PowerShell or
the ISE using run as admin
OR you could schedule a job to run the powershell script and use an account
with admins privileges
--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"DavieH" wrote:
Quote:

> I can open PS shells via "Run as administrator" but that option is greyed out
> for PS scripts and their shortcuts. I have set "unrestricted" access for
> execution policy!??
>
> DavieH
> ===
> Windows 7, PS v2.
My System SpecsSystem Spec
Old 02-05-2009   #4 (permalink)
Charlie Russel - MVP


 
 

Re: How can I run scipts with Admin privilege?

I simply open two powershell windows when I first log on - one standard
user, one elevated. The elevated one has a maroon background, and has
Administrator in the title bar.

--
Charlie.
http://msmvps.com/blogs/xperts64
http://mvp.support.microsoft.com/profile/charlie.russel

"RichS [MVP]" <RichSMVP@xxxxxx> wrote in message
news:1BE21458-D8E8-4A09-AE7A-8D59A22DFC20@xxxxxx
Quote:

> By design PowerShell does not run scripts by double clicking them. This
> is
> extremely unlikely to change in future versions of PowerShell. There is
> no
> way to elevate privileges from within a PowerShell script.
>
> The only way to run scripts with admins privileges is to start PowerShell
> or
> the ISE using run as admin
> OR you could schedule a job to run the powershell script and use an
> account
> with admins privileges
> --
> Richard Siddaway
> All scripts are supplied "as is" and with no warranty
> PowerShell MVP
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "DavieH" wrote:
>
Quote:

>> I can open PS shells via "Run as administrator" but that option is greyed
>> out
>> for PS scripts and their shortcuts. I have set "unrestricted" access for
>> execution policy!??
>>
>> DavieH
>> ===
>> Windows 7, PS v2.
My System SpecsSystem Spec
Old 02-05-2009   #5 (permalink)
Karl Mitschke


 
 

Re: How can I run scipts with Admin privilege?

Hello Charlie Russel - MVP,
Quote:

> I simply open two powershell windows when I first log on - one
> standard user, one elevated. The elevated one has a maroon background,
> and has Administrator in the title bar.
>
Mine's much the same:

Administrator PowerShell for the title, black background, a Yellow "Admiminstratoe
<path>" prompt, and magenta (inside company joke) for the foreground

Can't mistake it for my non admin shell!


My System SpecsSystem Spec
Old 02-05-2009   #6 (permalink)
Charlie Russel - MVP


 
 

Re: How can I run scipts with Admin privilege?

Exactly - no mistaking this one either!

I use a multiline prompt, as well, left over from my days as an Oracle DBA
and UNIX sysadmin. Has what account I'm running under, what my default
database is, and what my current location (machine and path) is. Converting
that from Korn shell was one of my first fun projects in PowerShell. But for
elevated windows, I change the background/foreground as well. Just so
there's no doubt!

--
Charlie.
http://msmvps.com/blogs/xperts64
http://mvp.support.microsoft.com/profile/charlie.russel

"Karl Mitschke" <kmitschke@xxxxxx> wrote in message
news:d66cd4c26245d8cb558b021e4ec9@xxxxxx
Quote:

> Hello Charlie Russel - MVP,
>
Quote:

>> I simply open two powershell windows when I first log on - one
>> standard user, one elevated. The elevated one has a maroon background,
>> and has Administrator in the title bar.
>>
> Mine's much the same:
>
> Administrator PowerShell for the title, black background, a Yellow
> "Admiminstratoe <path>" prompt, and magenta (inside company joke) for the
> foreground
>
> Can't mistake it for my non admin shell!
>
>
My System SpecsSystem Spec
Old 02-05-2009   #7 (permalink)
Josh Einstein


 
 

Re: How can I run scipts with Admin privilege?

"Charlie Russel - MVP" <charlie@xxxxxx> wrote in message
news:u4h$0y6hJHA.5000@xxxxxx
Quote:

> I simply open two powershell windows when I first log on - one standard
> user, one elevated. The elevated one has a maroon background, and has
> Administrator in the title bar.
Ha! You do that too? One thing that annoys the hell out of me though is that
I can only give it the normal range of ConsoleColor enum values. I can't
give it a more pleasing color which can you mix up if you go into the
command shell preferences.

As for elevating once you're at a prompt though, check this out. I just
added this to my Host.psm1 module. It depends on a utility by Wintellect
called Elevate.exe but it's pretty convenient if you're sitting in a
directory and think "dammit I forgot to elevate this process..."

Get the module here...
http://cid-89e05724af67a39e.skydrive...l/Modules/Host

Usage:
cd c:\some\deep\directory\structure\I\really\dont\wanna\type\again
Start-PowerShell .\Update-Gac.ps1 -Elevated
Start-PowerShell -Command { ...do horrible admin stuff... } -Elevated

Here's the function(s):

function Start-ElevatedProcess {

[CmdletBinding()]
param (
[Parameter(Position=1)][String]$Path,
[Parameter(Position=2)][String[]]$Arguments,
[Parameter()][Switch]$Wait
)

$ElevateExe = Join-Path $PSScriptRoot 'elevate.exe'
$ElevateMissingMessage = @"
The Wintellect elevate.exe utility was not found in the module directory.
This utility is required to launch elevated processes. Download this utility
and place it in the following folder.

$PSScriptRoot

Do you want to download it now from http://www.wintellect.com?
"@

if ( Test-Path $ElevateExe ) {

$ElevateArgs = @()

if ($Wait) { $ElevateArgs += '-wait' }

$ElevateArgs += $Path
$ElevateArgs += $Arguments

Start-Process $ElevateExe $ElevateArgs -NoNewWindow

}
else {
# Prompt the user to get it from Wintellect
Show-Menu 'Download Elevate.exe' -m $ElevateMissingMessage {
Choice 'No' { }
Choice 'Yes' {
[System.Diagnostics.Process]::Start('http://www.wintellect.com/CS/files/folders/sample_files/entry3385.aspx')
}
}
}

}

function Start-PowerShell {

[CmdletBinding(DefaultParameterSetName='File')]
param (
[Parameter(ParameterSetName='File', Position=1)]
[String]$File,
[Parameter(ParameterSetName='ScriptBlock')]
[ScriptBlock]$Command,
[Parameter()]
[String]$PSConsoleFile,
[Parameter()]
[Switch]$STA,
[Parameter()]
[Switch]$NoProfile,
[Parameter()]
[Switch]$Elevated
)

$PowerShellArgs = @('-NoExit')

if ( $PSConsoleFile ) {
# A PSConsole file to start with: Use the ProviderPath
$ResolvedConsoleFile = (Resolve-Path $PSConsoleFile).ProviderPath
$PowerShellArgs += ('-PSConsoleFile',$ResolvedConsoleFile)
}

if ( $STA ) { $PowerShellArgs += '-Sta' }
if ( $NoProfile ) { $PowerShellArgs += '-NoProfile' }

if ( $File ) {
# A script to run: Use the ProviderPath
$ResolvedFile = (Resolve-Path $File).ProviderPath
}

# Encodes the command as Base64 and prepends a
# Set-Location command which puts us in the same dir
$CommandString = "Set-Location '$PWD'"

if ( $Command ) { $CommandString += "; $Command" }
if ( $File ) { $CommandString += "; &'$ResolvedFile'" }

$CommandBytes = [System.Text.Encoding]::Unicode.GetBytes($CommandString)
$CommandBase64 = [Convert]::ToBase64String($CommandBytes)

$PowerShellArgs += ('-EncodedCommand',$CommandBase64)

# Start-Process bitches if you are in HKLM:\ or something
# so we'll temporarily cd into $PSHOME then pop out

Push-Location $PSHOME

try {
if ( $Elevated ) { Start-ElevatedProcess 'powershell.exe'
$PowerShellArgs }
else { Start-Process 'powershell.exe' $PowerShellArgs }
}
finally {
Pop-Location
}

}




My System SpecsSystem Spec
Old 02-05-2009   #8 (permalink)
DavieH


 
 

Re: How can I run scipts with Admin privilege?

I guess you do that stuff in the profile script and I can open a Cmd-replica
shell by running PS from within another script with a -Command block to set
up my variables (the black background is undoubtably because of some options
that I haven't set). By default I use two Admin shells because I learned
from Vista the problems that arise from "Virtualisation" (the data that gets
into \VirtualStore) because of insufficient privilege (and NOT because of
program data in Program Files like Adobe with v8 of their Reader!).

SUGGESTION:

Allow Admin privilege on script files and their shortcuts via the context
menu only because an (inexperienced) user can do more damage in a shell that
is persistently open with Admin privilege!

DavieH
====

"Charlie Russel - MVP" wrote:
Quote:

> Exactly - no mistaking this one either!
>
> I use a multiline prompt, as well, left over from my days as an Oracle DBA
> and UNIX sysadmin. Has what account I'm running under, what my default
> database is, and what my current location (machine and path) is. Converting
> that from Korn shell was one of my first fun projects in PowerShell. But for
> elevated windows, I change the background/foreground as well. Just so
> there's no doubt!
>
> --
> Charlie.
> http://msmvps.com/blogs/xperts64
> http://mvp.support.microsoft.com/profile/charlie.russel
>
> "Karl Mitschke" <kmitschke@xxxxxx> wrote in message
> news:d66cd4c26245d8cb558b021e4ec9@xxxxxx
Quote:

> > Hello Charlie Russel - MVP,
> >
Quote:

> >> I simply open two powershell windows when I first log on - one
> >> standard user, one elevated. The elevated one has a maroon background,
> >> and has Administrator in the title bar.
> >>
> > Mine's much the same:
> >
> > Administrator PowerShell for the title, black background, a Yellow
> > "Admiminstratoe <path>" prompt, and magenta (inside company joke) for the
> > foreground
> >
> > Can't mistake it for my non admin shell!
> >
> >
>
>
My System SpecsSystem Spec
Old 02-05-2009   #9 (permalink)
DavieH


 
 

Re: How can I run scipts with Admin privilege?

Wow... there's a few things I don't understand, but I'll try it before asking!

DavieH
====

"Josh Einstein" wrote:
Quote:

> As for elevating once you're at a prompt though, check this out. I just
> added this to my Host.psm1 module. It depends on a utility by Wintellect
> called Elevate.exe but it's pretty convenient if you're sitting in a
> directory and think "dammit I forgot to elevate this process..."
My System SpecsSystem Spec
Old 02-06-2009   #10 (permalink)
DavieH


 
 

Re: How can I run scipts with Admin privilege?

OK - got there (wasn't sure about @ operator).

And Elevate.exe enables me to execute commands with admin privilege from a
shortcut to a file containing:-

Elevate powershell.exe -outputformat XML -nologo -WindowStyle Normal
-command { ...... }

Or provide an Admin shell (by adding -noexit).

thanks,
DavieH
====

"DavieH" wrote:
Quote:

> Wow... there's a few things I don't understand, but I'll try it before asking!
>
> DavieH
> ====
>
> "Josh Einstein" wrote:
Quote:

> > As for elevating once you're at a prompt though, check this out. I just
> > added this to my Host.psm1 module. It depends on a utility by Wintellect
> > called Elevate.exe but it's pretty convenient if you're sitting in a
> > directory and think "dammit I forgot to elevate this process..."
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Solved Lost Admin Privilege! Please Help Me! General Discussion
admin privilege Vista account administration
Detect admin privilege due to elevation? Vista security
Is it possible for a service to start a user app running with admin privilege? Vista General
Is it possible for a service to start a user app running with admin privilege? Vista security


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46