Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running powershell script with admin rights #745

Open
JulienMaille opened this issue Jul 15, 2023 · 4 comments
Open

Running powershell script with admin rights #745

JulienMaille opened this issue Jul 15, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@JulienMaille
Copy link

JulienMaille commented Jul 15, 2023

I'm trying to change the brightness of my NUC leds at night and the only way I was able to do that is through WMI
I can call that from a powerwhell script, but it only works with admin permissions. Is there anything I can do?

2023-07-15 16:32:56 | Warn | ScriptHandler.Launch: Powershell Example's output does not indicate success: Get-WmiObject : Access denied‚ 
At character D:\Codez\NucLeds\AutoDark.ps1:31 : 9
+ $cisd = Get-WmiObject -Namespace "ROOT\WMI" -Class CISD_WMI
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation : (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
[int]$brightness = 64
if ( $Args[0] -eq "0" ) {
    $brightness = 5
}

$cisd = Get-WmiObject -Namespace "ROOT\WMI" -Class CISD_WMI

$bytes = [byte]0, 0, 0, $brightness;
$cisd.SetValueIndicatorOptionLedType([bitconverter]::ToInt32($bytes, 0)).Data.Bytes[0,1,2,3]
$bytes = [byte]2, 0, 0, $brightness;
$cisd.SetValueIndicatorOptionLedType([bitconverter]::ToInt32($bytes, 0)).Data.Bytes[0,1,2,3]

$cisd.AppNotify(0x1).Data
@JulienMaille JulienMaille added the enhancement New feature or request label Jul 15, 2023
@Spiritreader
Copy link
Member

Spiritreader commented Jul 18, 2023

Sadly not at the moment.
Auto Dark Mode cannot run as elevated or elevate commands at the moment.
Micrsoft Powertoys has an option to make the application run as administrator on demand.
We may be able to look into how they do it at some point and integrate that, but it's not on the short term list of features.


Is there anything I can do?

I may be able to offer a workaround though. I haven't tested this but I think this could potentially work:

What you can do is create a task in Windows Task Scheduler beforehand using its UI and configure your powershell script to run as elevated there.
https://www.digitalcitizen.life/use-task-scheduler-launch-programs-without-uac-prompts/

Then it should be possible to call that task in ADM without administrator rights within powershell: https://learn.microsoft.com/en-us/powershell/module/scheduledtasks/start-scheduledtask?view=windowsserver2019-ps

The flow would be something like this

Task (elevated, run as admin) <-calls- Windows Task Scheduler <-calls- Script <-calls- Auto Dark Mode

@JulienMaille
Copy link
Author

JulienMaille commented Jul 18, 2023

@Spiritreader Thanks for your suggested workaround. I have successfully created the scheduled tasks.
I'm now trying to run them from ADM with a powershell one-liner. I did not want to create a script that start the task that starts the other script.
I tried powershell -Command "Start-ScheduledTask -TaskName NucLedNight" from the console and it works.
but from ADM config

  Scripts:
  - Name: Powershell Example
    Command: powershell
    ArgsLight: [ -Command "Start-ScheduledTask -TaskName NucLedDay" ]
    ArgsDark: [ -Command "Start-ScheduledTask -TaskName NucLedNight" ]

It seems you had extra quotes around the argument and it fails:

2023-07-18 21:10:05 | Info | ScriptHandler.Launch: running Powershell Example: "powershell" "-Command "Start-ScheduledTask -TaskName NucLedDay"" 

@JulienMaille
Copy link
Author

JulienMaille commented Jul 18, 2023

ok, it seems I got it working with following syntax:

Enabled: true
Component:
  Scripts:
  - Name: Set NUC brightness
    Command: powershell
    ArgsLight:
     - -Command
     - "Start-ScheduledTask -TaskName NucLedDay"
    ArgsDark:
     - -Command
     - "Start-ScheduledTask -TaskName NucLedNight"
    AllowedSources: [Any]

@Spiritreader
Copy link
Member

Spiritreader commented Jul 18, 2023

It seems you had extra quotes around the argument and it fails:

the example you provided doesn't seem like it was from our documentation.

This is the original example:

  - Name: Powershell Example
    Command: powershell 
    ArgsLight: [C:\test.ps1, -message light -emotion 'happy']
    ArgsDark: [C:\test.ps1, -message dark -emotion 'happy']
    AllowedSources: [Any]

There are two "arguments", one is the script path, the other one are the arguments as a whole.
Powershell parses all parameters after the first one as a single argument.

This (what you posted earlier) is incorrect syntax:

  Scripts:
  - Name: Powershell Example
    Command: powershell
    ArgsLight: [ -Command "Start-ScheduledTask -TaskName NucLedDay" ]
    ArgsDark: [ -Command "Start-ScheduledTask -TaskName NucLedNight" ]

It uses one single argument but also quotes, which powershell won't understand.

What you probably meant to do is this:

Enabled: true
Component:
  Scripts:
  - Name: Set NUC brightness
    Command: powershell
    ArgsLight: ["-Command", "Start-ScheduledTask -TaskName NucLedDay"]
    ArgsDark: ["-Command", "Start-ScheduledTask -TaskName NucLedNight"]
    AllowedSources: [Any]

but it works either way. Your way is just the long way of defining lists in yaml, as it contracts to the bracket style if you minify it.

Never mind that though, I'm glad you got it working! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants