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 - Modify PowerShell prompt when running as admin

Reply
 
Old 10-05-2007   #1 (permalink)
Marc Scheuner


 
 

Modify PowerShell prompt when running as admin

Folks,

I'm trying to modify my prompt in PowerShell in case I'm running as
admin, just to signal to be cautious.

What I did is call a "DetectAdmin.ps1" script from my "Profile.ps1".
Part of it seems to work (colouring the background in dark red), but
the prompt function I defined (which should output "PS-ADMIN" instead
of just "PS ") doesn't "catch on" - it just seems to get ignored.

What am I missing??

Here's the code from DetectAdmin.ps1 ("borrowed" from some PowerShell
guru's site and slightly adapted):

----------------------------------------------------------------------------------------------------------------
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)

if
($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
{
$Host.UI.RawUI.WindowTitle = "ADMIN: " + $Host.UI.RawUI.WindowTitle
$Host.UI.RawUI.Backgroundcolor = "DarkRed"

function prompt
{
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()

"[PS-ADMIN] - User: " + $id.Name + "`n" + $(Get-Location) + $(if
($nestedpromptlevel -ge 1) { ">>" }) + "> "
}

clear-host
}
----------------------------------------------------------------------------------------------------------------

The "$Host.UI.RawUI.Backgroundcolor = DarkRed" seems to get
interpreted, but the "prompt" function I declare doesn't seem to work
- I must be missing something fundamental.......

Thanks for any hints and tips ! (I already read the other threads on
prompts, but those only cover the very basics and didn't help me find
a solution for my particular challenge here).

Marc

My System SpecsSystem Spec
Old 10-05-2007   #2 (permalink)
Jeff


 
 

Re: Modify PowerShell prompt when running as admin

On Oct 5, 12:09 pm, Marc Scheuner <no.s...@xxxxxx> wrote:
Quote:

> Folks,
>
> I'm trying to modify my prompt in PowerShell in case I'm running as
> admin, just to signal to be cautious.
>
> What I did is call a "DetectAdmin.ps1" script from my "Profile.ps1".
> Part of it seems to work (colouring the background in dark red), but
> the prompt function I defined (which should output "PS-ADMIN" instead
> of just "PS ") doesn't "catch on" - it just seems to get ignored.
>
> What am I missing??
>
> Here's the code from DetectAdmin.ps1 ("borrowed" from some PowerShell
> guru's site and slightly adapted):
>
> ----------------------------------------------------------------------------------------------------------------
> $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
> $p = New-Object System.Security.Principal.WindowsPrincipal($id)
>
> if
> ($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
> {
> $Host.UI.RawUI.WindowTitle = "ADMIN: " + $Host.UI.RawUI.WindowTitle
> $Host.UI.RawUI.Backgroundcolor = "DarkRed"
>
> function prompt
> {
> $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
>
> "[PS-ADMIN] - User: " + $id.Name + "`n" + $(Get-Location) + $(if
> ($nestedpromptlevel -ge 1) { ">>" }) + "> "
> }
>
> clear-host}
>
> ----------------------------------------------------------------------------------------------------------------
>
> The "$Host.UI.RawUI.Backgroundcolor = DarkRed" seems to get
> interpreted, but the "prompt" function I declare doesn't seem to work
> - I must be missing something fundamental.......
>
> Thanks for any hints and tips ! (I already read the other threads on
> prompts, but those only cover the very basics and didn't help me find
> a solution for my particular challenge here).
>
> Marc
The problem is the scope of your Prompt function; it is limited to the
block of your if statement. Adding "global:" to the name of the
function will put it in the correct scope:

function global:Prompt
{
# your ADMIN prompt code
}

I hope this helps. Run "Get-Help about_scope" for more information
about variable and function scope.

Jeff

My System SpecsSystem Spec
Old 10-14-2007   #3 (permalink)
Marc Scheuner


 
 

Re: Modify PowerShell prompt when running as admin

Hi Jeff,
Quote:

>The problem is the scope of your Prompt function; it is limited to the
>block of your if statement. Adding "global:" to the name of the
>function will put it in the correct scope:
>
>function global:Prompt
>{
> # your ADMIN prompt code
>}
Thanks - that's exactly what did it. Must re-read that chapter on
scope, I guess :-)

Marc
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
prompt for admin name and password Vista account administration
prompt for admin name and password Vista security
Can I Modify ADSI with Powershell??? PowerShell
How to modify "Program Files" from the command prompt with UAC on? Vista General
Is it possible to modify keymgr.dll entires via powershell? PowerShell


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