github.com/pmoroney/dnscontrol@v0.2.4-0.20171024134423-fad98f73f44a/providers/activedir/getzones_windows.go (about) 1 package activedir 2 3 import ( 4 "fmt" 5 "os/exec" 6 "strconv" 7 "strings" 8 "sync" 9 ) 10 11 var checkPS sync.Once 12 var psAvailible = false 13 14 func (c *adProvider) getRecords(domainname string) ([]byte, error) { 15 16 // If we are using PowerShell, make sure it is enabled 17 // and then run the PS1 command to generate the adzonedump file. 18 19 if !c.fake { 20 checkPS.Do(func() { 21 psAvailible = c.isPowerShellReady() 22 if !psAvailible { 23 fmt.Printf("\n\n\n") 24 fmt.Printf("***********************************************\n") 25 fmt.Printf("PowerShell DnsServer module not installed.\n") 26 fmt.Printf("See http://social.technet.microsoft.com/wiki/contents/articles/2202.remote-server-administration-tools-rsat-for-windows-client-and-windows-server-dsforum2wiki.aspx\n") 27 fmt.Printf("***********************************************\n") 28 fmt.Printf("\n\n\n") 29 } 30 }) 31 if !psAvailible { 32 return nil, fmt.Errorf("powershell module DnsServer not installed") 33 } 34 35 _, err := c.powerShellExec(c.generatePowerShellZoneDump(domainname), true) 36 if err != nil { 37 return []byte{}, err 38 } 39 } 40 // Return the contents of zone.*.json file instead. 41 return c.readZoneDump(domainname) 42 } 43 44 func (c *adProvider) isPowerShellReady() bool { 45 query, _ := c.powerShellExec(`(Get-Module -ListAvailable DnsServer) -ne $null`, true) 46 q, err := strconv.ParseBool(strings.TrimSpace(string(query))) 47 if err != nil { 48 return false 49 } 50 return q 51 } 52 53 func (c *adProvider) powerShellDoCommand(command string, shouldLog bool) error { 54 if c.fake { 55 // If fake, just record the command. 56 return c.powerShellRecord(command) 57 } 58 _, err := c.powerShellExec(command, shouldLog) 59 return err 60 } 61 62 func (c *adProvider) powerShellExec(command string, shouldLog bool) ([]byte, error) { 63 // log it. 64 err := c.logCommand(command) 65 if err != nil { 66 return nil, err 67 } 68 69 // Run it. 70 out, err := exec.Command("powershell", "-NoProfile", command).CombinedOutput() 71 if err != nil { 72 // If there was an error, log it. 73 c.logErr(err) 74 } 75 if shouldLog { 76 err = c.logOutput(string(out)) 77 if err != nil { 78 return []byte{}, err 79 } 80 } 81 82 // Return the result. 83 return out, err 84 }