Class: SpecInfra::Command::Windows

Inherits:
Object
  • Object
show all
Defined in:
lib/specinfra/command/windows.rb

Defined Under Namespace

Classes: NotSupportedError

Constant Summary collapse

REGISTRY_KEY_TYPES =
{
  :type_string       => 'String',
  :type_binary       => 'Binary',
  :type_dword        => 'DWord',
  :type_qword        => 'QWord',
  :type_multistring  => 'MultiString',
  :type_expandstring => 'ExpandString'
}

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



14
15
16
17
# File 'lib/specinfra/command/windows.rb', line 14

def method_missing method, *args
  raise NotSupportedError.new "#{method} currently not supported in Windows os" if method.to_s =~ /^check_.+/
  super(method, *args)
end

Instance Method Details

#check_32bit_enabled(name) ⇒ Object



329
330
331
332
333
334
# File 'lib/specinfra/command/windows.rb', line 329

def check_32bit_enabled(name)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindIISAppPool -name '#{name}').enable32BitAppOnWin64 -eq $true"
  end
end

#check_access_by_user(file, user, access) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/specinfra/command/windows.rb', line 73

def check_access_by_user(file, user, access)
  case access
  when 'r'
    check_readable(file, user)
  when 'w'
    check_writable(file, user)
  when 'x'
    check_executable(file, user)
  end
end

#check_belonging_group(user, group) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/specinfra/command/windows.rb', line 177

def check_belonging_group(user, group)
  user_id, user_domain =  user
  group_id, group_domain =  group
  Backend::PowerShell::Command.new do
    using 'find_user.ps1'
    using 'find_group.ps1'
    using 'find_usergroup.ps1'
    exec "(FindUserGroup -userName '#{user_id}'#{user_domain.nil? ? "" : " -userDomain '#{user_domain}'"} -groupName '#{group_id}'#{group_domain.nil? ? "" : " -groupDomain '#{group_domain}'"}) -ne $null"
  end
end

#check_directory(dir) ⇒ Object



47
48
49
50
51
52
# File 'lib/specinfra/command/windows.rb', line 47

def check_directory(dir)
  cmd = item_has_attribute dir, 'Directory'
  Backend::PowerShell::Command.new do
    exec cmd
  end
end

#check_enabled(service, level = nil) ⇒ Object



127
128
129
130
131
132
# File 'lib/specinfra/command/windows.rb', line 127

def check_enabled(service, level=nil)
  Backend::PowerShell::Command.new do
    using 'find_service.ps1'
    exec "(FindService -name '#{service}').StartMode -eq 'Auto'"
  end
end

#check_executable(file, by_whom) ⇒ Object



98
99
100
101
102
103
# File 'lib/specinfra/command/windows.rb', line 98

def check_executable(file, by_whom)
  Backend::PowerShell::Command.new do
    using 'check_file_access_rules.ps1'
    exec "CheckFileAccessRules -path '#{file}' -identity '#{get_identity by_whom}' -rules @('FullControl', 'Modify', 'ReadAndExecute', 'ExecuteFile')"
  end
end

#check_file(file) ⇒ Object



19
20
21
22
23
24
# File 'lib/specinfra/command/windows.rb', line 19

def check_file(file)
  cmd = item_has_attribute file, 'Archive'
  Backend::PowerShell::Command.new do
    exec cmd
  end
end

#check_file_contain(file, pattern) ⇒ Object



54
55
56
57
58
# File 'lib/specinfra/command/windows.rb', line 54

