Showing posts with label Function. Show all posts
Showing posts with label Function. Show all posts

Monday, April 1, 2013

Powershell - RunAsAdminAlways Function

Here is a function I use in many of my powershell scripts to check if they have been launched with elevated privileges. If it has then it just changes the window color and title. If it has not it is relaunched with those privileges.

If you create a PS1 file with all the code below and then replace everything below the "Stuff to be run in an elevated prompt goes below here." comment it will ensure that code is executed in an elevated command prompt.

Of course you could also always grab just the function and call it from one of your own existing script(s).

Function RunAsAdminAlways ($command) {
#*=============================================
#* Function: RunAsAdminAlways
#* Created: [02/15/2011]
#* Author: Charles Ulrich
#* Arguments: Should always be called with 
#* $myInvocation.MyCommand.Definition as the only argument
#* Ex: $y = RunAsAdminAlways ($myInvocation.MyCommand.Definition)
#*=============================================
#* Purpose: Checks if script is running as admin
#* and relaunches if not.
#*
#*=============================================

# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
  
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
  
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
    {
    # We are running "as Administrator" - so change the title and background color to indicate this
    $Host.UI.RawUI.WindowTitle = $command + "(Elevated)"
    $Host.UI.RawUI.BackgroundColor = "DarkBlue"
    clear-host
    }
else
    {
    # We are not running "as Administrator" - so relaunch as administrator
    Start-Process powershell -ArgumentList $command -verb "runas"
    
    # Exit from the current, unelevated, process
    exit
    }
}

$y = RunAsAdminAlways ($myInvocation.MyCommand.Definition)

# Stuff to be run in an elevated prompt goes below here.

Write-Host "Looks Good!"
Write-Host "Press any key to continue."

$z = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Friday, March 8, 2013

Powershell Multi-Select List Box Function

I use a bit of code for creating a multiple selection list box from an old technet article (http://technet.microsoft.com/en-us/library/ff730950.aspx) in a number of my powershell scripts.

Recently I was putting together a script on my new Windows 8 box and found that the function no longer returned what was selected. I ended up finding the solution to my problem here.

The issue seems to be related to the way Powershell 3.0 handles variable scopes. To make it work I had to add the script scope to the variable in the function.

Originally the variable was $x, now its $Script:x.

In case anyone finds themselves in the same boat i did here is an updated version of the function.

Function MultipleSelectionBox ($inputarray,$prompt,$listboxtype) {

# Taken from Technet - http://technet.microsoft.com/en-us/library/ff730950.aspx
# This version has been updated to work with Powershell v3.0.
# Had to replace $x with $Script:x throughout the function to make it work. 
# This specifies the scope of the X variable.  Not sure why this is needed for v3.
# http://social.technet.microsoft.com/Forums/en-SG/winserverpowershell/thread/bc95fb6c-c583-47c3-94c1-f0d3abe1fafc
#
# Function has 3 inputs:
#     $inputarray = Array of values to be shown in the list box.
#     $prompt = The title of the list box
#     $listboxtype = system.windows.forms.selectionmode (None, One, MutiSimple, or MultiExtended)

$Script:x = @()

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = $prompt
$objForm.Size = New-Object System.Drawing.Size(300,600) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True

$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {
        foreach ($objItem in $objListbox.SelectedItems)
            {$Script:x += $objItem}
        $objForm.Close()
    }
    })

$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,520)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"

$OKButton.Add_Click(
   {
        foreach ($objItem in $objListbox.SelectedItems)
            {$Script:x += $objItem}
        $objForm.Close()
   })

$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,520)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please make a selection from the list below:"
$objForm.Controls.Add($objLabel) 

$objListbox = New-Object System.Windows.Forms.Listbox 
$objListbox.Location = New-Object System.Drawing.Size(10,40) 
$objListbox.Size = New-Object System.Drawing.Size(260,20) 

$objListbox.SelectionMode = $listboxtype

$inputarray | ForEach-Object {[void] $objListbox.Items.Add($_)}

$objListbox.Height = 470
$objForm.Controls.Add($objListbox) 
$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

Return $Script:x
}