Class: Roebe::Windows::Powershell

Inherits:
Base show all
Defined in:
lib/roebe/windows/powershell.rb

Overview

Roebe::Powershell

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect

Constants inherited from Base

Base::COLOURS, Base::HOME_DIRECTORY_OF_USER_X, Base::N

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#actions, #append_this_onto_that_file, #append_what_into, #be_silent, #be_verbose?, #beautiful_url, #burlywood, #call_next, #chdir, #cheerful_guy, #cliner, #complex_esystem, #convert_global_env, #copy_directory, #copy_file, #cornflowerblue, #create_directory, #crimson, #current_month?, #current_time?, #current_year?, #cyan, #darkcyan, #darkgreen, #darkkhaki, #darkslateblue, #deeppink, #delete_symlink, #do_not_use_the_base_colours, #do_use_the_base_colours, #e, #ecomment, #editor_to_use?, #efancy, #eimp, #emphasis, #ensure_main_encoding, #ensure_utf_encoding, #eparse, #erev, #esteelblue, #etomato, #ewarn, #exit_program, #firebrick, #get_current_day, #get_current_month, #get_files_and_directories, #get_files_from, #get_german_name_for_this_weekday, #gold, #green, #grey, #hh_mm_ss, #hh_mm_ss_day_month_year, #home_dir?, #home_directory_of_user_x?, #infer_the_namespace, #internal_hash?, #is_a_directory?, #is_a_file?, #is_a_jpg_file?, #is_an_image_file?, #is_archive?, #is_audio_file?, #is_in_studium_dir?, #is_multimedia_file?, #is_on_roebe?, #is_on_windows?, #is_studium_available?, #is_symlink?, #is_this_a_ruby_file?, #is_video_file?, #iso_encoding?, #le, #left_colour, #lightblue, #lightgreen, #lightseagreen, #lightsteelblue, #lime, #limegreen, #localhost_to_data, #log_directory?, #main_encoding?, #mediumorchid, #mediumpurple, #mediumseagreen, #mediumslateblue, #mediumspringgreen, #mediumturquoise, #mkdir_p, #mv, #n_days_in_this_month, #n_pages_in_this_pdf_file?, #namespace?, #no_file_exists_at, #ogrey, #olive, #olivedrab, #open_in_browser, #opne, #opnesystem, #opnn, #orange, #orchid, #orev, #palegoldenrod, #palevioletred, #pink, #powderblue, #programs_dir?, #project_base_dir?, #random_html_colour, #rds, #read_file_in_iso_encoding, #read_file_via_the_default_encoding, #read_lines_via_iso_encoding, #readlines_with_main_encoding, #red, #register_sigint, #remove, #remove_directory, #remove_file, #rename_kde_konsole_tab, #replace_localhost_with_data, #report_pwd, #require_rescue, #reset_the_internal_hash, #return_all_directories_from_this_directory, #return_all_files_from_this_directory, #return_current_directory, #return_dd_mm_yyyy, #return_file_or_directory_of, #return_files_from_pwd, #return_last_part_of_the_current_directory, #return_utc, #rev, #right_arrow?, #right_colour, #roebe_log_directory?, #rosybrown, #royalblue, #ruby_base_directory?, #run_in_background, #run_rcfiles_then_run_ata_via_qdbus, #sandybrown, #sdir, #seagreen, #set_be_verbose, #set_xorg_buffer, #sfancy, #sfile, #silent_redirection?, #simp, #simple_esystem, #skyblue, #slateblue, #slategray, #springgreen, #steelblue, #string_italic, #swarn, #symlink, #teal, #temp_dir?, #to_camelcase, #to_counted_hash, #tomato, #touch, #try_to_require_the_beautiful_url_gem, #try_to_require_the_html_template, #try_to_require_the_open_gem, #try_to_require_the_program_information_gem, #try_to_require_the_xorg_buffer, #use_colours?, #verbose_truth, #weekday?, #word_wrap, #write_what_into, #yellow

Methods included from Base::CommandlineArguments

#append_onto_the_commandline_arguments, #clear_commandline_arguments, #commandline_arguments?, #commandline_arguments_as_string?, #commandline_arguments_without_leading_hyphens?, #first_argument?, #first_argument_without_leading_hyphens?, #has_an_argument_been_passed?, #remove_hyphened_arguments_from_the_commandline_arguments, #return_commandline_arguments_with_leading_hyphens, #second_argument?, #set_commandline_arguments

Constructor Details

#initialize(i = nil, run_already = true) ⇒ Powershell

#

initialize

#


36
37
38
39
40
41
42
43
# File 'lib/roebe/windows/powershell.rb', line 36

def initialize(
    i           = nil,
    run_already = true
  )
  reset
  set_input(i)
  run if run_already
end

Class Method Details

.[](i = '') ⇒ Object

#

Roebe::Windows::Powershell[]

#


333
334
335
# File 'lib/roebe/windows/powershell.rb', line 333

