Run Powershell script from shortcut as Admin, script crashes

I have a GUI script that runs just fine if I run it from my desktop, Right click > Run with Powershell.

If I create a link to the script so it can be run by double clicking the link it starts with Run As, requesting admin user/pass. The script starts and then immediately crashes.

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -windowstyle “hidden” -c “S:\Tools\CHDToolwithDymo.ps1”

If I remove Run as Admin, the script starts as it should. Applying Admin again crashes. If I run the program from right click, powershell, I am able to start the script as Admin and it runs without issue.

Any thoughts or history with this? I’ve seen a few sites that talk about how to enable this, but nothing seems to have the same error that I’m experiencing.

Thanks

 

It’s gonna be hard to recommend something meaningful without seeing your code.

Regardless of that: Why the command line option “-c”? The proper option would be “-File” I think.

right… code, makes sense.

I have a script to check for admin and if not prompt for elevated privledges. If I take it out the script runs from the shortcut, but when I leave it in the script starts and then crashes.

[pre]

If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{

Relaunch as an elevated process:

Start-Process powershell.exe “-File”,(’"{0}"’ -f $MyInvocation.MyCommand.Path) -Verb RunAs
exit
}

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

#New line variable - Can be added to help with formatting of output text
$NL = “`r`n”

.Net methods for hiding/showing the console in the background

Add-Type -Name Window -Namespace Console -MemberDefinition ’
[DllImport(“Kernel32.dll”)]
public static extern IntPtr GetConsoleWindow();

[DllImport(“user32.dll”)]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);

function Show-Console
{
$consolePtr = [Console.Window]::GetConsoleWindow()

Hide = 0,

ShowNormal = 1,

ShowMinimized = 2,

ShowMaximized = 3,

Maximize = 3,

ShowNormalNoActivate = 4,

Show = 5,

Minimize = 6,

ShowMinNoActivate = 7,

ShowNoActivate = 8,

Restore = 9,

ShowDefault = 10,

ForceMinimized = 11

[Console.Window]::ShowWindow($consolePtr, 4)
}

function Hide-Console
{
$consolePtr = [Console.Window]::GetConsoleWindow()
#0 hide
[Console.Window]::ShowWindow($consolePtr, 0)
}

#region Creates Base level form for the rest of the Buttons/Labels to be attached to.
$form = New-Object System.Windows.Forms.Form
$form.Text = ‘CHD Tool’
$form.Size = New-Object System.Drawing.Size(600,600)
$form.StartPosition = ‘CenterScreen’
$form.FormBorderStyle = ‘Fixed3D’
$form.MaximizeBox = $false
$form.BackColor = “#B3B8C0
$Icon = New-Object system.drawing.icon(“C:\Program Files\PropertyMngmt\rcmp_icon_hd.ico”)
$form.Icon = $Icon
#endregion

[/pre]

right… code, makes sense.

I have a script to check for admin and if not prompt for elevated privledges. If I take it out the script runs from the shortcut, but when I leave it in the script starts and then crashes.

[pre]

If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{

Relaunch as an elevated process:

Start-Process powershell.exe “-File”,(’"{0}"’ -f $MyInvocation.MyCommand.Path) -Verb RunAs
exit
}

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

#New line variable - Can be added to help with formatting of output text
$NL = “`r`n”

.Net methods for hiding/showing the console in the background

Add-Type -Name Window -Namespace Console -MemberDefinition ’
[DllImport(“Kernel32.dll”)]
public static extern IntPtr GetConsoleWindow();

[DllImport(“user32.dll”)]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);

function Show-Console
{
$consolePtr = [Console.Window]::GetConsoleWindow()

Hide = 0,

ShowNormal = 1,

ShowMinimized = 2,

ShowMaximized = 3,

Maximize = 3,

ShowNormalNoActivate = 4,

Show = 5,

Minimize = 6,

ShowMinNoActivate = 7,

ShowNoActivate = 8,

Restore = 9,

ShowDefault = 10,

ForceMinimized = 11

[Console.Window]::ShowWindow($consolePtr, 4)
}

function Hide-Console
{
$consolePtr = [Console.Window]::GetConsoleWindow()
#0 hide
[Console.Window]::ShowWindow($consolePtr, 0)
}

#region Creates Base level form for the rest of the Buttons/Labels to be attached to.
$form = New-Object System.Windows.Forms.Form
$form.Text = ‘Tool’
$form.Size = New-Object System.Drawing.Size(600,600)
$form.StartPosition = ‘CenterScreen’
$form.FormBorderStyle = ‘Fixed3D’
$form.MaximizeBox = $false
$form.BackColor = “#B3B8C0
$form.Icon = $Icon
#endregion

[/pre]