Module: BeakerWindows::WindowsFeature
- Included in:
- Beaker::TestCase
- Defined in:
- lib/beaker-windows/windows_feature.rb
Instance Method Summary collapse
-
#get_windows_features_on(host, opts = {}) ⇒ Object
Retrieve a list of Windows roles and features from a host.
-
#install_windows_feature_on(host, feature_name, opts = {}) ⇒ Object
Install a Windows role or feature on a host.
-
#remove_windows_feature_on(host, feature_name, opts = {}) ⇒ Object
Remove a Windows role or feature on a host.
Instance Method Details
#get_windows_features_on(host, opts = {}) ⇒ Object
Retrieve a list of Windows roles and features from a host. The list can be filtered to available or installed.
Attributes
-
host- A Windows Beaker host running PowerShell 3 or greater. -
opts:filter- Filter the list of Windows features.-
:all- Do not filter anything from the list. (Default) -
:available- Filter the list to only the available Windows features. -
:installed- Filter the list to only the installed Windows features.
-
Returns
Array - An array of strings representing Windows features.
Raises
ArgumentError - An invalid filter was specified. RuntimeError - The host does not have PowerShell 3 or greater available.
Example
get_windows_features_on(host) get_windows_features_on(host, :filter => :available) get_windows_features_on(host, :filter => :installed)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/beaker-windows/windows_feature.rb', line 29 def get_windows_features_on(host, opts={}) # Init opts[:filter] ||= :all ps_cmd = 'Get-WindowsFeature' # Filter features case opts[:filter] when :available ps_cmd << ' | Where { \$_.Installed -Eq \$false }' when :installed ps_cmd << ' | Where { \$_.Installed -Eq \$true }' else = 'Unknown filter! Specify :all, :available or :installed.' raise(ArgumentError, ) unless opts[:filter] == :all end # Select only the feature name ps_cmd << ' | Select -ExpandProperty Name' # Parse output result = on(host, exec_ps_cmd(ps_cmd), :accept_all_exit_codes => true) raise(RuntimeError, 'This method requires PowerShell 3 or greater!') if result.exit_code == 1 return result.stdout.rstrip.split("\n") end |
#install_windows_feature_on(host, feature_name, opts = {}) ⇒ Object
Install a Windows role or feature on a host.
Attributes
-
host- A Windows Beaker host running PowerShell 3 or greater. -
feature_name- The name of the Windows feature to install. (NOT THE DISPLAY NAME!) -
opts:suppress_fail- Suppress raising exception on feature installation failure.-
:true- Suppress the raising a RuntimeError exception. -
:false- Allow RuntimeError to be raised if feature fails to install. (Default)
-
Raises
RuntimeError - Failed to install the feature. RuntimeError - Invalid feature name or incorrect PowerShell version!
Example
install_windows_feature_on(host, ‘Print-Server’) install_windows_feature_on(host, ‘Bad-Feature’, :suppress_fail => true)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/beaker-windows/windows_feature.rb', line 76 def install_windows_feature_on(host, feature_name, opts={}) # Init opts[:suppress_fail] ||= false ps_cmd = "(Install-WindowsFeature -Name '#{feature_name}' -ErrorAction 'Stop').Success" # Parse output result = on(host, exec_ps_cmd(ps_cmd), :accept_all_exit_codes => true) unless opts[:suppress_fail] raise(RuntimeError, 'Invalid feature name or incorrect PowerShell version!') if result.exit_code == 1 raise(RuntimeError, 'Failed to install feature!') unless result.stdout =~ /True/ end end |
#remove_windows_feature_on(host, feature_name, opts = {}) ⇒ Object
Remove a Windows role or feature on a host.
Attributes
-
host- A Windows Beaker host running PowerShell 3 or greater. -
feature_name- The name of the Windows feature to remove. (NOT THE DISPLAY NAME!) -
opts:suppress_fail- Suppress raising exception on feature installation failure.-
:true- Suppress the raising a RuntimeError exception. -
:false- Allow RuntimeError to be raised if feature fails to be removed. (Default)
-
Raises
RuntimeError - Failed to remove the feature. RuntimeError - Invalid feature name or incorrect PowerShell version!
Example
remove_windows_feature_on(host, ‘Print-Server’) remove_windows_feature_on(host, ‘Bad-Feature’, :suppress_fail => true)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/beaker-windows/windows_feature.rb', line 110 def remove_windows_feature_on(host, feature_name, opts={}) # Init opts[:suppress_fail] ||= false ps_cmd = "(Remove-WindowsFeature -Name '#{feature_name}' -ErrorAction 'Stop').Success" # Parse output result = on(host, exec_ps_cmd(ps_cmd), :accept_all_exit_codes => true) unless opts[:suppress_fail] raise(RuntimeError, 'Invalid feature name or incorrect PowerShell version!') if result.exit_code == 1 raise(RuntimeError, 'Failed to remove feature!') unless result.stdout =~ /True/ end end |