Solved

use variable on regex

Posted on 2016-07-28
6
35 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

24 Experts available now in Live!

Get 1:1 Help Now