Thursday, May 24, 2018

Check Your Administrator Status in PowerShell

Most of the time, a script we've written for PowerShell may work like this:



 But then occasionally, this happens:



Whoops. What happened?

One thing to double-check is, Does our script require administrative access to work, and are we running PowerShell as an Administrator?

Here's how to check that:

([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")

 
The command is returning False, so no, this PowerShell instance was not launched as Administrator.

Of course, that's a long, hard-to-remember command, which means it's an excellent candidate for a function:

Function Check-Admin 
{  # Check Admin rights
([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")  
}  

 Stick that function into our profile, and we'll never have to remember that ugly command again.

Thursday, May 17, 2018

Choose Random Numbers with PowerShell

Do we need to choose a number from 1 to 100?

Do we need to choose five random numbers from 1 to 100?

Do we have PowerShell?

Then we are all set.

First, create an array of numbers from 1 to 100:

$array = (1..100)

Then let PowerShell randomly choose 5 numbers from the array:

Get-Random -InputObject $array -Count 5




And if we need those results in numerical order?

Get-Random -InputObject $array -Count 5 | Sort-Object



 
   

Tuesday, May 15, 2018

PowerShell: Forcing Confirmation

In a previous post, we saw how to check the Confirmation Preference, and how it works compare to a cmdlet's built-in confirmation level. But if we want to force a cmdlet to run with confirmation, we can use the '-Confirm' switch.

Here's how that works:

 

Like last time, we launched an instance of calculator.exe, found the process ID using Get-Process, then killed it with Stop-Process. PowerShell didn't hesitate to do as we commanded. But for safety's sake, let's add the '-Confirm' switch:



This time we had to take an extra step before the process was stopped. This feature would be useful in, for example, an interactive script designed for newer users, who might not be as comfortable taking permanent or destructive actions.

Saturday, May 5, 2018

Check the Confirmation Preference in PowerShell

All PowerShell cmdlets have an impact on a computer. Of course, some cmdlets have a bigger impact than others. A simple Get-* cmdlet typically just retrieves information as Read-Only, whereas a Remove-* cmdlet could bring a system to its knees.

The impact that a cmdlet can have is hard-coded into the cmdlet itself by the developer of the cmdlet, be it Microsoft or a 3rd-party developer. The developer determines if the impact is “High”, “Medium”, or “Low”. For example, removing a mailbox in Exchange is considered a HIGH impact, and Stop-Process is considered a MEDIUM impact.

But our PowerShell environment also has a ‘default’ impact setting:

                $ConfirmPreference



So when we enter a command into PowerShell, it compares the impact level of the cmdlet with our default setting. If the cmdlet’s impact level is greater than our $confirmpreference setting, then the cmdlet prompts us, asking, “Are you sure you want to do this?” Since our system’s set for “HIGH”, then all cmdlets will run without any prompt or confirmation. While being prompted after running cmdlets might be annoying, it may be wise for a PowerShell beginner to take precautions.

For example:

Here I launched an instance of calculator.exe, then grabbed the ID using ‘Get-Process.’ When I entered ‘Stop-Process <ID#>’, PowerShell compared my $confirmpreference setting (HIGH) with the cmdlet’s Impact setting (MEDIUM) and decided that I must know what I’m doing, so it killed the instance of calculator.exe without hesitation. But if I’m feeling skittish, I’ll might want to lower my $confirmpreference setting to a safer level:

                $ConfirmPreference = “low”



Let’s see what happens when I try to kill a new instance of calculator.exe:


Now PowerShell prompts me before doing anything with an impact higher than LOW.