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