Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powershell args #5

Merged
merged 4 commits into from
Jul 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/puppet/provider/exec/powershell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
elsif File.exists?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
else
"powershell.exe"
'powershell.exe'
end

PS_ARGS = '-NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass'

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like when i looked at this a number of months ago you should add -inputformat none here. I can't remember the exact reasoning but I felt like stdin redirection caused hangs or some other bizarre scenario. i worked around it, but figured i'd mention it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like:
http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected
http://stackoverflow.com/questions/4238192/running-powershell-from-msdeploy-runcommand-does-not-exit/4239192#4239192

Another option would be to write the powershell script to a tempfile (securely) and then use -file <path>. That might be a good idea anyways, in case the command is long, e.g. from a template, and exceeds the maximum CreateProcess length. Thoughts?

commands :powershell => POWERSHELL

desc <<-EOT
Expand All @@ -28,7 +30,7 @@
EOT

def run(command, check = false)
super("\"#{POWERSHELL}\" #{command}", check)
super("\"#{POWERSHELL}\" #{PS_ARGS} -Command \"#{command}\"", check)
end

def checkexe(command)
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
dir = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift File.join(dir, 'lib')

require 'mocha'
require 'puppet'
require 'rspec'
#require 'spec/autorun'
require 'mocha/api'

RSpec.configure do |config|
config.mock_with :mocha
Expand Down
13 changes: 12 additions & 1 deletion spec/unit/provider/exec/powershell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@

describe Puppet::Type.type(:exec).provider(:powershell), :if => Puppet.features.microsoft_windows? do
let(:command) { '$(Get-WMIObject Win32_UserAccount -Filter "Name=\'Administrator\'")' }
let(:args) { '-NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass' }
let(:resource) { Puppet::Type.type(:exec).new(:command => command, :provider => :powershell) }
let(:provider) { described_class.new(resource) }

let(:powershell) {
if File.exists?("#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe"
elsif File.exists?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
else
'powershell.exe'
end
}

describe "#run" do
it "should be able to run powershell commands" do
Puppet::Util::Execution.expects(:execute).with(['powershell.exe', command], anything)
Puppet::Util::Execution.expects(:execute).with("\"#{powershell}\" #{args} -Command \"#{command}\"", anything)
provider.run(command)
end
end
Expand Down