Skip to main content

Check if your script are running in administrator context

Doing system changes via a script files will often require that you run it in the context of administrator.

Doing this with script I run myself is easy enough, I know that if it fails it is because I forgot to use "Run as administrator" when I started it (or did a poor job creating the script).
However when creating scripts that other people will run it will be a good idea to add a check in the script and give feedback to the person running it.
Doing this from Powershell:
You need to create a function to do this from Powershell, or integrate it in the script.

function Test-IsAdmin {
([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
}


More info HERE

 Doing this from a command line file (cmd):

 You can use "net session >nul 2>&1" to check and add one scenarios if success and another if failed. In my example, I just use the goto command since this gives a better structur to the script.

Example script that adds a line to the hosts. file (needs to be admin for this):


@echo off
goto check_Permissions

:check_Permissions
 echo.
 echo Administrative permissions required. Detecting permissions...
 echo.
    net session >nul 2>&1
    if %errorLevel% == 0 goto Success else goto Error
:Error
 echo Failure: Current permissions inadequate.
 echo.
 echo "! You need to run this as administrator"
 echo "! Right click the file and select Run As Administrator"
 echo.
goto END

:Success
 echo 192.168.10.10 pong.com >>  %Windir%\system32\drivers\etc\hosts.
 echo "! Changes successfull"
 echo.
:END
pause


Feedback when unsuccessfull:

 
Feedback when unsuccessfull:
 
 
And the end result of this script:-)
 
 
Just put in the commands or whatever you want in the :Success section of the script to be executed if it's running as administrator. And the message or commands you want to be executed if it's not running as administrator in the :Error section.
 
 


Popular posts from this blog

Changing Windows product key (license) for a MDT OS deployment task

Creating a new OS deployment task in MDT you will be asked to specify product key


That’s easy enough, but what if you type in the wrong key or just want to change it (add it) later? You might even have gotten a new MAK.

That is not as easy, you cannot do it from the graphical interface, you have to do it from one of the configuration files. Well you might to it from graphical interface by typing it in every time deploying a computer gets deployed but that is not a great solution.

MDT - The task sequence has been suspended. LiteTouch is trying to install applications

Every now and then I get this error message when fuelling a computer with MDT:
"The task sequence has been suspended. LiteTouch is trying to install applications. This cannot be performed in Windows PE."

How to get and use the Windows key from UEFI/BIOS

I found myself in the situation that I needed to get the Windows key out from the ACPI in the UEFI bios and preferable get to use it in a task sequence in MDT.

There are different ways of doing this, but I wanted to do this with little code and easy to understand.
There is a nice tool created to get the key on github, created by Christian Korneck, called get_win81key. This tool gives you the key in plane text. You can get it
HERE: https://github.com/christian-korneck/get_win8key


If I were supposed to do this manually, I now would just take that key and type it in with slmgr command or in the GUI. But I want to do this within a task in MDT.
Testing a couple of solutions without using the get_win8key with my great scripting guru colleague HÃ¥vard ( www.grondal.me) and figuring out that it cannot be done with default powershell stuff, it needs more code. Therefore, we quickly figured out that some use of the “get_win8key” files is the easiest way, but we need a way of setting the output fro…