Wednesday 15 May 2013

RunAs compatible with Windows XP, 7 and 8

Sometime ago I posted the powershell code for a 'RunAs' program to be able to run application with elevated privileges all from a central location.

I have updated it to work with all Windows version as well as fixed some bugs. The code is also a great place to start to learn more about forms which was the topic of my last post.

Save the code to as RunAs.ps1 or download it from here.

Then create a shortcut with the following target if you are using Window Vista/7/8 or Server 2008/R2/2012:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -command "& {Start-Process $psHome\powershell.exe -argumentlist '-windowstyle hidden -executionpolicy bypass -file C:\Temp\RunAs.ps1' -Verb RunAs}"
Create a shortcut with the following target if you are using Window XP or Server 2003:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -windowstyle hidden -file C:\Temp\RunAs.ps1

Replace C:\Temp\RunAs.ps1 with the path where you placed your script.

Here is the code:

<#
.SYNOPSIS
 Run application from a central location with elevated privileges. Choose your most used applications and save time.
.NOTES
 File Name: RunAs.ps1
 Author:  Felipe Binotto
 Email:  felipe.binotto@powershell.com
.LINK
 Script posted at:
 http://powershell.com
 http://fbinotto.blogspot.com
.USAGE
 Click on the + sign to add applications to the list.
 If you want to add an application that doesn't require arguments just leave the field blank.
 If you want to add an application that uses the Microsoft Management Console then select mmc.exe as the application and in the argument select the MSC file.
 Select an application and click on the - sign to exclude an application from the list.
 Click on the @ sign to send me an email with any questions or to report a bug.
.EXAMPLE
 Choose mmc.exe as application and compmgmt.msc as argument.
#>

function Return-DropDown([Object] $DropDown){
 $Choice = $DropDown.SelectedItem.ToString()

   foreach($Item in $xml.Items.Item){
   if($Item.app -eq $Choice){
   if($script:xp){
   if($Item.path.split(".")[-1] -eq "msc"){
   Start-Process mmc "$($Item.path)" -WorkingDirectory C:\ -credential $script:credential}
   elseif($Item.arguments){
   Start-Process $Item.path $Item.arguments -WorkingDirectory C:\ -credential $script:credential}
   else{Start-Process -FilePath $Item.path -WorkingDirectory C:\ -credential $script:credential}}
   else{
   if($Item.path.split(".")[-1] -eq "msc"){
   Start-Process mmc "$($Item.path)" -WorkingDirectory C:\}
   elseif($Item.arguments){
   Start-Process $Item.path $Item.arguments -WorkingDirectory C:\}
   else{Start-Process -FilePath $Item.path -WorkingDirectory C:\}}
 

}}

function Add-App(){
Get-Name
if($script:check){
$objForm = New-Object System.Windows.Forms.OpenFileDialog
$objForm.InitialDirectory = "C:\"
$objForm.Filter = "All Files (*.*)|*.*"
$objForm.Title = "Source"
$objForm.ShowHelp = $true
$show = $objForm.ShowDialog()
if($show.value__ -eq 1){

 Get-Arguments
 $path = $objForm.FileName
 $item = $xml.CreateElement("item")
 $Item.SetAttribute("App", $script:app)
 $Item.SetAttribute("Path", $path)
 $Item.SetAttribute("Arguments", $script:arg)
 $item.set_InnerText($app)
 $xml.get_DocumentElement().AppendChild($item)
 $xml | Export-Clixml "$env:userprofile\apps.xml"
 $DropDown.Items.Add($Item.app) | Out-Null
 $script:arg = $null
}}}
function Remove-App(){
for($i=0; $i -lt $xml.Items.Item.Count; $i++){
if($xml.Items.Item[$i].App -eq $DropDown.SelectedItem.ToString()){
$DropDown.Items.Remove($xml.Items.Item[$i].app)
$xml.Items.RemoveChild($xml.Items.item[$i])
$xml | Export-Clixml "$env:userprofile\apps.xml"}
}}

Function Get-Name(){
$Form = New-Object System.Windows.Forms.Form
$Form.width = 200
$Form.height = 90
$Form.startposition = "centerscreen"
$Form.Text = "App's Name"
$arg = new-object System.Windows.Forms.TextBox
$arg.Location = new-object System.Drawing.Size(10,5)
$arg.Size = new-object System.Drawing.Size(170,20)
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Text = "OK"
$OKButton.Add_Click({$script:app = $arg.Text; $script:check = $true; $Form.Close()})
$OKButton.Location = new-object System.Drawing.Size(50,35)
$OKButton.Size = new-object System.Drawing.Size(40,20)
$CButton = new-object System.Windows.Forms.Button
$CButton.Text = "CANCEL"
$CButton.Location = new-object System.Drawing.Size(95,35)
$CButton.Size = new-object System.Drawing.Size(70,20)
$CButton.Add_Click({$script:check = $false; $Form.Close()})
$Form.Controls.Add($CButton)
$Form.Controls.Add($arg)
$Form.Controls.Add($arglabel)
$Form.Controls.Add($OKButton)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()}
Function Get-Arguments(){
$Form = New-Object System.Windows.Forms.Form
$Form.width = 200
$Form.height = 90
$Form.startposition = "centerscreen"
$Form.Text = "Arguments"
$arg = new-object System.Windows.Forms.TextBox
$arg.Location = new-object System.Drawing.Size(10,5)
$arg.Size = new-object System.Drawing.Size(170,20)
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Text = "OK"
$OKButton.Add_Click({$script:arg = $arg.Text; $Form.Close()})
$OKButton.Location = new-object System.Drawing.Size(50,35)
$OKButton.Size = new-object System.Drawing.Size(40,20)
$NOButton = new-object System.Windows.Forms.Button
$NOButton.Text = "NO"
$NOButton.Location = new-object System.Drawing.Size(95,35)
$NOButton.Size = new-object System.Drawing.Size(40,20)
$NOButton.Add_Click({$Form.Close()})
$Form.Controls.Add($NOButton)
$Form.Controls.Add($arg)
$Form.Controls.Add($OKButton)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()}

Function Main(){
if((Get-WmiObject win32_operatingsystem | select -ExpandProperty version).substring(0,3) -lt 6.0)
{$script:xp = $true; $script:credential = Get-Credential}
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
if(Test-Path "$env:userprofile\apps.xml"){$xml = Import-Clixml "$env:userprofile\apps.xml"}
else{
$xml = @'
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <XD>&lt;items&gt;&lt;item App="CMD" Path="C:\Windows\System32\cmd.exe" Arguments=""&gt;CMD&lt;/item&gt;&lt;/items&gt;</XD>
</Objs>
'@ | Out-File "$env:userprofile\apps.xml"
$xml = Import-Clixml "$env:userprofile\apps.xml"}
$Form = New-Object System.Windows.Forms.Form
$Form.width = 270
$Form.height = 60
$Form.Text = "RunAs"
$Form.backcolor = "black"
$Form.maximumsize = New-Object System.Drawing.Size(270,60)
$Form.minimumsize = New-Object System.Drawing.Size(270,60)
$Form.startposition = "centerscreen"
$Form.KeyPreview = $True
$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {Return-DropDown($DropDown)}})
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$Form.Close()}})

