![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 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 Specs![]() |
| | #3 (permalink) |
| | 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 >} scope, I guess :-) Marc |
My System Specs![]() |
![]() |
| 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 | |||