将Coldfusion变量放入Powershell [英] coldfusion variables into powershell

查看:110
本文介绍了将Coldfusion变量放入Powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的powershell脚本,该脚本可以这样创建txt文件:-

I have a simple powershell script that creates a txt file thus:-

set-executionpolicy unrestricted -force

$MyVar = 'My Content'

$MyVar | out-file -FilePath "C:\_Testing\test.txt"

这是从此处的ColdFusion脚本中调用的:-

This is called from a ColdFusion script here:-

<cfexecute name="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    arguments="C:\ColdFusion9\wwwroot\testsite\wwwroot\_Testing\powershellTest.ps1"/>

那行得通-创建txt文件并将内容放入其中,但是我想做的是通过cfexecute将变量传递到$ MyVar中,以便内容是动态的.

And that works - creates the txt file and puts the content in but what I want to do though is pass a variable through the cfexecute into the $MyVar so that the content is dynamic.

非常感谢

保罗

推荐答案

您想要的是带有参数的脚本.参数定义放在脚本的顶部,如下所示:

What you want is a script that has parameters. Parameter definitions are put at the top of your script, and look like this:

param(
    [Parameter(Mandatory=$true)]
    [string]
    # Variable to test that parameter is getting set from ColdFusion
    $MyVar
)

$MyVar | Set-Content "C:\_Testing\test.txt"

首先出现的属性是有关参数的元数据.在示例中,我们声明参数是强制性的,如果未提供值,PowerShell将给出错误.

First come attributes, which are meta data about the parameter. In the example, we are stating the parameters is mandatory and PowerShell will give an error if a value isn't provided.

接下来是变量的类型.您可以使用任何.NET类型,例如[int][DateTime][Hashtable]

Next comes the variable's type. You can use any .NET type, e.g. [int], [DateTime], [Hashtable], etc.

在类型之后是变量的文档,当有人运行Get-Help powershellTest.ps1时很有用.

After the type, is the variable's documentation, useful when somebody runs Get-Help powershellTest.ps1.

最后,我们声明变量$MyVar.

您可以在 about_functions_advanced_pa​​rameters 帮助主题.

现在,最棘手的部分是您对PowerShell的调用实际上是通过cmd.exe first 进行的,因此,根据脚本参数的值,您可能需要做一些时髦的魔术才能获得事情引用正确.

Now, the tricky part is that your call to PowerShell is actually going through cmd.exe first, so depending on the value of your script's parameter, you may need to do some funky magic to get things quoted correctly.

此外,使用Set-Content代替Out-File. Out-File用于保存二进制对象和数据,并将在UCS-2中对文本文件进行编码. Set-Content将使用ANSI/UTF-8对文件进行编码.

Also, use Set-Content instead of Out-File. Out-File is intended for saving binary objects and data and will encode text files in UCS-2. Set-Content will encode the files in ANSI/UTF-8.

这篇关于将Coldfusion变量放入Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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