19 Dec 2014 @ 7:30 AM 

Checking if a variable has a value is something I do very often in my scripts.
Most of the times, I do this by checking if the variable equals to $null.
However, there is a more performance friendly way to do this…

First, let’s create a few variables, which have the four most common types:

1

Next, let’s verify the types, just to show you that check it in the way I will show you, works for most (if not all) variable types:

2

Now to actually check if the variables have values:

3

…and to show you it correctly tells me if the parameter does exist, yet has no value in it:

4

Posted By: Jeff Wouters
Last Edit: 19 Dec 2014 @ 07:30 AM

EmailPermalinkComments (0)
Tags
Categories: DuPSUG, PowerShell

 18 Dec 2014 @ 12:33 PM 

Today I was asked to look at a section of a batch script that checked if the command prompt or script was run with elevated permissions.
So I started both a normal and elevated CMD and started testing and scripting.
The solution that was used in the script utilized the WHOAMI command line tool, which is part of the Windows operating system.
However, we found that the script didn’t work on Windows 8 or any later Windows operating system.

@ECHO OFF
set IS_ELEVATED=0
whoami /groups | findstr /b /c:"Mandatory Label\High Mandatory Level" | findstr /c:"Enabled group" > nul: && set IS_ELEVATED=1
if %IS_ELEVATED% == 0 (
    echo You must run the command prompt as administrator...
    pause
    exit /b 1
) else {
    echo The command prompt is run as administrator...
    echo Place the code you want to execute in this else block
}

Instead of using the WHOAMI tool, you could also use the NET tool, which is also part of the Windows operating system.

@ECHO OFF
net session >nul 2>&1
if %errorLevel% == 0 (
    echo The command prompt is run as administrator...
    echo Place the code you want to execute in this else block
) else (
    echo You must run the command prompt as administrator to install.
    pause
    exit /b 1
)

The benefit of this is that, by using the NET tool, the check for an elevated prompt works from Windows XP up to Windows 10 (technical preview). :-)

Posted By: Jeff Wouters
Last Edit: 18 Dec 2014 @ 12:33 PM

EmailPermalinkComments (1)
Tags
Categories: DuPSUG, PowerShell, Script





 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 346
  • Comments » 503
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

About



    No Child Pages.

Contact



    No Child Pages.

Speaking



    No Child Pages.

Health Check



    No Child Pages.