gpt4 book ai didi

powershell - 将Coldfusion变量放入Powershell

转载 作者:行者123 更新时间:2023-12-02 23:47:09 25 4
gpt4 key购买 nike

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

set-executionpolicy unrestricted -force

$MyVar = 'My Content'

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

这是从ColdFusion脚本中调用的:-
<cfexecute name="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
arguments="C:\ColdFusion9\wwwroot\testsite\wwwroot\_Testing\powershellTest.ps1"/>

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

任何帮助非常感谢

保罗

最佳答案

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

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

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

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

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

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

最后,我们声明变量 $MyVar

您可以在 about_functions_advanced_parameters帮助主题上获取有关参数,参数属性,验证等的更多信息。

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

另外,请使用 Set-Content而不是 Out-FileOut-File用于保存二进制对象和数据,并将在UCS-2中对文本文件进行编码。 Set-Content将使用ANSI / UTF-8对文件进行编码。

关于powershell - 将Coldfusion变量放入Powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17528100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com