def check_file_contain(file, pattern)
  Backend::PowerShell::Command.new do
    exec %Q![[Io.File]::ReadAllText("#{file}") -match '#{convert_regexp(pattern)}'!
  end
end

#check_file_contain_within(file, pattern, from = nil, to = nil) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/specinfra/command/windows.rb', line 60

def check_file_contain_within file, pattern, from=nil, to=nil
  from ||= '^'
  to ||= '$'
  Backend::PowerShell::Command.new do
    using 'crop_text.ps1'
    exec %Q!(CropText -text ([Io.File]::ReadAllText("#{file}")) -fromPattern '#{convert_regexp(from)}' -toPattern '#{convert_regexp(to)}') -match '#{pattern}'!
  end
end

#check_file_hidden(file) ⇒ Object



26
27
28
29
30
31
# File 'lib/specinfra/command/windows.rb', line 26

def check_file_hidden(file)
  cmd = item_has_attribute file, 'Hidden'
  Backend::PowerShell::Command.new do
    exec cmd
  end
end

#check_file_readonly(file) ⇒ Object



33
34
35
36
37
38
# File 'lib/specinfra/command/windows.rb', line 33

def check_file_readonly(file)
  cmd = item_has_attribute file, 'ReadOnly'
  Backend::PowerShell::Command.new do
    exec cmd
  end
end

#check_file_system(file) ⇒ Object



40
41
42
43
44
45
# File 'lib/specinfra/command/windows.rb', line 40

def check_file_system(file)
  cmd = item_has_attribute file, 'System'
  Backend::PowerShell::Command.new do
    exec cmd
  end
end

#check_file_version(name, version) ⇒ Object



254
255
256
257
# File 'lib/specinfra/command/windows.rb', line 254

def check_file_version(name,version)
  cmd = "((Get-Command '#{name}').FileVersionInfo.ProductVersion -eq '#{version}') -or ((Get-Command '#{name}').FileVersionInfo.FileVersion -eq '#{version}')"
  Backend::PowerShell::Command.new { exec cmd }
end

#check_group(group) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/specinfra/command/windows.rb', line 169

def check_group(group)
  group_id, domain =  group
  Backend::PowerShell::Command.new do
    using 'find_group.ps1'
    exec "(FindGroup -groupName '#{group_id}'#{domain.nil? ? "" : " -domain '#{domain}'"}) -ne $null"
  end
end

#check_identity_type(name, type) ⇒ Object



350
351
352
353
354
355
# File 'lib/specinfra/command/windows.rb', line 350

def check_identity_type(name, type)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindIISAppPool -name '#{name}').processModel.identityType -eq '#{type}'"
  end
end

#check_idle_timeout(name, minutes) ⇒ Object



343
344
345
346
347
348
# File 'lib/specinfra/command/windows.rb', line 343

def check_idle_timeout(name, minutes)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindIISAppPool -name '#{name}').processModel.idleTimeout.Minutes -eq #{minutes}"
  end
end

#check_iis_app_pool(name) ⇒ Object



315
316
317
318
319
320
# File 'lib/specinfra/command/windows.rb', line 315

def check_iis_app_pool(name)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "@(FindIISAppPool -name '#{name}').count -gt 0"
  end
end

#check_iis_app_pool_dotnet(name, dotnet) ⇒ Object



322
323
324
325
326
327
# File 'lib/specinfra/command/windows.rb', line 322

def check_iis_app_pool_dotnet(name, dotnet)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindIISAppPool -name '#{name}').managedRuntimeVersion -match 'v#{dotnet}'"
  end
end

#check_iis_website_app_pool(name, app_pool) ⇒ Object



280
281
282
283
284
285
# File 'lib/specinfra/command/windows.rb', line 280

def check_iis_website_app_pool(name, app_pool)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindIISWebsite -name '#{name}').applicationPool -match '#{app_pool}'"
  end
end

#check_iis_website_application(name, app, pool, physicalPath) ⇒ Object



308
309
310
311
312
313
# File 'lib/specinfra/command/windows.rb', line 308

def check_iis_website_application(name, app, pool, physicalPath)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindSiteApplication -name '#{name}' -app '#{app}' -pool '#{pool}' -physicalPath '#{physicalPath}') -eq $true"
  end
end

#check_iis_website_binding(name, port, protocol, ipAddress, hostHeader) ⇒ Object



294
295
296
297
298
299
# File 'lib/specinfra/command/windows.rb', line 294

def check_iis_website_binding(name, port, protocol, ipAddress, hostHeader)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindSiteBindings -name '#{name}' -protocol '#{protocol}' -hostHeader '#{hostHeader}' -port #{port} -ipAddress '#{ipAddress}').count -gt 0"
  end
end

#check_iis_website_enabled(name) ⇒ Object



259
260
261
262
263
264
# File 'lib/specinfra/command/windows.rb', line 259

def check_iis_website_enabled(name)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindIISWebsite -name '#{name}').serverAutoStart -eq $true"
  end
end

#check_iis_website_installed(name) ⇒ Object



266
267
268
269
270
271
# File 'lib/specinfra/command/windows.rb', line 266

def check_iis_website_installed(name)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "@(FindIISWebsite -name '#{name}').count -gt 0"
  end
end

#check_iis_website_path(name, path) ⇒ Object



287
288
289
290
291
292
# File 'lib/specinfra/command/windows.rb', line 287

def check_iis_website_path(name, path)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "[System.Environment]::ExpandEnvironmentVariables( ( FindIISWebsite -name '#{name}' ).physicalPath ).replace('\\', '/' ) -eq ('#{path}'.trimEnd('/').replace('\\', '/'))"
  end
