2017-03-01 5 views
0

Powershellスクリプトへのパスを指定する代わりに、Puppet execリソースを使用して直接Powershellコマンドを実行しようとしています。Puppet execリソースを使用してPowerShellコマンドを直接実行する

exec { "Change status and start-up of Win service": 
    command => 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NoLogo -NonInteractive -Command "& {set-service Spooler -Status Running -StartupType Automatic; exit (1 - [int]$?)}"', 
    onlyif => "if(Get-Service Spooler) { exit 0 } else { exit 1 }", 
    logoutput => 'on_failure', 
} 

私は上記のコマンドを実行しようとすると、私は次のエラーを取得する:

Error: Failed to apply catalog: Parameter onlyif failed on Exec[Change status and start-up of Win service]: 'if(Get-Service Spooler 
) { exit 0 } else { exit 1 }' is not qualified and no path was specified. Please qualify the command or specify a path. at /etc/puppetlabs/code/environments/production/modules/common/manifests/svc_auto_running.pp:17 

は、いくつかのリンクを見て、すべてのバイナリへのフルパスを指定して述べたように、この場合に私はがあると思いいますPS実行ファイルへのパスで、すでに指定されています。

ご協力いただければ幸いです。

UPDATE:powershellコマンドでonlyif文をラップしようとしたところ、execを使ってpowershellコマンドを直接実行することができました。しかし、今問題は、存在しない Winサービスがonlyifステートメントにあるかどうかを調べると、コマンドはまだ正常にを渡すことになります。

$powershell = 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoProfile -NoLogo -NonInteractive' 

exec { "Change status and start-up of Win service": 
    command => "$powershell -Command '& {set-service iDoNotExist -Status Running -StartupType Automatic; exit (1 - [int]$?)}'", 
    #onlyif => "$powershell -Command '& {if(Get-Service iDoNotExist); exit (1 - [int]$?)}'", 
    onlyif => "$powershell -Command '& {if(Get-Service iDoNotExist) { exit 0 } else { exit 1 }}'", 
    logoutput => 'on_failure', 
} 
+4

をあなたはおそらくもPowerShellコマンドで 'onlyif'文をラップする必要があり、私は実行可能ファイルを実行しようとしている現時点では推測します。 – arco444

+0

ありがとう@ arco444!それはうまくいった。 :)あなたは答えとしてそれを掲示するかもしれません。私はこれを閉じます。ところで、これには私が雇うことのできるよりよい代替手段がありますか? – Technext

+0

別の問題がありましたが、やはり立ち往生しました。私のポストを更新しました。 – Technext

答えて

1

@ arco444で提案されているように、私はonlyif文をpowershellコマンドでラップしようとしましたが、それは私のために働いていましたが、後で私の投稿のUPDATEセクションで述べたように、最後に、次のように働いていた:

$powershell = 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoProfile -NoLogo -NonInteractive' 

exec { "Change status and start-up of Win service": 
    command => "$powershell -Command \"& {set-service $service_name -Status Running -StartupType Automatic; exit (1 - [int]$?)}\"", 
    onlyif => "$powershell -Command '& {\"if(Get-Service ${service_name})\"; exit (1 - [int]$?)}'", 
    logoutput => 'on_failure', 
} 
1

おそらく、これらのコマンドを実行するにはpowershell prividerを使用する方がよいでしょう。このモジュールはpuppetlabs/powershellをインストールできます。

Powershellコマンドを実行するために使用できるpowershellプロバイダを提供します。それはおそらくあなたが人形の実行中にコマンドを実行するときに多くの厄介なエッジケースを避けるのに役立ちます。

+0

提案していただきありがとうございます。私は実際には、私は[これに直面するまで、すべてのエグゼクティブの前にpowershellプロバイダを使用していた(http://stackoverflow.com/questions/42484043/capturing-output-error-when-invoking-powershell-script/42485152?noredirect=1# comment72112698_42485152)問題です。だから私はそれを避けています。 – Technext

関連する問題