尝试自学Powershell和厨师,所以我确信我错过了一些简单的东西
所以我实际上使用的是名为Chef的服务器管理器程序。但脚本使用PS解释器运行。我确保导入了ServerManager模块,我正在使用Admin priv运行PS,而我正在运行64位版本的PS。
这是启用IIS的脚本(如果它尚未
)powershell_script 'Install IIS' do
code 'Add-WindowsFeature Web-Server'
guard_interpreter :powershell_script
not_if "(Get-WindowsFeature -Name Web-Server).Installed"
end
当我运行它时,我收到以下错误信息:
PS C:\Users\Administrator\chef-repo> chef-apply .\webserver.rb
Recipe: (chef-apply cookbook)::(chef-apply recipe)
* powershell_script[Install IIS] action run
================================================================================
Error executing action `run` on resource 'powershell_script[Install IIS]'
================================================================================
Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of "powershell.exe" -NoLogo -NonInteractive -NoProfile -ExecutionPolicy RemoteSigned -InputFormat
None -File "C:/Users/ADMINI~1/AppData/Local/Temp/chef-script20141224-1308-ftog62.ps1" ----
STDOUT:
STDERR: The term 'Add-WindowsFeature' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
---- End output of "powershell.exe" -NoLogo -NonInteractive -NoProfile -ExecutionPolicy RemoteSigned -InputFormat No
ne -File "C:/Users/ADMINI~1/AppData/Local/Temp/chef-script20141224-1308-ftog62.ps1" ----
Ran "powershell.exe" -NoLogo -NonInteractive -NoProfile -ExecutionPolicy RemoteSigned -InputFormat None -File "C:/Us
ers/ADMINI~1/AppData/Local/Temp/chef-script20141224-1308-ftog62.ps1" returned 1
Resource Declaration:
---------------------
# In .\webserver.rb
1: powershell_script 'Install IIS' do
2: code 'Add-WindowsFeature Web-Server'
3: guard_interpreter :powershell_script
4: not_if "(Get-WindowsFeature -Name Web-Server).Installed"
5: end
Compiled Resource:
------------------
# Declared in .\webserver.rb:1:in `run_chef_recipe'
powershell_script("Install IIS") do
action "run"
retries 0
retry_delay 2
guard_interpreter :powershell_script
command "\"powershell.exe\" -NoLogo -NonInteractive -NoProfile -ExecutionPolicy RemoteSigned -InputFormat None -File \"C:/Users/ADMINI~1/AppData/Local/Temp/chef-script20141224-1308-ftog62.ps1\""
backup 5
returns 0
code "Add-WindowsFeature Web-Server"
interpreter "powershell.exe"
cookbook_name "(chef-apply cookbook)"
recipe_name "(chef-apply recipe)"
not_if "(Get-WindowsFeature -Name Web-Server).Installed"
end
[2014-12-24T09:37:19-08:00] FATAL: Stacktrace dumped to C:/chef/cache/chef-stacktrace.out
[2014-12-24T09:37:19-08:00] FATAL: Mixlib::ShellOut::ShellCommandFailed: powershell_script[Install IIS] ((chef-apply coo
kbook)::(chef-apply recipe) line 1) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0
], but received '1'
---- Begin output of "powershell.exe" -NoLogo -NonInteractive -NoProfile -ExecutionPolicy RemoteSigned -InputFormat None
-File "C:/Users/ADMINI~1/AppData/Local/Temp/chef-script20141224-1308-ftog62.ps1" ----
STDOUT:
STDERR: The term 'Add-WindowsFeature' is not recognized as the name of a cmdlet, function, script file, or operable prog
ram. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
---- End output of "powershell.exe" -NoLogo -NonInteractive -NoProfile -ExecutionPolicy RemoteSigned -InputFormat None -
File "C:/Users/ADMINI~1/AppData/Local/Temp/chef-script20141224-1308-ftog62.ps1" ----
Ran "powershell.exe" -NoLogo -NonInteractive -NoProfile -ExecutionPolicy RemoteSigned -InputFormat None -File "C:/Users/
ADMINI~1/AppData/Local/Temp/chef-script20141224-1308-ftog62.ps1" returned 1
答案 0 :(得分:3)
对于那些试图通过Chef Basics教程并且仍然坚持这个错误的人 -
本教程似乎假设您拥有PowerShell 3.0,其中模块在需要时自动导入。对于PowerShell 2.0,情况并非如此,您必须在食谱中明确导入它们。
首先,通过运行$psversiontable
确保运行PowerShell 2.0。
PS C:\Users\Administrator\chef-repo> $psversiontable
Name Value
---- -----
CLRVersion 2.0.50727.5477
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
注意PSVersion
的价值 - 如果不是2.0
,您可能还有其他问题。
以下是导入右侧模块的更新脚本:
powershell_script 'Install IIS' do
code <<-EOH
Import-Module ServerManager
Add-WindowsFeature Web-Server
EOH
guard_interpreter :powershell_script
not_if "Import-Module ServerManager; (Get-WindowsFeature -Name Web-Server).Installed"
end
享受:)
答案 1 :(得分:1)
这可以使用windows_feature
(内置于厨师)来完成,因为这不是您认为的机会:
%w{ IIS-WebServerRole IIS-WebServer NetFx4Extended-ASPNET45 IIS-HttpCompressionDynamic IIS-WebServerManagementTools IIS-ManagementConsole IIS-ApplicationDevelopment IIS-ApplicationInit IIS-ISAPIFilter IIS-ISAPIExtensions IIS-NetFxExtensibility45 IIS-ASPNET45 IIS-ManagementScriptingTools IIS-HttpRedirect }.each do |feature|
windows_feature feature do
action :install
end
end
列表是我正在启用的所有功能......你只能这样做1个功能:
windows_feature 'IIS-WebServerRole' do
action :install
end
但上面的内容并不是IIS所需的全部内容...... 您可以在此处找到非常具体的信息:https://github.com/chef-cookbooks/windows