Solved

use variable on regex

Posted on 2016-07-28
6
39 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
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

 

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

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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