?
Solved

use variable on regex

Posted on 2016-07-28
6
Medium Priority
?
47 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 85

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 13

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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 

Author Comment

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

Accepted Solution

by:
oBdA earned 2000 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

777 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