def self.[](i = '')
  new(i)
end

.output_clipboard(i) ⇒ Object

#

Roebe::Powershell.output_clipboard

#


326
327
328
# File 'lib/roebe/windows/powershell.rb', line 326

def self.output_clipboard(i)
  e paste(i)
end

.paste(i) ⇒ Object

#

Roebe::Powershell.paste

#


318
319
320
321
# File 'lib/roebe/windows/powershell.rb', line 318

def self.paste(i)
  result = `powershell.exe -Command Get-Clipboard`
  return result
end

Instance Method Details

#enable_nix_subsystem_for_windowsObject

#

enable_windows_subsystem_for_linux

This method will attempt to enable the Linux-subsystem on windows, via PowerShell. Note that this requires Administrator access.

#

enable_nix_subsystem_for_windows



167
168
169
170
# File 'lib/roebe/windows/powershell.rb', line 167

def enable_windows_subsystem_for_linux
  _ = 'Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux'
  erun_via_powershell _
end

#enable_windows_subsystem_for_linuxObject

#

enable_windows_subsystem_for_linux

This method will enable the Windows Subsystem for Linux.

#


164
165
166
167
# File 'lib/roebe/windows/powershell.rb', line 164

def enable_windows_subsystem_for_linux
  _ = 'Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux'
  erun_via_powershell _
end

#erun_via_powershell(i) ⇒ Object

#

erun_via_powershell

This method combines the powershell-instruction with an “echo” on the commandline, e. g. to show which command will be run exactly.

#


175
176
177
178
# File 'lib/roebe/windows/powershell.rb', line 175

def erun_via_powershell(i)
  e i
  run_via_powershell i
end

#expand_archive(i = 'Ubuntu') ⇒ Object

#

expand_archive

#


254
255
256
257
# File 'lib/roebe/windows/powershell.rb', line 254

def expand_archive(i = 'Ubuntu')
  _ =  'Expand-Archive ~/'+i+'.zip ~/'+i
  erun_via_powershell _
end

#input?Boolean

#

input?

#

Returns:

  • (Boolean)


64
65
66
# File 'lib/roebe/windows/powershell.rb', line 64

def input?
  @input
end

#install_opensuseObject

#

install_opensuse

Use this to install OpenSUSE from the commandline.

#


264
265
266
267
268
269
# File 'lib/roebe/windows/powershell.rb', line 264

def install_opensuse
  target_url = useful_wsl_links[:OpenSUSE]
  _ = 'Invoke-WebRequest -Uri '+target_url+' -OutFile ~/OpenSUSE.zip -UseBasicParsing'
  erun_via_powershell _
  expand_archive 'OpenSUSE'
end

#install_slesObject

#

install_sles

Use this to install SLES from the commandline.

#


276
277
278
279
280
281
# File 'lib/roebe/windows/powershell.rb', line 276

def install_sles
  target_url = useful_wsl_links[:SLES]
  _ = 'Invoke-WebRequest -Uri '+target_url+' -OutFile ~/SLES.zip -UseBasicParsing'
  erun_via_powershell _
  expand_archive 'SLES'
end

#install_ubuntuObject

#

install_ubuntu

Use this to install Ubuntu from the commandline.

#


244
245
246
247
248
249
# File 'lib/roebe/windows/powershell.rb', line 244

def install_ubuntu
  target_url = useful_wsl_links[:Ubuntu]
  _ = "Invoke-WebRequest -Uri #{target_url} -OutFile ~/Ubuntu.zip -UseBasicParsing"
  erun_via_powershell _
  expand_archive 'Ubuntu'
end
#

menu (menu tag)

#


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/roebe/windows/powershell.rb', line 71

def menu(i)
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i # case tag
    # ===================================================================== #
    # === powershell install_ubuntu
    # ===================================================================== #
    when /install(_|-)?ubuntu/
      install_ubuntu
    # ===================================================================== #
    # === powershell install_opensuse
    # ===================================================================== #
    when /install(_|-)?opensuse/
      install_opensuse
    # ===================================================================== #
    # === powershell install_sles
    # ===================================================================== #
    when /install(_|-)?sles/
      install_sles
    # ===================================================================== #
    # === powershell enable_windows_subsystem_for_linux
    # ===================================================================== #
    when /enable(_|-)?windows(_|-)?subsystem(_|-)?for(_|-)?linux/
      enable_windows_subsystem_for_linux
    # ===================================================================== #
    # === powershell windows_subsystem_for_linux_is_enabled?
    # ===================================================================== #
    when /windows(_|-)?subsystem(_|-)?for(_|-)?linux(_|-)?is(_|-)?enabled\??/
      windows_subsystem_for_linux_is_enabled?
    # ===================================================================== #
    # === powershell --windows_server_build_number?
    # ===================================================================== #
    when /-?-?windows(_|-)?server(_|-)?build(_|-)?number\??/
      windows_server_build_number?
    # ===================================================================== #
    # === powershell --view-events
    # ===================================================================== #
    when /-?-?view(-|_)?events$/i,
         /show_?process_?creation_?events/i,
         /creation_?events/i
      show_process_creation_events
    # ===================================================================== #
    # === powershell --help
    # ===================================================================== #
    when /help/
      show_help_then_exit
    # ===================================================================== #
    # === powershell --enable-linux
    # ===================================================================== #
    when /enable(-|_)?linux$/i,
         /enable(-|_)?nix$/i,
         /linux$/i
      enable_nix_subsystem_for_windows
    # ===================================================================== #
    # === powershell --remove-groove
    # ===================================================================== #
    when /remove(-|_)?groove$/i,
         /remove(-|_)?groove(-|_)?player$/i,
         /remove(-|_)?groove(-|_)?music$/i
      remove_groove_player
    end
  end
