Class: Mixlibrary::Core::Windows::Features

Inherits:
Object
  • Object
show all
Defined in:
lib/mixlibrary/core/windows/features.rb

Instance Method Summary collapse

Constructor Details

#initialize(feature_name) ⇒ Features

Returns a new instance of Features.



9
10
11
12
13
14
15
16
# File 'lib/mixlibrary/core/windows/features.rb', line 9

def initialize(feature_name)
  if feature_name.to_s.strip.length == 0
    # It's nil, empty, or just whitespace
    raise "Feature name cannot be empty or just white space"
  end

  @feature_name=feature_name
end

Instance Method Details

#install_featureObject



28
29
30
31
32
33
34
35
36
# File 'lib/mixlibrary/core/windows/features.rb', line 28

def install_feature()
  Chef::Log.info("Installing feature:#{@feature_name}")
  script= <<-EOF
    import-module ServerManager;
    Add-WindowsFeature -Name "#{@feature_name}"
  EOF

  procobj = Mixlibrary::Core::Shell.windows_script_out!(:powershell, script)
end

#is_feature_available?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mixlibrary/core/windows/features.rb', line 38

def is_feature_available?()
  Chef::Log.info("Is feature available:#{@feature_name}")
  script= <<-EOF
    import-module ServerManager;
    $myvar=@(get-WindowsFeature -Name #{@feature_name})

    if($myvar.Count -eq 0){
        write-host "Found no matching features"
        exit 6
    }

  write-host "Printing Matching Features"
  foreach($myfeature in $myvar){
    $myfeature | ft -Property FeatureType,DisplayName,Name,InstallState,Installed | Out-Host
  }

  exit 5

  EOF

  procobj = Mixlibrary::Core::Shell.windows_script_out(:powershell, script)

  return procobj.stderr.empty? && procobj.stdout !~ /Removed/i && procobj.exitstatus==5
end

#is_installed?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mixlibrary/core/windows/features.rb', line 63

def is_installed?
  Chef::Log.info("Is feature installed:#{@feature_name}")
  #Need to handle this use case: Get-WindowsFeature Web* | Select Installed | % { Write-Host $_.Installed }
  #(cmd.stdout =~ /true/i) != nil ---There needs to be one true
  #(cmd.stdout =~ /False/i) == nil ---There needs to be no false
  #And no standard error
  script= <<-EOF
      import-module ServerManager;
      Get-WindowsFeature -Name #{@feature_name} | Select Installed | % { Write-Host $_.Installed }
  EOF

  procobj = Mixlibrary::Core::Shell.windows_script_out!(:powershell, script)
  procobj.stderr.empty? && (procobj.stdout =~ /False/i) == nil && (procobj.stdout =~ /true/i) != nil

end

#remove_featureObject



18
19
20
21
22
23
24
25
26
# File 'lib/mixlibrary/core/windows/features.rb', line 18

def remove_feature()
  Chef::Log.info("Removing feature:#{@feature_name}")
  script= <<-EOF
    import-module ServerManager;
    Remove-WindowsFeature -Name "#{@feature_name}"
  EOF

  procobj = Mixlibrary::Core::Shell.windows_script_out!(:powershell, script)
end