summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStuart Preston <stuart@chef.io>2018-02-19 17:41:52 +0000
committerStuart Preston <stuart@chef.io>2018-02-21 17:14:59 +0000
commitf3561ea9536b78a77082ad605ecc888f678fc143 (patch)
treeee831aa711add948f68c3c9cbd8b9bdba82c4932
parent8d081dc08f86b384f43e4f7dd72646b4c0c7a743 (diff)
downloadchef-f3561ea9536b78a77082ad605ecc888f678fc143.tar.gz
Adding new powershell_execute mixin
Signed-off-by: Stuart Preston <stuart@chef.io>
-rw-r--r--lib/chef/dsl/universal.rb2
-rw-r--r--lib/chef/mixin/powershell_execute.rb44
2 files changed, 46 insertions, 0 deletions
diff --git a/lib/chef/dsl/universal.rb b/lib/chef/dsl/universal.rb
index 6e3d162b6f..bdf48e3371 100644
--- a/lib/chef/dsl/universal.rb
+++ b/lib/chef/dsl/universal.rb
@@ -18,6 +18,7 @@
#
require "chef/dsl/platform_introspection"
+require "chef/mixin/powershell_execute"
require "chef/mixin/powershell_out"
require "chef/mixin/shell_out"
@@ -43,6 +44,7 @@ class Chef
#
module Universal
include Chef::DSL::PlatformIntrospection
+ include Chef::Mixin::PowershellExecute
include Chef::Mixin::PowershellOut
include Chef::Mixin::ShellOut
end
diff --git a/lib/chef/mixin/powershell_execute.rb b/lib/chef/mixin/powershell_execute.rb
new file mode 100644
index 0000000000..ffa925c056
--- /dev/null
+++ b/lib/chef/mixin/powershell_execute.rb
@@ -0,0 +1,44 @@
+# Author:: Stuart Preston (<stuart@chef.io>)
+# Copyright:: Copyright 2018, Chef Software, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+require "win32ole"
+require "json"
+
+class Chef
+ module Mixin
+ module PowershellExecute
+
+ # Run a command under PowerShell via a managed (.NET) COM interop API.
+ # This implementation requires the managed dll to be registered on the target machine.
+ # Required: .NET Framework 4.0 or compatible, 64 bit platform.
+ #
+ # Typical command used to install the interop assembly into the registry:
+ # C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm.exe Chef.PowerShell.dll /codebase
+ #
+ # @param script [String] script to run
+ # @return [String] JSON containing output
+ def powershell_execute(*command_args)
+ script = command_args.first
+ options = command_args.last.is_a?(Hash) ? command_args.last : nil
+
+ ps = WIN32OLE.new('Chef.PowerShell')
+ outcome = ps.ExecuteScript(script)
+
+ JSON.parse(outcome)
+ end
+ end
+ end
+end