winrm-elevated 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +5 -0
- data/Gemfile +9 -0
- data/LICENSE +202 -0
- data/README.md +54 -0
- data/Rakefile +28 -0
- data/VERSION +1 -0
- data/changelog.md +5 -0
- data/lib/winrm-elevated.rb +25 -0
- data/lib/winrm-elevated/runner.rb +76 -0
- data/lib/winrm-elevated/scripts/elevated_shell.ps1 +100 -0
- data/spec/config-example.yml +5 -0
- data/spec/matchers.rb +64 -0
- data/spec/powershell_elevated_spec.rb +86 -0
- data/spec/spec_helper.rb +43 -0
- data/winrm-elevated.gemspec +33 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2940150c1108b6135ff4712c64ad169d70fcffa2
|
4
|
+
data.tar.gz: 42efa9e3662deda528e95fd9136de461a5549d6f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1cbcd05d2a81bde6948710277eeb585a3056025be7937654e986e8e75bd778812c8e4ad293dd91cd0e344e70b4ae7874e409774f7326912248458c7a8966a364
|
7
|
+
data.tar.gz: 66cf31d2b965d1ff95390e1439008e120032712cc612196c48e4d34c84d12225f0e513d0cb4c4b7d55d490974c4c7706085c422793a9659eb738be9afd1b6b47
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Runs PowerShell commands as elevated over Windows Remote Management (WinRM) via a scheduled task
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/winrm-elevated.svg)](http://badge.fury.io/rb/winrm-elevated)
|
3
|
+
|
4
|
+
## Running commands elevated
|
5
|
+
```ruby
|
6
|
+
require 'winrm'
|
7
|
+
require 'winrm-elevated'
|
8
|
+
|
9
|
+
service = WinRM::WinRMWebService.new(...
|
10
|
+
elevated_runner = WinRM::Elevated::Runner.new(service)
|
11
|
+
result = elevated_runner.powershell_elevated('dir', 'Administrator', 'password')
|
12
|
+
puts "Std out: #{result.output}"
|
13
|
+
```
|
14
|
+
|
15
|
+
|
16
|
+
## Troubleshooting
|
17
|
+
|
18
|
+
If you're having trouble, first of all its most likely a network or WinRM configuration
|
19
|
+
issue. Take a look at the [WinRM gem troubleshooting](https://github.com/WinRb/WinRM#troubleshooting)
|
20
|
+
first.
|
21
|
+
|
22
|
+
## Contributing
|
23
|
+
|
24
|
+
1. Fork it.
|
25
|
+
2. Create a branch (git checkout -b my_feature_branch)
|
26
|
+
3. Run the unit and integration tests (bundle exec rake integration)
|
27
|
+
4. Commit your changes (git commit -am "Added a sweet feature")
|
28
|
+
5. Push to the branch (git push origin my_feature_branch)
|
29
|
+
6. Create a pull requst from your branch into master (Please be sure to provide enough detail for us to cipher what this change is doing)
|
30
|
+
|
31
|
+
### Running the tests
|
32
|
+
|
33
|
+
We use Bundler to manage dependencies during development.
|
34
|
+
|
35
|
+
```
|
36
|
+
$ bundle install
|
37
|
+
```
|
38
|
+
|
39
|
+
Once you have the dependencies, you can run the unit tests with `rake`:
|
40
|
+
|
41
|
+
```
|
42
|
+
$ bundle exec rake spec
|
43
|
+
```
|
44
|
+
|
45
|
+
To run the integration tests you will need a Windows box with the WinRM service properly configured. Its easiest to use the Vagrant Windows box in the Vagrantilfe of this repo.
|
46
|
+
|
47
|
+
1. Create a Windows VM with WinRM configured (see above).
|
48
|
+
2. Copy the config-example.yml to config.yml - edit this file with your WinRM connection details.
|
49
|
+
3. Run `bundle exec rake integration`
|
50
|
+
|
51
|
+
## WinRM-elevated Authors
|
52
|
+
* Shawn Neal (https://github.com/sneal)
|
53
|
+
|
54
|
+
[Contributors](https://github.com/WinRb/winrm-elevated/graphs/contributors)
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
require 'rubocop/rake_task'
|
6
|
+
require 'bundler/gem_tasks'
|
7
|
+
|
8
|
+
# Change to the directory of this file.
|
9
|
+
Dir.chdir(File.expand_path('../', __FILE__))
|
10
|
+
|
11
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
12
|
+
task.pattern = 'spec/*_spec.rb'
|
13
|
+
task.rspec_opts = ['--color', '-f documentation']
|
14
|
+
task.rspec_opts << '-tunit'
|
15
|
+
end
|
16
|
+
|
17
|
+
# Run the integration test suite
|
18
|
+
RSpec::Core::RakeTask.new(:integration) do |task|
|
19
|
+
task.pattern = 'spec/*_spec.rb'
|
20
|
+
task.rspec_opts = ['--color', '-f documentation']
|
21
|
+
task.rspec_opts << '-tintegration'
|
22
|
+
end
|
23
|
+
|
24
|
+
RuboCop::RakeTask.new
|
25
|
+
|
26
|
+
task default: [:spec, :rubocop]
|
27
|
+
|
28
|
+
task all: [:default, :integration]
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/changelog.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2015 Shawn Neal <sneal@sneal.net>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'winrm'
|
18
|
+
require_relative 'winrm-elevated/runner'
|
19
|
+
|
20
|
+
module WinRM
|
21
|
+
# WinRM File System
|
22
|
+
module Elevated
|
23
|
+
# Top level module code
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2015 Shawn Neal <sneal@sneal.net>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'winrm'
|
18
|
+
require 'winrm-fs'
|
19
|
+
|
20
|
+
module WinRM
|
21
|
+
module Elevated
|
22
|
+
# Runs PowerShell commands elevated via a scheduled task
|
23
|
+
class Runner
|
24
|
+
# Creates a new Elevated Runner instance
|
25
|
+
# @param [WinRMWebService] WinRM web service client
|
26
|
+
def initialize(winrm_service)
|
27
|
+
@winrm_service = winrm_service
|
28
|
+
@winrm_file_manager = WinRM::FS::FileManager.new(winrm_service)
|
29
|
+
@elevated_shell_path = 'c:/windows/temp/winrm-elevated-shell.ps1'
|
30
|
+
end
|
31
|
+
|
32
|
+
# Run a command or PowerShell script elevated without any of the
|
33
|
+
# restrictions that WinRM puts in place.
|
34
|
+
#
|
35
|
+
# @param [String] The command or PS script to wrap in a scheduled task
|
36
|
+
# @param [String] The admin user name to execute the scheduled task as
|
37
|
+
# @param [String] The admin user password
|
38
|
+
#
|
39
|
+
# @return [Hash] :stdout and :stderr
|
40
|
+
def powershell_elevated(script, username, password, &block)
|
41
|
+
# if an IO object is passed read it, otherwise assume the contents of the file were passed
|
42
|
+
script_text = script.respond_to?(:read) ? script.read : script
|
43
|
+
|
44
|
+
upload_elevated_shell_wrapper_script
|
45
|
+
wrapped_script = wrap_in_scheduled_task(script_text, username, password)
|
46
|
+
@winrm_service.run_cmd(wrapped_script, &block)
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def upload_elevated_shell_wrapper_script
|
52
|
+
file = Tempfile.new(['winrm-elevated-shell', 'ps1'])
|
53
|
+
begin
|
54
|
+
file.write(elevated_shell_script_content)
|
55
|
+
file.fsync
|
56
|
+
file.close
|
57
|
+
@winrm_file_manager.upload(file.path, @elevated_shell_path)
|
58
|
+
ensure
|
59
|
+
file.close
|
60
|
+
file.unlink
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def elevated_shell_script_content
|
65
|
+
IO.read(File.expand_path('../scripts/elevated_shell.ps1', __FILE__))
|
66
|
+
end
|
67
|
+
|
68
|
+
def wrap_in_scheduled_task(script_text, username, password)
|
69
|
+
ps_script = WinRM::PowershellScript.new(script_text)
|
70
|
+
"powershell -executionpolicy bypass -file \"#{@elevated_shell_path}\" " \
|
71
|
+
"-username \"#{username}\" -password \"#{password}\" -timeout \"#{@winrm_service.timeout}\" " \
|
72
|
+
"-encoded_command \"#{ps_script.encoded}\""
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
param([String]$username, [String]$password, [String]$encoded_command, [String]$timeout)
|
2
|
+
|
3
|
+
$task_name = "WinRM_Elevated_Shell"
|
4
|
+
$out_file = "$env:Temp\winrm_elevated_out.log"
|
5
|
+
$err_file = "$env:Temp\winrm_elevated_err.log"
|
6
|
+
|
7
|
+
if (Test-Path $out_file) {
|
8
|
+
del $out_file
|
9
|
+
}
|
10
|
+
if (Test-Path $err_file) {
|
11
|
+
del $err_file
|
12
|
+
}
|
13
|
+
|
14
|
+
$task_xml = @'
|
15
|
+
<?xml version="1.0" encoding="UTF-16"?>
|
16
|
+
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
|
17
|
+
<Principals>
|
18
|
+
<Principal id="Author">
|
19
|
+
<UserId>{username}</UserId>
|
20
|
+
<LogonType>Password</LogonType>
|
21
|
+
<RunLevel>HighestAvailable</RunLevel>
|
22
|
+
</Principal>
|
23
|
+
</Principals>
|
24
|
+
<Settings>
|
25
|
+
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
|
26
|
+
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
|
27
|
+
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
|
28
|
+
<AllowHardTerminate>true</AllowHardTerminate>
|
29
|
+
<StartWhenAvailable>false</StartWhenAvailable>
|
30
|
+
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
|
31
|
+
<IdleSettings>
|
32
|
+
<StopOnIdleEnd>false</StopOnIdleEnd>
|
33
|
+
<RestartOnIdle>false</RestartOnIdle>
|
34
|
+
</IdleSettings>
|
35
|
+
<AllowStartOnDemand>true</AllowStartOnDemand>
|
36
|
+
<Enabled>true</Enabled>
|
37
|
+
<Hidden>false</Hidden>
|
38
|
+
<RunOnlyIfIdle>false</RunOnlyIfIdle>
|
39
|
+
<WakeToRun>false</WakeToRun>
|
40
|
+
<ExecutionTimeLimit>{timeout}</ExecutionTimeLimit>
|
41
|
+
<Priority>4</Priority>
|
42
|
+
</Settings>
|
43
|
+
<Actions Context="Author">
|
44
|
+
<Exec>
|
45
|
+
<Command>cmd</Command>
|
46
|
+
<Arguments>{arguments}</Arguments>
|
47
|
+
</Exec>
|
48
|
+
</Actions>
|
49
|
+
</Task>
|
50
|
+
'@
|
51
|
+
|
52
|
+
$arguments = "/c powershell.exe -EncodedCommand $encoded_command > $out_file 2>$err_file"
|
53
|
+
|
54
|
+
$task_xml = $task_xml.Replace("{arguments}", $arguments)
|
55
|
+
$task_xml = $task_xml.Replace("{username}", $username)
|
56
|
+
$task_xml = $task_xml.Replace("{timeout}", $timeout)
|
57
|
+
|
58
|
+
$schedule = New-Object -ComObject "Schedule.Service"
|
59
|
+
$schedule.Connect()
|
60
|
+
$task = $schedule.NewTask($null)
|
61
|
+
$task.XmlText = $task_xml
|
62
|
+
$folder = $schedule.GetFolder("\")
|
63
|
+
$folder.RegisterTaskDefinition($task_name, $task, 6, $username, $password, 1, $null) | Out-Null
|
64
|
+
|
65
|
+
$registered_task = $folder.GetTask("\$task_name")
|
66
|
+
$registered_task.Run($null) | Out-Null
|
67
|
+
|
68
|
+
$timeout = 10
|
69
|
+
$sec = 0
|
70
|
+
while ( (!($registered_task.state -eq 4)) -and ($sec -lt $timeout) ) {
|
71
|
+
Start-Sleep -s 1
|
72
|
+
$sec++
|
73
|
+
}
|
74
|
+
|
75
|
+
function SlurpOutput($file, $cur_line, $out_type) {
|
76
|
+
if (Test-Path $file) {
|
77
|
+
get-content $file | select -skip $cur_line | ForEach {
|
78
|
+
$cur_line += 1
|
79
|
+
if ($out_type -eq 'err') {
|
80
|
+
$host.ui.WriteErrorLine("$_")
|
81
|
+
} else {
|
82
|
+
$host.ui.WriteLine("$_")
|
83
|
+
}
|
84
|
+
}
|
85
|
+
}
|
86
|
+
return $cur_line
|
87
|
+
}
|
88
|
+
|
89
|
+
$err_cur_line = 0
|
90
|
+
$out_cur_line = 0
|
91
|
+
do {
|
92
|
+
Start-Sleep -m 100
|
93
|
+
$out_cur_line = SlurpOutput $out_file $out_cur_line 'out'
|
94
|
+
$err_cur_line = SlurpOutput $err_file $err_cur_line 'err'
|
95
|
+
} while (!($registered_task.state -eq 3))
|
96
|
+
|
97
|
+
$exit_code = $registered_task.LastTaskResult
|
98
|
+
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($schedule) | Out-Null
|
99
|
+
|
100
|
+
exit $exit_code
|
data/spec/matchers.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rspec/expectations'
|
3
|
+
|
4
|
+
# rspec matchers
|
5
|
+
module WinRMSpecs
|
6
|
+
def self.stdout(output)
|
7
|
+
output[:data].collect do |i|
|
8
|
+
i[:stdout]
|
9
|
+
end.join('\r\n').gsub(/(\\r\\n)+$/, '')
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.stderr(output)
|
13
|
+
output[:data].collect do |i|
|
14
|
+
i[:stderr]
|
15
|
+
end.join('\r\n').gsub(/(\\r\\n)+$/, '')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
RSpec::Matchers.define :have_stdout_match do |expected_stdout|
|
20
|
+
match do |actual_output|
|
21
|
+
!expected_stdout.match(WinRMSpecs.stdout(actual_output)).nil?
|
22
|
+
end
|
23
|
+
failure_message do |actual_output|
|
24
|
+
"expected that '#{WinRMSpecs.stdout(actual_output)}' would match #{expected_stdout}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
RSpec::Matchers.define :have_stderr_match do |expected_stderr|
|
29
|
+
match do |actual_output|
|
30
|
+
!expected_stderr.match(WinRMSpecs.stderr(actual_output)).nil?
|
31
|
+
end
|
32
|
+
failure_message do |actual_output|
|
33
|
+
"expected that '#{WinRMSpecs.stderr(actual_output)}' would match #{expected_stderr}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
RSpec::Matchers.define :have_no_stdout do
|
38
|
+
match do |actual_output|
|
39
|
+
stdout = WinRMSpecs.stdout(actual_output)
|
40
|
+
stdout == '\r\n' || stdout == ''
|
41
|
+
end
|
42
|
+
failure_message do |actual_output|
|
43
|
+
"expected that '#{WinRMSpecs.stdout(actual_output)}' would have no stdout"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
RSpec::Matchers.define :have_no_stderr do
|
48
|
+
match do |actual_output|
|
49
|
+
stderr = WinRMSpecs.stderr(actual_output)
|
50
|
+
stderr == '\r\n' || stderr == ''
|
51
|
+
end
|
52
|
+
failure_message do |actual_output|
|
53
|
+
"expected that '#{WinRMSpecs.stderr(actual_output)}' would have no stderr"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
RSpec::Matchers.define :have_exit_code do |expected_exit_code|
|
58
|
+
match do |actual_output|
|
59
|
+
expected_exit_code == actual_output[:exitcode]
|
60
|
+
end
|
61
|
+
failure_message do |actual_output|
|
62
|
+
"expected exit code #{expected_exit_code}, but got #{actual_output[:exitcode]}"
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
describe 'powershell elevated runner', integration: true do
|
3
|
+
describe 'ipconfig' do
|
4
|
+
subject(:output) { elevated_runner.powershell_elevated('ipconfig', username, password) }
|
5
|
+
it { should have_exit_code 0 }
|
6
|
+
it { should have_stdout_match(/Windows IP Configuration/) }
|
7
|
+
it { should have_no_stderr }
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'echo \'hello world\' using apostrophes' do
|
11
|
+
subject(:output) { elevated_runner.powershell_elevated("echo 'hello world'", username, password) }
|
12
|
+
it { should have_exit_code 0 }
|
13
|
+
it { should have_stdout_match(/hello world/) }
|
14
|
+
it { should have_no_stderr }
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'ipconfig with incorrect argument -z' do
|
18
|
+
subject(:output) { elevated_runner.powershell_elevated('ipconfig 127.0.0.1 -z', username, password) }
|
19
|
+
it { should have_exit_code 1 }
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'Math area calculation' do
|
23
|
+
subject(:output) do
|
24
|
+
cmd = <<-EOH
|
25
|
+
$diameter = 4.5
|
26
|
+
$area = [Math]::pow([Math]::PI * ($diameter/2), 2)
|
27
|
+
Write-Host $area
|
28
|
+
EOH
|
29
|
+
elevated_runner.powershell_elevated(cmd, username, password)
|
30
|
+
end
|
31
|
+
it { should have_exit_code 0 }
|
32
|
+
it { should have_stdout_match(/49.9648722805149/) }
|
33
|
+
it { should have_no_stderr }
|
34
|
+
end
|
35
|
+
|
36
|
+
describe 'ipconfig with a block' do
|
37
|
+
subject(:stdout) do
|
38
|
+
outvar = ''
|
39
|
+
elevated_runner.powershell_elevated('ipconfig', username, password) do |stdout, _stderr|
|
40
|
+
outvar << stdout
|
41
|
+
end
|
42
|
+
outvar
|
43
|
+
end
|
44
|
+
it { should match(/Windows IP Configuration/) }
|
45
|
+
end
|
46
|
+
|
47
|
+
describe 'capturing output from Write-Host and Write-Error' do
|
48
|
+
subject(:output) do
|
49
|
+
script = <<-eos
|
50
|
+
Write-Host 'Hello'
|
51
|
+
$host.ui.WriteErrorLine(', world!')
|
52
|
+
eos
|
53
|
+
|
54
|
+
@captured_stdout = ''
|
55
|
+
@captured_stderr = ''
|
56
|
+
elevated_runner.powershell_elevated(script, username, password) do |stdout, stderr|
|
57
|
+
@captured_stdout << stdout if stdout
|
58
|
+
@captured_stderr << stderr if stderr
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should have stdout' do
|
63
|
+
expect(output.stdout).to eq("Hello\r\n")
|
64
|
+
expect(output.stdout).to eq(@captured_stdout)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should have stderr' do
|
68
|
+
# TODO: Option to parse CLIXML
|
69
|
+
# expect(output.output).to eq("Hello\n, world!")
|
70
|
+
# expect(output.stderr).to eq(", world!")
|
71
|
+
expect(output.stderr).to eq(
|
72
|
+
"#< CLIXML\r\n<Objs Version=\"1.1.0.1\" " \
|
73
|
+
"xmlns=\"http://schemas.microsoft.com/powershell/2004/04\">" \
|
74
|
+
"<S S=\"Error\">, world!_x000D__x000A_</S></Objs>\r\n")
|
75
|
+
expect(output.stderr).to eq(@captured_stderr)
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should have output' do
|
79
|
+
# TODO: Option to parse CLIXML
|
80
|
+
# expect(output.output).to eq("Hello\n, world!")
|
81
|
+
expect(output.output).to eq("Hello\r\n#< CLIXML\r\n<Objs Version=\"1.1.0.1\" " \
|
82
|
+
"xmlns=\"http://schemas.microsoft.com/powershell/2004/04\">" \
|
83
|
+
"<S S=\"Error\">, world!_x000D__x000A_</S></Objs>\r\n")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'winrm'
|
3
|
+
require 'winrm-elevated'
|
4
|
+
require_relative 'matchers'
|
5
|
+
|
6
|
+
# Creates a WinRM connection for integration tests
|
7
|
+
module ConnectionHelper
|
8
|
+
def winrm_connection
|
9
|
+
WinRM::WinRMWebService.new(
|
10
|
+
winrm_config['endpoint'],
|
11
|
+
winrm_config['auth_type'].to_sym,
|
12
|
+
user: username,
|
13
|
+
pass: password,
|
14
|
+
basic_auth_only: true)
|
15
|
+
end
|
16
|
+
|
17
|
+
def elevated_runner
|
18
|
+
@elevated_runner ||= WinRM::Elevated::Runner.new(winrm_connection)
|
19
|
+
end
|
20
|
+
|
21
|
+
def winrm_config
|
22
|
+
unless @winrm_config
|
23
|
+
path = File.expand_path("#{File.dirname(__FILE__)}/config.yml")
|
24
|
+
unless File.exist?(path)
|
25
|
+
path = File.expand_path("#{File.dirname(__FILE__)}/config-example.yml")
|
26
|
+
end
|
27
|
+
@winrm_config = YAML.load(File.read(path))
|
28
|
+
end
|
29
|
+
@winrm_config
|
30
|
+
end
|
31
|
+
|
32
|
+
def username
|
33
|
+
winrm_config['options']['user']
|
34
|
+
end
|
35
|
+
|
36
|
+
def password
|
37
|
+
winrm_config['options']['pass']
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
RSpec.configure do |config|
|
42
|
+
config.include(ConnectionHelper)
|
43
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
version = File.read(File.expand_path('../VERSION', __FILE__)).strip
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.name = 'winrm-elevated'
|
9
|
+
s.version = version
|
10
|
+
s.date = Date.today.to_s
|
11
|
+
|
12
|
+
s.author = ['Shawn Neal']
|
13
|
+
s.email = ['sneal@sneal.net']
|
14
|
+
s.homepage = 'https://github.com/WinRb/winrm-elevated'
|
15
|
+
|
16
|
+
s.summary = 'Ruby library for running commands as elevated'
|
17
|
+
s.description = <<-EOF
|
18
|
+
Ruby library for running commands via WinRM as elevated through a scheduled task
|
19
|
+
EOF
|
20
|
+
s.license = 'Apache-2.0'
|
21
|
+
|
22
|
+
s.files = `git ls-files`.split(/\n/)
|
23
|
+
s.require_path = 'lib'
|
24
|
+
s.rdoc_options = %w(-x test/ -x examples/)
|
25
|
+
s.extra_rdoc_files = %w(README.md LICENSE)
|
26
|
+
|
27
|
+
s.required_ruby_version = '>= 1.9.0'
|
28
|
+
s.add_runtime_dependency 'winrm', '~> 1.3'
|
29
|
+
s.add_runtime_dependency 'winrm-fs', '~> 0.2.2'
|
30
|
+
s.add_development_dependency 'rspec', '~> 3.2'
|
31
|
+
s.add_development_dependency 'rake', '~> 10.3'
|
32
|
+
s.add_development_dependency 'rubocop', '~> 0.28'
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: winrm-elevated
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shawn Neal
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: winrm
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: winrm-fs
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.2.2
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.2.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.28'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.28'
|
83
|
+
description: |2
|
84
|
+
Ruby library for running commands via WinRM as elevated through a scheduled task
|
85
|
+
email:
|
86
|
+
- sneal@sneal.net
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files:
|
90
|
+
- README.md
|
91
|
+
- LICENSE
|
92
|
+
files:
|
93
|
+
- ".gitignore"
|
94
|
+
- ".rspec"
|
95
|
+
- ".rubocop.yml"
|
96
|
+
- ".travis.yml"
|
97
|
+
- Gemfile
|
98
|
+
- LICENSE
|
99
|
+
- README.md
|
100
|
+
- Rakefile
|
101
|
+
- VERSION
|
102
|
+
- changelog.md
|
103
|
+
- lib/winrm-elevated.rb
|
104
|
+
- lib/winrm-elevated/runner.rb
|
105
|
+
- lib/winrm-elevated/scripts/elevated_shell.ps1
|
106
|
+
- spec/config-example.yml
|
107
|
+
- spec/matchers.rb
|
108
|
+
- spec/powershell_elevated_spec.rb
|
109
|
+
- spec/spec_helper.rb
|
110
|
+
- winrm-elevated.gemspec
|
111
|
+
homepage: https://github.com/WinRb/winrm-elevated
|
112
|
+
licenses:
|
113
|
+
- Apache-2.0
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options:
|
117
|
+
- "-x"
|
118
|
+
- test/
|
119
|
+
- "-x"
|
120
|
+
- examples/
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: 1.9.0
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 2.4.5
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: Ruby library for running commands as elevated
|
139
|
+
test_files: []
|