0

I have the below GUI for my application. I need to execute respective powershell script based on the menu selection


from tkinter import *
from tkinter.ttk import *
from time import strftime

# creating tkinter window
root = Tk()
root.title('Menu Demonstration')

# Creating Menubar
menubar = Menu(root)

# Adding File Menu and commands
file = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='InfoBlox', menu = file)
file.add_command(label ='Add CNAME record', command = None)
file.add_command(label ='Add A-record and PTR record', command = None)
file.add_separator()
file.add_command(label ='Exit', command = root.destroy)

# Adding Edit Menu and commands
edit = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Installation', menu = edit)
edit.add_command(label ='Chrome', command = None)


# Adding Help Menu
ad_ = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Active Directory', menu = ad_)
ad_.add_command(label ='Add server in group', command = None)


# display Menu
root.config(menu = menubar)
mainloop()

Please let me know how can I click the menu options to call and execute powershell scripts

Empty Coder
  • 589
  • 6
  • 19

1 Answers1

0

You can call the scripts using subprocess.call (from this answer) and use lambda functions to call them when clicking menu items:

import subprocess
import os.path

def run_script(name):
    script_dir = r"path\to\scripts"
    script_path = os.path.join(script_dir, name)
    subprocess.call(f"powershell -ExecutionPolicy Bypass -File {script_path}", shell = True)

# Adding File Menu and commands
file = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='InfoBlox', menu = file)
file.add_command(label ='Add CNAME record', command = lambda: run_script("cname.ps1"))
file.add_command(label ='Add A-record and PTR record', command = lambda: run_script("aptr.ps1"))
file.add_separator()
file.add_command(label ='Exit', command = root.destroy)

You may not need the -ExecutionPolicy Bypass but I did (relevant answer).

Henry
  • 3,472
  • 2
  • 12
  • 36
  • Thanks for the response. I tried the way you mentioned and call a sending email file to check but getting below error `The argument 'Send-email.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter. Windows PowerShell Copyright (C) 2016 Microsoft Corporation. All rights reserved` – Empty Coder May 05 '22 at 15:14
  • Try changing `{name}` in `subprocess.call` to `{script_path}`. – Henry May 05 '22 at 19:06
  • getting same error `Processing -File 'C:\Automation' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again.` – Empty Coder May 06 '22 at 09:14
  • Make sure you're using `script_path`, not `script_dir`. If that still doesn't work try printing `script_path` to see if it's what you're expecting. If it's not check your file name and directory. – Henry May 06 '22 at 10:53
  • Checked by printing script_path. problem was the name of my directory. it was having space like `"c:\script files\"`. i moved it to some other location now working fine. any solution for this path with space? – Empty Coder May 06 '22 at 14:46
  • Changing it to `f"powershell -ExecutionPolicy Bypass -File \"{script_path}\""` might work (I've surrounded the file name with escaped quotes) – Henry May 06 '22 at 14:56