1

I want to delete DHCP Reservations using a Python script and have the following code:

import tkinter as tk
from tkinter import messagebox
import subprocess

def delete_reservation():
    dhcp_server = "My DHCP-Server"
    scope = "Scope-IPAddress"
    ip_reservation = entry_ip.get().strip()

    if not ip_reservation:
        messagebox.showwarning("Error", "Please enter a IP-Address.")
        return

    try:
        ps_command = (
            f"Remove-DhcpServerv4Reservation "
            f"-ComputerName {dhcp_server} "
            f"-ScopeId {scope} "
            f"-IPAddress {ip_reservation} "
            f"-Confirm:$false"
        )

        subprocess.run(["powershell", "-Command", ps_command], check=True)
        messagebox.showinfo("Success", f"Reservation {ip_reservation} Has been deletes successfully")
    except subprocess.CalledProcessError as e:
        messagebox.showerror("Error", f"Couldn't delete reservation:\n{e}")

root = tk.Tk()
root.title("Delete DHCP-Reservation")
root.geometry("350x150")

tk.Label(root, text="IP-Address of Reservation:").pack(pady=10)
entry_ip = tk.Entry(root, width=30)
entry_ip.pack()

tk.Button(root, text="Delete", command=delete_reservation, bg="#d9534f", fg="white").pack(pady=20)

root.mainloop()

Somehow it wont execute. The error im getting is "... returned non-zero exit status 1."

2
  • 1
    I got it working leaving out the ScopeId parameter. You can not use ScopeId and IPAddress at the same time: they are in different parameter sets. Another cause might be that you must run the script in an elevated session. But AFAIK that is only an issue when running locally on the DHCP server. Commented Nov 19 at 11:07
  • It worked! Thanks alot. Commented Nov 20 at 13:01

0

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.