It is currently March 28th, 2024, 11:38 pm

Execute commands of powershell script

Get help with creating, editing & fixing problems with skins
soyelrafa
Posts: 96
Joined: March 1st, 2017, 3:09 pm

Execute commands of powershell script

Post by soyelrafa »

Hello! I found on internet a Powershell script that allows you to controle Windows 10 virtual desktop function (create new desktops, delete them, change to concrete desktop,etc.) and I want to incorpore it to a skin. (Link)

I found a topic of jsmorley doing calls to a Ps1 file (Link), but I can't reproduce it to this script.

I try with:
[MeasureRun]
Measure=Plugin
Plugin=RunCommand
Program=PowerShell.exe
Parameter=-NoProfile -ExecutionPolicy Bypass -Command "& '.\VirtualDesktop.ps1' 'New-Desktop'"
OutputType=ANSI
DynamicVariables=1
But I can't get it to work. The only way I can make it works it's to create another .ps1 file with the command:

Rainmeter:

Code: Select all

[MeasureAñadirEscritorio]
Measure=PLUGIN
Plugin=RunCommand
Program=powershell.exe
Parameter=-File "#@#Scripts\VirtualDesktop\NewDesktopAndSwitch.ps1"
OutputType=ANSI
RegExpSubstitute=2
.PS1 file:

Code: Select all

. "$PSScriptRoot\VirtualDesktop.ps1"

$NewDesktop = New-Desktop | Switch-Desktop
Is there a way to make it work directly from Rainmeter? Thanks!
soyelrafa
Posts: 96
Joined: March 1st, 2017, 3:09 pm

Re: Execute commands of powershell script

Post by soyelrafa »

I get it to work creating a script file with if's and else's, and sending the properly option at the measure.

Example:

Rainmeter:

Code: Select all

[MeasureTest]
Measure=Plugin
Plugin=RunCommand
Program=PowerShell.exe
Parameter=-NoProfile -ExecutionPolicy Bypass -File "#@#Scripts\VirtualDesktop\Actions.ps1" -Command "'New'"
OutputType=ANSI
DynamicVariables=1
.Ps1 file:

Code: Select all

$Action = $args[1]

If ($Action -Match "New") {
$NewDesktop = New-Desktop
}
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm

Re: Execute commands of powershell script

Post by SilverAzide »

soyelrafa wrote:I get it to work creating a script file with if's and else's, and sending the properly option at the measure.
I don't think you need to do this, but I don't have time to verify the exact syntax for you. I think the issue with your original skin is that your powershell command syntax isn't quite right.

Instead of this:
Parameter=Powershell -NoProfile -ExecutionPolicy Bypass -Command "& '.\VirtualDesktop.ps1' 'New-Desktop'"

Try this:
Parameter=Powershell -NoProfile -ExecutionPolicy Bypass -Command "& .\VirtualDesktop.ps1 ; New-Desktop"

I think the logic is that you want to run the VirtualDesktop.ps1 script, then run the "New-Desktop" command, correct? To do this on one line you need to use a semi-colon between the two commands. (Note that I'm putting the powershell command on the Parameter line, if you do this, you need to leave the Program value blank.)
Gadgets Wiki GitHub More Gadgets...
soyelrafa
Posts: 96
Joined: March 1st, 2017, 3:09 pm

Re: Execute commands of powershell script

Post by soyelrafa »

SilverAzide wrote:I don't think you need to do this, but I don't have time to verify the exact syntax for you. I think the issue with your original skin is that your powershell command syntax isn't quite right.

Instead of this:
Parameter=Powershell -NoProfile -ExecutionPolicy Bypass -Command "& '.\VirtualDesktop.ps1' 'New-Desktop'"

Try this:
Parameter=Powershell -NoProfile -ExecutionPolicy Bypass -Command "& .\VirtualDesktop.ps1 ; New-Desktop"

I think the logic is that you want to run the VirtualDesktop.ps1 script, then run the "New-Desktop" command, correct? To do this on one line you need to use a semi-colon between the two commands. (Note that I'm putting the powershell command on the Parameter line, if you do this, you need to leave the Program value blank.)
Your logic is right I think. VirtualDesktop.ps1 have the command "New-Desktop" inside, so should be loaded first and then execute the command "New-Desktop". I tried with your code, but I can't get it to work.

Thanks for your help in any case!