Advertisement
Guest User

Untitled

a guest
May 10th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.76 KB | None | 0 0
  1.  
  2. require 'winrm'
  3.  
  4. module YP
  5.   module DevOps
  6.     class Ypwinrm
  7.  
  8.       @winrm_connector
  9.       attr_reader :pdcserver
  10.  
  11.       def initialize(username, password)
  12.         @pdcserver = "172.27.12.130"
  13.         opts = {
  14.           endpoint: "http://#{pdcserver}:5985/wsman",
  15.           user: username,
  16.           password: password
  17.         }
  18.         @winrm_connector = WinRM::Connection.new(opts)
  19.       end
  20.  
  21.       def remove_dns(servername)
  22.         unless servername.downcase.include?("*")
  23.           @winrm_connector.shell(:powershell) do |shell|
  24.             output = shell.run("Remove-DnsServerResourceRecord -ZoneName 'itops.ad.ypg.com' -Name '#{servername}' -RRType 'A' -Force") do |stdout, stderr|
  25.               STDOUT.print stdout
  26.               STDERR.print stderr
  27.             end
  28.             puts "The script exited with exit code #{output.exitcode}"
  29.           end
  30.         else
  31.           raise "Cannot use wildcard with delete functions!"
  32.         end
  33.       end
  34.  
  35.       def remove_computer_acc(servername)
  36.         unless servername.downcase.include?("*")
  37.           @winrm_connector.shell(:powershell) do |shell|
  38.             output = shell.run("Remove-ADComputer -Identity '#{servername}' -Confirm:$False") do |stdout, stderr|
  39.               STDOUT.print stdout
  40.               STDERR.print stderr
  41.             end
  42.             puts "The script exited with exit code #{output.exitcode}"
  43.           end
  44.         else
  45.           raise "Cannot use wildcard with delete functions!"
  46.         end
  47.       end
  48.  
  49.       def get_computer_acc(servername)
  50.         @winrm_connector.shell(:powershell) do |shell|
  51.           output = shell.run("Get-ADComputer -Identity '#{servername}' -Properties *") do |stdout, stderr|
  52.             STDOUT.print stdout
  53.             STDERR.print stderr
  54.           end
  55.           puts "The script exited with exit code #{output.exitcode}"
  56.         end
  57.       end
  58.  
  59.       def get_dns(servername)
  60.         @winrm_connector.shell(:powershell) do |shell|
  61.           output = shell.run("Get-DnsServerResourceRecord -ZoneName 'itops.ad.ypg.com' -Name '#{servername}' -RRType 'A'") do |stdout, stderr|
  62.             STDOUT.print stdout
  63.             STDERR.print stderr
  64.           end
  65.           puts "The script exited with exit code #{output.exitcode}"
  66.         end
  67.       end
  68.  
  69.       def powershell_command(command)
  70.         unless servername.downcase.include?("*")
  71.           @winrm_connector.shell(:powershell) do |shell|
  72.             output = shell.run(command) do |stdout, stderr|
  73.               STDOUT.print stdout
  74.               STDERR.print stderr
  75.             end
  76.             puts "The script exited with exit code #{output.exitcode}"
  77.           end
  78.         else
  79.           raise "Cannot use wildcard with powershell function!"
  80.         end
  81.       end
  82.  
  83.     end
  84.   end
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement