Quantcast
Channel: Active questions tagged windows - Super User
Viewing all articles
Browse latest Browse all 9224

Gaining administrator privileges from Function PowerShell

$
0
0

1. GetAdmin from function inside Script

function test {    GetAdmin    if ( $(fltmc).count -eq 3) { 'Not Admin' } else { 'Admin' }    write-host "`'$PSCommandPath`'"    pause; exit 0}function GetAdmin {   if ( $(fltmc).count -eq 3) {       $arg = "-NoProfile", "-ExecutionPolicy Bypass", "-File `"$PSCommandPath`""        Start -Verb RunAs powershell.exe -ArgumentList $arg; exit 0    }}test 

When GetAdmin function inside script, there is no problem. Everything is OK.


2. GetAdmin from function outside Script

I tried to make GetAdmin function calledable from script above, and place it in $env:PSModulePath.

So that I can calling it whenever I need, without including it in every scripts that needs admin elevate.

function GetAdmin {   if ( $(fltmc).count -eq 3) {       $arg = "-NoProfile", "-ExecutionPolicy Bypass", "-File `"$PSCommandPath`""        Start -Verb RunAs powershell.exe -ArgumentList $arg; exit 0    }}

And making sure the function was loadedgcm 'GetAdmin'

CommandType     Name                                               Version    Source-----------     ----                                               -------    ------Function        GetAdmin                                           0.0.1      bill

After that...

I tried to call the function from a .ps1 file

function test {    GetAdmin    if ( $(fltmc).count -eq 3) { 'Not Admin' } else { 'Admin' }    write-host "`'$PSCommandPath`'"    pause; exit 0}test 

But The script always auto closes. can't go back to test.ps1

Perhaps someone can help me.

making GetAdmin Function calledable.

I mean Get Admin elevate from outside /other script.

Not from inside script


Viewing all articles
Browse latest Browse all 9224

Trending Articles