我有一个用于搜索字符串的powershell脚本,当我直接从powershell命令提示符运行它时,该脚本起作用了,但当我将它放入heat模板的userdata中时,它无法运行:该脚本是:
$regex = [regex]"(?<=\>)(\d+)(.*)SNAPSHOT(?=\/\<)"
$allsnapshot=$regex.Matches($testcode1) | % { $_.matches } | % { $_.value } |get-unique |sort -descending错误是:
execute_user_data_script C:\Program Files (x86)\Cloudbase Solutions\Cloudbase-Init\Python27\lib\site-packages\cloudbaseinit\plugins\windows\userdatautils.py:58
2015-04-25 12:11:45.140 1796 DEBUG cloudbaseinit.plugins.windows.userdatautils [-] User_data stderr:
The term '?<=\>' 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.
At C:\Users\cloudbase-init\appdata\local\temp\835603b2-b3dc-4a9b-a156-029c75322
a8f.ps1:26 char:24
+ $regex = [regex]"(?<=\> <<<< )(\d+)(.*)SNAPSHOT(?=\/\<)"
+ CategoryInfo : ObjectNotFound: (?<=\>:String) [], CommandNotFou
ndException
+ FullyQualifiedErrorId : CommandNotFoundException我认为这是一个解析问题。但不知道如何修复它。那么我如何避免使用Python来解析它呢?
我在上面挣扎了好几天。我很感谢你的建议。
发布于 2015-04-25 22:15:40
Python可能不允许您使用PowerShell v3对象类型转换,这就是您在隐式输入变量时所做的事情,比如xml或在本例中是您的正则表达式。
我建议您尝试一下久经考验的PowerShell v1和up对象类型转换语法,如下所示:
$regex = New-Object -TypeName Regex -ArgumentList "(?<=\>)(\d+)(.*)SNAPSHOT(?=\/\<)" 我经常遇到像你这样的问题,当我把我的工具从新版本的PowerShell和WMF移植到旧版本时。错误可能是无法辨别的!
如果这不起作用,请告诉我,我们可以试着一起找出一个解决方案。
尝试使用here-string,下面是一个示例:
"""This is a multiline string
with more than one line
in the source code."""如下所示:How to write string literals in python without having to escape them?。
https://stackoverflow.com/questions/29865287
复制相似问题