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

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

6 Comments

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
Try changing {name} in subprocess.call to {script_path}.
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.
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.
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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.