end

#check_iis_website_running(name) ⇒ Object



273
274
275
276
277
278
# File 'lib/specinfra/command/windows.rb', line 273

def check_iis_website_running(name)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindIISWebsite -name '#{name}').state -eq 'Started'"
  end
end

#check_iis_website_virtual_dir(name, vdir, path) ⇒ Object



301
302
303
304
305
306
# File 'lib/specinfra/command/windows.rb', line 301

def check_iis_website_virtual_dir(name, vdir, path)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindSiteVirtualDir -name '#{name}' -vdir '#{vdir}' -path '#{path}') -eq $true"
  end
end

#check_installed(package, version = nil) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/specinfra/command/windows.rb', line 105

def check_installed(package, version=nil)
  version_selection = version.nil? ? "" : "-appVersion '#{version}'"
  Backend::PowerShell::Command.new do
    using 'find_installed_application.ps1'
    exec "(FindInstalledApplication -appName '#{package}' #{version_selection}) -eq $true"
  end
end

#check_listening(port) ⇒ Object



147
148
149
150
151
152
# File 'lib/specinfra/command/windows.rb', line 147

def check_listening(port)
  Backend::PowerShell::Command.new do
    using 'is_port_listening.ps1'
    exec "IsPortListening -portNumber #{port}"
  end
end

#check_listening_with_protocol(port, protocol) ⇒ Object



154
155
156
157
158
159
# File 'lib/specinfra/command/windows.rb', line 154

def check_listening_with_protocol(port, protocol)
  Backend::PowerShell::Command.new do
    using 'is_port_listening.ps1'
    exec "IsPortListening -portNumber #{port} -protocol '#{protocol}'"
  end
end

#check_managed_pipeline_mode(name, mode) ⇒ Object



336
337
338
339
340
341
# File 'lib/specinfra/command/windows.rb', line 336

def check_managed_pipeline_mode(name, mode)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindIISAppPool -name '#{name}').managedPipelineMode -eq '#{mode}'"
  end
end

#check_periodic_restart(name, minutes) ⇒ Object



371
372
373
374
375
376
# File 'lib/specinfra/command/windows.rb', line 371

def check_periodic_restart(name, minutes)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindIISAppPool -name '#{name}').recycling.periodicRestart.time.TotalMinutes -eq #{minutes}"
  end
end

#check_process(process) ⇒ Object



141
142
143
144
145
# File 'lib/specinfra/command/windows.rb', line 141

def check_process(process)
  Backend::PowerShell::Command.new do
    exec "(Get-Process '#{process}') -ne $null"
  end
end

#check_reachable(host, port, proto, timeout) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/specinfra/command/windows.rb', line 211

def check_reachable(host, port, proto, timeout)
    if port.nil?
       Backend::PowerShell::Command.new do
        exec "(New-Object System.Net.NetworkInformation.Ping).send('#{host}').Status -eq 'Success'"
       end
    else
      Backend::PowerShell::Command.new do
       using 'is_remote_port_listening.ps1'
       exec"(IsRemotePortListening -hostname #{host} -port #{port} -timeout #{timeout} -proto #{proto}) -eq $true"
      end
    end
end

#check_readable(file, by_whom) ⇒ Object



84
85
86
87
88
89
# File 'lib/specinfra/command/windows.rb', line 84

def check_readable(file, by_whom)
  Backend::PowerShell::Command.new do
    using 'check_file_access_rules.ps1'
    exec "CheckFileAccessRules -path '#{file}' -identity '#{get_identity by_whom}' -rules @('FullControl', 'Modify', 'ReadAndExecute', 'Read', 'ListDirectory')"
  end
end

#check_registry_key(key_name, key_property = {}) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/specinfra/command/windows.rb', line 188

def check_registry_key(key_name, key_property = {})
  if key_property.empty?
    cmd = "(Get-Item 'Registry::#{key_name}') -ne $null"
  else
    if key_property.key? :value
      value = convert_key_property_value key_property
      cmd = "(Compare-Object (Get-Item 'Registry::#{key_name}').GetValue('#{key_property[:name]}') #{value}) -eq $null"
    else
      cmd = "(Get-Item 'Registry::#{key_name}').GetValueKind('#{key_property[:name]}') -eq '#{REGISTRY_KEY_TYPES[key_property[:type]]}'"
    end
  end
  Backend::PowerShell::Command.new { exec cmd }
end

#check_resolvable(name, type) ⇒ Object



202
203
204
205
206
207
208
209
# File 'lib/specinfra/command/windows.rb', line 202

