Solved

use variable on regex

Posted on 2016-07-28
6
32 Views
Last Modified: 2016-07-29
Hello,

I try to use variable $_.fg_name  on regex but the result is []


$file = "C:\test\mv_ddl.sql"
$_.fg_name = "Primary"



(Get-Content $file) | ForEach-Object {$_ -replace [regex]::Escape("ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]"), "ALLOW_PAGE_LOCKS  = ON) ON \[$($_.fg_name))\]" -replace [regex]::Escape(") ON [PRIMARY]"), ") ON \[$($_.fg_name))\] TEXTIMAGE_ON [LOB]"} | Set-Content $file

How can I resolve, please?

Thanks

Regards
0
Comment
Question by:bibi92
  • 3
  • 2
6 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 41733343
$_ is the loop variable inside the ForEach, which is the string from the input file currently being processed.
Just use a regular variable (and please put code inside code style tags, see the buttons over the Edit field).
$file = "C:\test\mv_ddl.sql"
$fg_name = "Primary"

(Get-Content $file) | ForEach-Object {$_ -replace [regex]::Escape("ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]"), "ALLOW_PAGE_LOCKS  = ON) ON \[$($fg_name))\]" -replace [regex]::Escape(") ON [PRIMARY]"), ") ON \[$($fg_name))\] TEXTIMAGE_ON [LOB]"} | Set-Content $file

Open in new window

0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41733350
You need to get that variable another way, $_ is referring to the object piped in.  For example:

1,2,3 | %{ write-host $_ } 

Open in new window


So you need to reference the object which contains the property you want to use.  What object contains fg_name and can you post code where you are getting that object?
0
 

Author Comment

by:bibi92
ID: 41733364
I test Obda solution the result for the variable is  \[)\].

Thanks
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:bibi92
ID: 41733384
Thanks a lot
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41733391
On closer inspection, you had a closing parenthesis too many, and since the patterns will be escaped by [regex]::Escape, you don't need to escape the square brackets.
$file = "C:\test\mv_ddl.sql"
$fg_name = "Primary"

(Get-Content $file) | ForEach-Object {$_ -replace [regex]::Escape("ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]"), "ALLOW_PAGE_LOCKS  = ON) ON [$($fg_name)]" -replace [regex]::Escape(") ON [PRIMARY]"), ") ON [$($fg_name)] TEXTIMAGE_ON [LOB]"} | Set-Content $file 

Open in new window

0
 

Author Closing Comment

by:bibi92
ID: 41734766
thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now