定义要在批处理脚本中使用的变量 [英] Define variable to be used in the batch script

查看:84
本文介绍了定义要在批处理脚本中使用的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个安装服务的批处理脚本。我正在使用puppet使用Powershell在Windows上安装此服务。

I have a batch script which installs a service. I am using puppet to install this service on windows using Powershell.

C:\graylog-collector\bin\graylog-collector-service.bat install GrayLogCollector 

通过PS使用以上内容将安装该服务,因为它只是启动.bat文件。此服务需要 JAVA_HOME ,它已被设置为env系统变量,但是Puppet不知道。我试图在运行此批处理文件之前传递 JAVA_HOME 的值。我尝试了几种不同的方法,例如:

Using the above via PS would install the service as its simply launching the .bat file. This service requires JAVA_HOME which is already set as an env system variable, but Puppet does not know that. I am trying to pass the value of JAVA_HOME before running this batch file. I tried several different things, eg:

$JAVA_HOME = [Environment]::GetEnvironmentVariable("JAVA_HOME", "Machine"); C:\graylog-collector-0.5.0\bin\graylog-collector-service.bat install GrayLogCollector 

我不想编辑文件,也不想再次声明变量。我正在尝试使用系统中已设置的内容。基本上,尝试获取 JAVA_HOME 的值并将其输入.bat文件。

I do not want to edit the file and do not want to declare the variable again. I am trying to use what is already set in the system. Basically, trying to get the value of JAVA_HOME and feed it in the .bat file.

推荐答案

我可能会创建一个自定义事实来解决 JAVA_HOME 环境变量:

I would probably create a custom fact for resolving the JAVA_HOME environment variable:

Facter.add(:java_home) do
  setcode do
    ENV['JAVA_HOME']
  end
end

或(具有回退功能,用于定位 java 应该不设置环境变量的二进制文件):

or (more complex with a fallback for locating the java binary should the environment variable not be set):

Facter.add(:java_home) do
  setcode do
    begin
      ENV['JAVA_HOME'] || File.dirname(File.dirname(`readlink -f $(which java) 2>/dev/null`.chomp))
    rescue
      nil
    end
  end
end

Facter.add(:java_home) do
  confine :osfamily => 'Windows'
  setcode do
    begin
      ENV['JAVA_HOME'] || File.dirname(File.dirname(`where java 2>nul`.lines[0].chomp))
    rescue
      nil
    end
  end
end

命名文件 java_home.rb 并将其放置在正确的目录中。我们通常将这些事实放在应用程序配置文件模块的 lib / facter 中。

Name the file java_home.rb and place it in a proper directory. We usually place such facts in lib/facter of our application profiles module.

然后,您可以使用该事实来修改 exec 资源的环境:

Then you can use that fact to modify the environment of an exec resource:

exec { 'Install GraylogCollector service':
  command     => 'graylog-collector-service.bat install GrayLogCollector',
  unless      => 'sc query GraylogCollector',
  path        => [ 'C:\Windows', 'C:\Windows\system32', 'C:\graylog-collector\bin' ],
  environment => [ "JAVA_HOME=${::java_home}" ],
  logoutput   => true,
}

NB :我无法测试现在,所以我不能完全确定批处理脚本是否可以像这样直接运行(不过我希望这样做)。如果不只是将行更改为

NB: I can't test this right now, so I'm not entirely certain if the batch script can be run directly like that (I would expect it to, though). If it doesn't just change the line to

command => 'cmd.exe /c "graylog-collector-service.bat install GrayLogCollector"',

这篇关于定义要在批处理脚本中使用的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