end

#remove_groove_playerObject

#

remove_groove_player

This will remove the Microsoft groove player.

Invoke like this:

powershell --remove-groove
#


188
189
190
191
# File 'lib/roebe/windows/powershell.rb', line 188

def remove_groove_player
  _ = '"remove-AppxPackage (Get-AppxPackage -AllUsers|Where{$_.PackageFullName -match "ZuneMusic"}).PackageFullName"'
  erun_via_powershell _
end

#resetObject

#

reset (reset tag)

#


48
49
50
# File 'lib/roebe/windows/powershell.rb', line 48

def reset
  super()
end

#runObject

#

run (run tag)

#


311
312
313
# File 'lib/roebe/windows/powershell.rb', line 311

def run
  menu(input?)
end

#run_via_powershell(i = '') ⇒ Object

#

run_via_powershell

This method will invoke powershell.exe.

See also the discussion here:

https://stackoverflow.com/questions/26134810/get-powershell-output-in-ruby
#


201
202
203
204
205
# File 'lib/roebe/windows/powershell.rb', line 201

def run_via_powershell(i = '')
  # i = '"'+i+'"' if i.include? ' '
  _ = "powershell.exe #{i}"
  esystem _
end

#set_input(i = '') ⇒ Object

#

set_input

#


55
56
57
58
59
# File 'lib/roebe/windows/powershell.rb', line 55

def set_input(i = '')
  i = i.first if i.is_a? Array
  i = i.to_s.dup
  @input = i
end

#show_help(optional_also_exit_afterwards = nil) ⇒ Object

#

show_help (help tag)

#


286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/roebe/windows/powershell.rb', line 286

def show_help(optional_also_exit_afterwards = nil)
  e 'These options are available right now:'
  e
  e '  --enable-linux                       # Enable the linux subsystem on windows.'
  e '  --enable-windows-subsystem-for-linux # Enable the windows subsystem for linux'
  e '  --install_ubuntu                     # Install Ubuntu'
  e '  --install_opensuse                   # Install OpenSUSE'
  e '  --install_sles                       # Install SLES'
  e '  --remove-groove                      # Remove the groove music player.'
  e '  --view-events                        # View process creation events.'
  e '  --windows-server-build-number?       # Determine the windows server build number.'
  e
  exit if optional_also_exit_afterwards
end

#show_help_then_exitObject

#

show_help_then_exit

#


304
305
306
# File 'lib/roebe/windows/powershell.rb', line 304

def show_help_then_exit
  show_help(:do_exit)
end

#show_process_creation_eventsObject

#

show_process_creation_events

You can view “process creation events” on windows via the PowerShell. This method is doing precisely that.

#


153
154
155
156
# File 'lib/roebe/windows/powershell.rb', line 153

def show_process_creation_events
  _ = "Get-WinEvent Security | ? id -eq 4688 | ? { $_.Properties[5].Value -match 'conhost' } | Select TimeCreated"
  erun_via_powershell _
end
#

This will return a Hash showing useful WSL links, pointing towards different Linux distributions.

#


231
232
233
234
235
236
237
# File 'lib/roebe/windows/powershell.rb', line 231

def useful_wsl_links
  {
    Ubuntu:   'https://aka.ms/wsl-ubuntu-1604',
    OpenSUSE: 'https://aka.ms/wsl-opensuse-42',
    SLES:     'https://aka.ms/wsl-sles-12'
  }
end

#windows_server_build_number?Boolean

#

windows_server_build_number?

Use this method to find out the Windows Server build number, via the PowerShell.

#

Returns:

  • (Boolean)


142
143
144
145
# File 'lib/roebe/windows/powershell.rb', line 142

def windows_server_build_number?
  _ = 'systeminfo | Select-String "^OS Name","^OS Version"'
  erun_via_powershell _
end

#windows_subsystem_for_linux_is_enabled?Boolean

#

windows_subsystem_for_linux_is_enabled?

#

Returns:

  • (Boolean)


210
211
212
213
# File 'lib/roebe/windows/powershell.rb', line 210

def windows_subsystem_for_linux_is_enabled?
  _ = 'Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux'
  erun_via_powershell _
end