johnlindquist / kit

Script Kit. Automate Anything.
https://scriptkit.com
MIT License
3.88k stars 135 forks source link

Cannot use powershell for scripts #1458

Open ramarivera opened 5 months ago

ramarivera commented 5 months ago

I have the following simple script

// Name: RecycleDefaultAppPool

import "@johnlindquist/kit";

function configure() {
  $.shell = "powershell.exe";
}

function showNotificationUsingPowershell(message: string) {
  console.log(`Set to ${$.shell}`);

  const script = `Add-Type -AssemblyName System.Windows.Forms
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon
$notifyIcon.Icon = [System.Drawing.SystemIcons]::Information
$notifyIcon.BalloonTipIcon = 'Info'
$notifyIcon.BalloonTipText = '${message}'
$notifyIcon.BalloonTipTitle = 'Notification'
$notifyIcon.Visible = $true
$notifyIcon.ShowBalloonTip(10000)`;

  $`${script}`;
}

configure();

console.log("Recycling DefaultAppPool");
console.log(`Set to ${$.shell}`);

let applicationPoolToRestartName = await arg({
  placeholder: "Name of the application pool to restart?",
  initialChoices: ["DefaultAppPool"],
});

dev();

showNotificationUsingPowershell(`Recycling ${applicationPoolToRestartName}...`);

However when I execute it, it tries to use bash, even tho I told it to use powershell and can see in the dev console that $.shell is set to powershell.

What am I doing wrong? 😢

ramarivera commented 5 months ago

Now this is fun lol

If I use instead

async function showNotificationUsingPowershell(message: string) {
  const script = `Add-Type -AssemblyName System.Windows.Forms
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon
$notifyIcon.Icon = [System.Drawing.SystemIcons]::Information
$notifyIcon.BalloonTipIcon = 'Info'
$notifyIcon.BalloonTipText = '${message}'
$notifyIcon.BalloonTipTitle = 'Notification'
$notifyIcon.Visible = $true
$notifyIcon.ShowBalloonTip(10000)`;

  const results = await term({
    command: script,
    shell: "powershell.exe",
  });

  console.log("RESULTS", script, results);
}

it does open powershell, but it executes the lines in the inverse order 🙃 e.g. $notifyIcon.ShowBalloonTip(10000) first

JosXa commented 5 months ago

Try await exec(script, { shell: 'powershell' }) for powershell, and the builtin notify helper for native notifications. It uses https://www.npmjs.com/package/node-notifier under the hood

ramarivera commented 5 months ago

Yeah the notifications wwere just a random example, because I needed something very bright and loud, akin to a console.log :D