Example of Python controlling Windows plus domains

Source: Internet
Author: User

Just finished, haven't had time to test, rely on is netdom, if need package, can leave a message

The code is as follows Copy Code

Import pythoncom
Import WMI
Import OS
Import Platform

def joindomain (Domain,username,password,dns):
If not domain or not username or not password or not DNS:
Return False
# Change the work directory
Os.chdir (Os.path.dirname (__file__))
Currpath = Os.path.dirname (Os.path.abspath (__file__))
# Get OS Details
OsInfo = Platform.uname ()
If not osinfo[0] = = ' Windows ':
Print "This feature only supports windows!"
Return False
CmdLine = None
If osinfo[2].lower () = = ' XP ': # Windows XP
Netdomexe = Os.path.join (Currpath, "netdom", "XP", "Netdom.exe")
If not os.path.exists (Netdomexe):
Print "Netdom.exe not found!"
Return False
CmdLine = "./netdom/xp/netdom Join%s/domain:%s/userd:%s@%s/passwordd:%s"% (Osinfo[1],domain,username,domain, Password
Elif osinfo[2].lower () = = "7" and osinfo[4] = = ' x86 ': # windows 7 32bit
Netdomexe = Os.path.join (Currpath, "netdom", "x86", "Netdom.exe")
Netdommui = Os.path.join (Currpath, "netdom", "x86", "Netdom.exe.mui")
If not os.path.exists (Netdomexe) or not os.path.exists (Netdommui):
Print "Netdom.exe or Netdom.exe.mui not found!"
Return False
#copy the files
CmdLine = ' Copy '%s ' '%windir%system32 '/y '% netdomexe
Os.system (CmdLine)
CmdLine = ' Copy '%s ' '%windir%system32zh-cn '/y '% netdommui
Os.system (CmdLine)
CmdLine = ' Copy '%s ' '%windir%system32en-us '/y '% netdommui
Os.system (CmdLine)
CmdLine = "netdom join%s/domain:%s/userd:%s@%s/passwordd:%s"% (Osinfo[1],domain,username,domain,password)

Elif osinfo[2].lower () = "7" and osinfo[4] = = ' AMD64 ': # windows 7 64bit
Netdomexe = Os.path.join (Currpath, "netdom "," x64 "," Netdom.exe ")
Netdommui = Os.path.join (Currpath," netdom "," x64 "," Netdom.exe.mui ")
If not Os.path.exists (Netdomexe) or not os.path.exists (netdommui):
print ' Netdom.exe or Netdom.exe.mui not found! '
Return False
#copy the files
cmdline = ' copy '%s ' '%windir%system32 '/y '% netdomexe
Os.system (cmdline) cmdline = ' copy '%s '%windir%system32zh-cn '/y '% netdommui
Os.system (cmdline)
CmdLine = ' copy '%s '%windir%s ystem32en-us "/y '% netdommui
Os.system (cmdline)
CmdLine =" netdom join%s/domain:%s/userd:%s@%s/passwordd:%s " % (Osinfo[1],domain,username,domain,password)

# Change the DNS server
Wmiservice = WMI. WMI ()
Colnicconfigs = wmiservice.win32_networkadapterconfiguration (ipenabled = True)
Objnicconfig = Colnicconfigs[0]
returnvalue = Objnicconfig.setdnsserversearchorder (DNSServerSearchOrder = [DNS])
If not returnvalue[0] = = 0 or not returnvalue[1] = = 1:
Print "Set DNS server Failed"
Return False
# Flush DNS
Os.system ("Ipconfig/flushdns")

# Execute the cmdline of join domain
If CmdLine = None:
Os.system (CmdLine)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.