def check_resolvable(name, type)
  if type == "hosts"
      cmd = "@(Select-String -path (Join-Path -Path $($env:windir) -ChildPath 'system32/drivers/etc/hosts') -pattern '#{name}\\b').count -gt 0"
  else
      cmd = "@([System.Net.Dns]::GetHostAddresses('#{name}')).count -gt 0"
  end
  Backend::PowerShell::Command.new { exec cmd }
end

#check_running(service) ⇒ Object



134
135
136
137
138
139
# File 'lib/specinfra/command/windows.rb', line 134

def check_running(service)
  Backend::PowerShell::Command.new do
    using 'find_service.ps1'
    exec "(FindService -name '#{service}').State -eq 'Running'"
  end
end

#check_scheduled_task(name) ⇒ Object



378
379
380
381
382
383
# File 'lib/specinfra/command/windows.rb', line 378

def check_scheduled_task(name)
  Backend::PowerShell::Command.new do
    using 'find_scheduled_task.ps1'
    exec "(FindScheduledTask -name '#{name}').TaskName -eq '\\#{name}'"
  end
end

#check_service_installed(service) ⇒ Object



113
114
115
116
117
118
# File 'lib/specinfra/command/windows.rb', line 113

def check_service_installed(service)
  Backend::PowerShell::Command.new do
    using 'find_service.ps1'
    exec "@(FindService -name '#{service}').count -gt 0"
  end
end

#check_service_start_mode(service, mode) ⇒ Object



120
121
122
123
124
125
# File 'lib/specinfra/command/windows.rb', line 120

def check_service_start_mode(service, mode)
  Backend::PowerShell::Command.new do
    using 'find_service.ps1'
    exec  "'#{mode}' -match (FindService -name '#{service}').StartMode -and (FindService -name '#{service}') -ne $null"
  end
end

#check_user(user) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/specinfra/command/windows.rb', line 161

def check_user(user)
  user_id, domain =  user
  Backend::PowerShell::Command.new do
    using 'find_user.ps1'
    exec "(FindUser -userName '#{user_id}'#{domain.nil? ? "" : " -domain '#{domain}'"}) -ne $null"
  end
end

#check_user_profile(name) ⇒ Object



357
358
359
360
361
362
# File 'lib/specinfra/command/windows.rb', line 357

def (name)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindIISAppPool -name '#{name}').processModel.loadUserProfile -eq $true"
  end
end

#check_username(name, username) ⇒ Object



364
365
366
367
368
369
# File 'lib/specinfra/command/windows.rb', line 364

def check_username(name, username)
  Backend::PowerShell::Command.new do
    using 'find_iis_component.ps1'
    exec "(FindIISAppPool -name '#{name}').processModel.username -eq '#{username}'"
  end
end

#check_windows_feature_enabled(name, provider) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/specinfra/command/windows.rb', line 224

def check_windows_feature_enabled(name,provider)

  if provider.nil?
    cmd =  "@(ListWindowsFeatures -feature #{name}).count -gt 0"
  else
    cmd =  "@(ListWindowsFeatures -feature #{name} -provider #{provider}).count -gt 0"
  end

  Backend::PowerShell::Command.new do
    using 'list_windows_features.ps1'
    exec cmd
  end
end

#check_windows_hot_fix_installed(description, hot_fix_id = nil) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/specinfra/command/windows.rb', line 238

def check_windows_hot_fix_installed(description, hot_fix_id=nil)
  hot_fix_id_match = /(KB\d+)/i.match(description)
  hot_fix_id = hot_fix_id_match ? hot_fix_id_match[1] : description if hot_fix_id.nil?

  args = [ 
    '-description', "'#{description}'",
    '-hotFixId', "'#{hot_fix_id}'"
  ]
 
  cmd = "(FindInstalledHotFix #{args.join(' ')})" 
  Backend::PowerShell::Command.new do
    using 'find_installed_hot_fix.ps1'
    exec "#{cmd} -eq $true"
  end
end

#check_writable(file, by_whom) ⇒ Object



91
92
93
94
95
96
# File 'lib/specinfra/command/windows.rb', line 91

def check_writable(file, by_whom)
  Backend::PowerShell::Command.new do
    using 'check_file_access_rules.ps1'
    exec "CheckFileAccessRules -path '#{file}' -identity '#{get_identity by_whom}' -rules @('FullControl', 'Modify', 'Write')"
  end
end

#get_file_content(file) ⇒ Object



69
70
71
# File 'lib/specinfra/command/windows.rb', line 69

def get_file_content(file)
  %Q![Io.File]::ReadAllText("#{file}")!
end