0

The following PowerShell script:

$srch = '-v\s*(.*?)\s*=\s*%1'
$repl = "-v `'`$1'` = %1"
(Get-Content batchfile.bat) -replace $srch, $repl | Set-Content rave.bat

produces these results from a Windows 8.1 machine:

sqlcmd -S rave -v 'test me' = %1 -i rave_params.sql -o alert.txt

The same script produces these results from a Windows 2012 server:

sqlcmd -S rave -v test me = %1 -i rave_params.sql -o alert.txt

The script is supposed to produce results leaving the single quotes around the text. It works fine on the Windows 8.1 machine yet does not on Windows Server 2012. Any ideas what I can do to produce the same results on Windows Server 2012?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    If you could provide an example string that produces the outputted results? could it be that the second single quote in the $repl string has the backtick before the quote and not after it? – HAL9256 Mar 13 '14 at 19:11
  • Are there different versions of powershell on the two systems? – David Mar 13 '14 at 19:32
  • Windows 8.1 ships with PowerShell v4, Windows Server 2012 (not R2) with PowerShell v3. Also, you seem to have a typo in your code. The last backtick in `$repl` goes *before* the single quote. – Ansgar Wiechers Mar 13 '14 at 19:56
  • Yeah I think this would be a .NET 2.0 vs 4.0 issue. But the only change I see to .NET regex is in 4.5 - `Ability to limit how long the regular expression engine will attempt to resolve a regular expression before it times out. See the Regex.MatchTimeout property.` – Keith Hill Mar 13 '14 at 21:05
  • The 2012 Server is running Powershell 4 as I have updated it to match the same PS version on the Win 8 box. – woodlawn_la Mar 13 '14 at 21:07
  • One other thing - when I run the script on the 2012 box it looks as though it flashes by in a black PS box as opposed to the typical blue. Does that mean anything to u guys? – woodlawn_la Mar 13 '14 at 21:16
  • Here is the contents of batchfile.bat -> **sqlcmd -S rave -v test me = %1 -i rave_params.sql -o alert.txt** and here is the contents of how the replaced file is to look rave.bat -> **sqlcmd -S rave -v 'test me' = %1 -i rave_params.sql -o alert.txt** – woodlawn_la Mar 13 '14 at 22:42

1 Answers1

0

Without sample input it's hard to test, but does this give you any better results?

$srch = '-v\s*(.*?)\s*=\s*%1'
$repl = @'
-v '$1' = %1
'@
mjolinor
  • 66,130
  • 7
  • 114
  • 135
  • Your code works great as far as replacing the text. Although it still does not provide the single quotes around the **test me** after it has written to the new file. I can't seem to get powershell to output the text as **'test me'** with the single quotes around the txt. – woodlawn_la Mar 13 '14 at 22:25
  • Can you post the original text of the .bat file so I can test against your data? – mjolinor Mar 13 '14 at 22:31
  • Here is the contents of batchfile.bat -> **sqlcmd -S rave -v test me = %1 -i rave_params.sql -o alert.txt** and here is the contents of how the replaced file is to look rave.bat -> **sqlcmd -S rave -v 'test me' = %1 -i rave_params.sql -o alert.txt** – woodlawn_la Mar 13 '14 at 22:39
  • I just tested that on a 2012 Standard server OS (PS V3), and I get the 'test me' in quotes. – mjolinor Mar 13 '14 at 22:48
  • I believe you. I can't understand why it does not output that for me on this newly generated and patched 2012 server. Like I mentioned before - it works great on a Win 8 workstation but does not work correctly on the server. And the server is just an SQL box and IIS nothing else. – woodlawn_la Mar 13 '14 at 22:54
  • mjolinor - it starting working correctly after 2 days of fighting it. I made no changes whatsoever and now it works. Go figure. I am using your code so I will credit you with the answer. – woodlawn_la Mar 14 '14 at 15:20