0

I have a powershell script to search a string, and the script worked when I run it from powershell command prompt directly, but failed to run when I put it in userdata of heat template: The script is:

$regex = [regex]"(?<=\>)(\d+)(.*)SNAPSHOT(?=\/\<)"
$allsnapshot=$regex.Matches($testcode1) | % { $_.matches } | % { $_.value }  |get-unique |sort -descending

The error is:

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

I think it's a parse issue. but don't know how to fix it. So how can I avoid Python to parse it?

I struggled on it for several days. I appreciate any advice.

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
Celina yang
  • 59
  • 1
  • 10
  • Do you need to escape the '\' since this is in a string, such as '\\d' instead of '\d' (as well as other places where '\' is used)? – OnlineCop Apr 27 '15 at 17:12

1 Answers1

0

It's possible that Python might not allow you to use PowerShell v3 Object casting, which is what you're doing when you implicitly type a variable, like [xml](Get-Content .\SomeXml.xml) or in this case, your regex.

I'd recommend trying the tried and true PowerShell v1 and up object casting syntax, which looks like this:

$regex = New-Object -TypeName Regex -ArgumentList "(?<=\>)(\d+)(.*)SNAPSHOT(?=\/\<)" 

I've often run into problems just like your experiencing when I port my tools from newer versions of PowerShell and WMF to older versions. The errors can be indecipherable!

If this doesn't help, please let me know and we can try to work out a solution together.


Try to use a here-string, here is an example of one:

"""This is a multiline string
 with more than one line
 in the source code."""

as seen here: How to write string literals in python without having to escape them?.

Community
  • 1
  • 1
FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • the same error: At C:\Users\cloudbase-init\appdata\local\temp\5362e2d8-5c06-4984-9dea-468027607 106.ps1:25 char:58 + $regex = New-Object -TypeName Regex -ArgumentList "(?<=\> <<<< )(\d+)(.*)SNAP SHOT(?=\/\<)" + CategoryInfo : ObjectNotFound: (?<=\>:String) [], CommandNotFou ndException – Celina yang Apr 27 '15 at 00:48
  • I think these characters must be very special in Python, But how to bypass the parse? – Celina yang Apr 27 '15 at 00:51
  • Ah, I see. Your specific problem will need the help of a python expert to solve, then. I think you're right about this being an issue with python trying to understand the powershell command. – FoxDeploy Apr 27 '15 at 01:42
  • Check and see if python has a concept of a 'here string', if so, you could use that to tell python 'Dude, seriously, don't try to parse this, but execute it'. I'll check tomorrow and see what I can find. – FoxDeploy Apr 27 '15 at 01:43
  • I removed (?<=\>), then it reported error for (\b+); then I replaced \b+ with [0-9]+, the error moved to (.*): ObjectNotFound: (.*:String) [], CommandNotFoundException – Celina yang Apr 27 '15 at 03:22
  • I installed python 2.7.9 and try to print those three lines within """ """, the output is: 'This is a multiline string\nwith more than one line\n in the source code.' – Celina yang Apr 27 '15 at 18:11