第三版 OK 有大用
#!/usr/bin/python3
#
import subprocess
import getpass
import random
import tkinter as tk
from tkinter import messagebox
import string
import wmi
c = wmi.WMI()
current_user = ""
for s in c.Win32_ComputerSystem():
domain = s.Domain
print("Domain:", domain)
for u in c.Win32_ComputerSystem(domain=domain):
print("Username:", u.UserName)
current_user = u.UserName
# import socket
#
# hostname = socket.gethostname()
# print("当前用户是: ", current_user)
# current_user = "BBBB\\33333333"
current_user = current_user.upper()
current_user = current_user.strip()
position = current_user.find("BBBB\\")
if( position == 0 ):
current_user=current_user[9:] #从BBBB\\之后开始截取
def check_last_char(s):
last_char = s[-1] # 获取字符串的最后一个字符
if last_char.isalpha(): # 判断最后一个字符是否是字母
return 0
elif last_char.isdigit(): # 判断最后一个字符是否是数字
return 1
else:
return 2
last_char = check_last_char(current_user)
if( last_char == 2 ):
messagebox.showinfo("修改计算机名称", f"工号不能含有中文或特殊字符")
exit()
elif( last_char == 0 ): #结尾字母
end_str1 = str(random.randint(0, 9))
end_str2 = str(random.randint(0, 9))
elif( last_char == 1 ): #结尾数字
end_str1 = random.choice(string.ascii_uppercase)
end_str2 = random.choice(string.ascii_uppercase)
computer_name = f"JS-{current_user}{end_str1}{end_str2}"
# 构造WMIC命令
command = f"wmic computersystem where name='%computername%' call rename name='{computer_name}'"
# 执行命令
try:
# 使用subprocess调用WMIC命令
result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
messagebox.showinfo("修改计算机名称", f"计算机名称已修改为 {computer_name} ,请重启电脑")
print("计算机名称已成功更改为:", computer_name)
except subprocess.CalledProcessError as e:
print(f"发生错误: {e}")
print("命令输出:")
print(e.stdout.decode())
print("错误详情:")
print(e.stderr.decode())
messagebox.showinfo("修改计算机名称", f"修改计算机名称失败")
第二版
import subprocess
import getpass
import random
import tkinter as tk
from tkinter import messagebox
import string
# import socket
#
# hostname = socket.gethostname()
current_user = getpass.getuser()
print("当前用户是: ", current_user)
def check_last_char(s):
last_char = s[-1] # 获取字符串的最后一个字符
if last_char.isalpha(): # 判断最后一个字符是否是字母
return 0
elif last_char.isdigit(): # 判断最后一个字符是否是数字
return 1
else:
return 2
last_char = check_last_char(current_user)
if( last_char == 2 ):
messagebox.showinfo("修改计算机名称", f"工号不能含有中文或特殊字符")
exit()
elif( last_char == 0 ): #结尾字母
end_str1 = str(random.randint(0, 9))
end_str2 = str(random.randint(0, 9))
elif( last_char == 1 ): #结尾数字
end_str1 = random.choice(string.ascii_uppercase)
end_str2 = random.choice(string.ascii_uppercase)
computer_name = f"JS-{current_user}{end_str1}{end_str2}"
# 构造WMIC命令
command = f"wmic computersystem where name='%computername%' call rename name='{computer_name}'"
# 执行命令
try:
# 使用subprocess调用WMIC命令
result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
messagebox.showinfo("修改计算机名称", f"计算机名称已修改为 {computer_name} ,请重启电脑")
print("计算机名称已成功更改为:", computer_name)
except subprocess.CalledProcessError as e:
print(f"发生错误: {e}")
print("命令输出:")
print(e.stdout.decode())
print("错误详情:")
print(e.stderr.decode())
messagebox.showinfo("修改计算机名称", f"修改计算机名称失败")
第一版
import subprocess
import getpass
import tkinter as tk
current_user = getpass.getuser()
print("当前用户是: ", current_user)
def account_pwd_values():
# 获取Entry小部件中的文本值
account = account_entry.get()
root.destroy()
root = tk.Tk()
label1 = tk.Label(root, text=f"计算机名称只能以JS-{current_user}开头")
label1.grid(column=0,columnspan=2,row=0,padx=0,pady=0)
# 创建第一个输入框
account_label = tk.Label(root, text=f"JS-{current_user}")
account_label.grid(column=0,row=2,padx=0,pady=0)
account_entry = tk.Entry(root,bd=1,relief=tk.SOLID)
account_entry.grid(column=1,row=2,columnspan=2,padx=0,pady=0)
# 创建一个按钮,并绑定事件处理函数
button = tk.Button(root, text="修改", command=account_pwd_values)
button.grid(column=0,row=4,columnspan=2,padx=2,pady=2)
root.title(f'修改计算机名称')
root.geometry("280x100")
# 运行窗口的主循环
root.mainloop()
exit();
# 获取新的计算机名称
new_computer_name = input("请输入新的计算机名称: ")
# 构造WMIC命令
command = f"wmic computersystem where name='%computername%' call rename name='{new_computer_name}'"
# 执行命令
try:
# 使用subprocess调用WMIC命令
result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("计算机名称已成功更改为:", new_computer_name)
except subprocess.CalledProcessError as e:
print(f"发生错误: {e}")
print("命令输出:")
print(e.stdout.decode())
print("错误详情:")
print(e.stderr.decode())
# 提示用户可能需要重启计算机以完成更改
print("请注意:你可能需要重启计算机以完成名称更改。")