我有一个powershell脚本来搜索字符串,当我直接从powershell命令提示符运行它时脚本工作,但是当我把它放在热模板的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解析呢?
我挣扎了好几天。我很感激任何建议。
答案 0 :(得分:0)
Python可能不允许您使用PowerShell v3对象转换,这是您在隐式输入变量时所做的事情,例如[xml](Get-Content。\ SomeXml。 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?。