$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,0)
$DropDown.Size = new-object System.Drawing.Size(160,30)
$DropDownArrayList = New-Object System.Collections.ArrayList
foreach($Item in $xml.Items.Item){
$DropDownArrayList.add($Item.App) | Out-Null}
$DropDownArrayList.sort()
ForEach ($Item in $DropDownArrayList) {
 $DropDown.Items.Add($Item) | Out-Null
}
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = new-object System.Drawing.Size(60,0)
$OKButton.Size = new-object System.Drawing.Size(40,25)
$OKButton.BackColor = "Transparent"
$OKButton.Text = "OK"
$OKButton.ForeColor = "White"
$OKButton.Add_Click({Return-DropDown($DropDown)})
$AddButton = new-object System.Windows.Forms.Button
$AddButton.Location = new-object System.Drawing.Size(40,0)
$AddButton.Size = new-object System.Drawing.Size(20,25)
$AddButton.BackColor = "Transparent"
$AddButton.Text = "+"
$AddButton.ForeColor = "White"
$AddButton.Add_Click({Add-App})
$RemoveButton = new-object System.Windows.Forms.Button
$RemoveButton.Location = new-object System.Drawing.Size(20,0)
$RemoveButton.Size = new-object System.Drawing.Size(20,25)
$RemoveButton.BackColor = "Transparent"
$RemoveButton.Text = "-"
$RemoveButton.ForeColor = "White"
$RemoveButton.Add_Click({Remove-App})
$EmailButton = new-object System.Windows.Forms.Button
$EmailButton.Location = new-object System.Drawing.Size(0,0)
$EmailButton.Size = new-object System.Drawing.Size(20,25)
$EmailButton.BackColor = "Transparent"
$EmailButton.Text = "@"
$EmailButton.ForeColor = "White"
$EmailButton.Add_Click({Start-Process "mailto:felipe.binotto@powershell.com"})
$Form.Controls.Add($DropDown)
$Form.Controls.Add($OKButton)
$Form.Controls.Add($RemoveButton)
$Form.Controls.Add($AddButton)
$Form.Controls.Add($EmailButton)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()}
Main


No comments:

Post a Comment