Regex & powershell, replace values between "" (only between #################### in a file)?

Hi,

I have a ps1 in which I would like to replace all values for variables between ####################
So that would be: Variables.ps1
 ####################
$RDSServer = "server01"
$ExchangeServer = "server02"
etc
 ####################

How can I remove the values?
I could get the content via get-content c:\globalvariables
But  how then the regex to get the data between  ####################
and then replace the value between " " ?
J.
janhoedtAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Essentially you have to search for the text that demarks an editable region.  Before that, you emit the line unchanged.  The first time you encounter it, a flag gets flipped so that you know you should perform the remove operations.  And when you encounter it a second time, the flag gets flipped again, so that lines are emitted unchanged again.
Get-Content file.txt | ForEach -Begin { $editable = $false } -Process `
{
    If ( $_ -match "^\s*#+\s*$" )
    {
        # we have entered/exited editable region
        # change the editable state
        $editable = -not $editable
        $_
    }
    ElseIf ( -not $editable )
    { $_ }
    ElseIf ( $editable )
    {
        $_ -replace '(\s*\$.+=\s*("|'')).+(("|'').*)', '$1$2'
    }
} | Set-Content newfile.txt

Open in new window

aikimarkCommented:
Are you changing your own (executing) PS script?
janhoedtAuthor Commented:
And what should the script do? Don't see anything happening in the newfile.txt
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.

footechCommented:
For a file with the content in your example
####################
$RDSServer = "server01"
$ExchangeServer = "server02"
etc
 ####################

...it results in a file with the following:
####################
$RDSServer = ""
$ExchangeServer = ""
etc
 ####################


If that's not what you want, provide two sample files.
1) with sample input
2) with what you expect the output to be
janhoedtAuthor Commented:
Thanks, will check again Note: I m using $gloval:server01 but probably it does not matter?
janhoedtAuthor Commented:
meant global of course
footechCommented:
@janhoedt - Please followup to confirm that the script is doing what you want (or not).
janhoedtAuthor Commented:
Hi, thanks for the update.
Created a sample input, below, the output just should be a file with all variables between the ##### set tot ""

function get-test123{}

####################
#GENERAL
$Global:Company = "OurCompany"
$Global:Organization = "OurOrganization"
$Global:EmailSuffix = "@OurCompany.com"
$Global:DomainFQDN = $env:USERDNSDOMAIN
$Global:SCCMSiteCode01 = "SLT"
$Global:MailFromAddress = "noreply$EmailSuffix"
$Global:MailToAddressforAlerts = "mymailaddress$EmailSuffix"
#SERVERS
$Global:SCCMSiteServer01 = 'serversccm01'
$Global:PublicSMTPServer01 = 'SMTP' #SMTP server which does not need authentication
$Global:ConnectionBroker01 = 'server01'
#RDS
$Global:CollectionRemoteapps = 'CollectionRemoteapps01'
#SOFTWARE GENERAL
$Global:PackageSourcesShare = "\\$DomainFQDN\PackageSources\"
#SOFTWARE PER TECHNOLOGY
$Global:AppVPackagesShare = "\\$DomainFQDN\AppV"
#SCCM VARIABLES FOR deployments/queries
$Global:CMDeployPurpose = 'Required' #Available or Required
$Global:CMDeployLogFile = Join-Path  $PackageSourcesShare -childpath 'Deployments.log'
$Global:CMDistributionPointGroupName = "$Company DG"
$Global:RDSADGroupNames = @{ 
  Group01 = 'APPLICATION01'
  Group02 = 'APPLICATION02'
}
####################

#OtherStuff like
#FQDN
$Global:SCCMSiteServer01_FQDN =  ($SCCMSiteServer01 + "." + $DomainFQDN)
$Global:PublicSMTPServer01_FQDN = ($PublicSMTPServer01 + "." + $DomainFQDN)

Open in new window

aikimarkCommented:
Please provide an example where you have a similar (name = delimitedvalue) pattern outside of the ############ lines.  Your example can easily be changed with a simple replace, not involving any iteration or logic.
janhoedtAuthor Commented:
Sorry, don't get your question. The example I posted is exactly what I need.
aikimarkCommented:
$a = Get-Content "C:\Users\Mark\Downloads\Q_29088564.txt"
$a -match "((?:.|\n)+?##+)((?:.|\n)+?)(##+(?:.|\n)+)" > null
$matches[2] = ($matches[2] -replace ' = +([''"])([^\1]*?)\1', ' = $1$1')
$matches[1..($matches.count)] -join ''

Open in new window

Produces the following:
function get-test123{}

####################
#GENERAL
$Global:Company = ""
$Global:Organization = ""
$Global:EmailSuffix = ""
$Global:DomainFQDN = $env:USERDNSDOMAIN
$Global:SCCMSiteCode01 = ""
$Global:MailFromAddress = ""
$Global:MailToAddressforAlerts = ""
#SERVERS
$Global:SCCMSiteServer01 = ''
$Global:PublicSMTPServer01 = '' #SMTP server which does not need authentication
$Global:ConnectionBroker01 = ''
#RDS
$Global:CollectionRemoteapps = ''
#SOFTWARE GENERAL
$Global:PackageSourcesShare = ""
#SOFTWARE PER TECHNOLOGY
$Global:AppVPackagesShare = ""
#SCCM VARIABLES FOR deployments/queries
$Global:CMDeployPurpose = '' #Available or Required
$Global:CMDeployLogFile = Join-Path  $PackageSourcesShare -childpath 'Deployments.log'
$Global:CMDistributionPointGroupName = ""
$Global:RDSADGroupNames = @{ 
  Group01 = ''
  Group02 = ''
}
####################

#OtherStuff like
#FQDN
$Global:SCCMSiteServer01_FQDN =  ($SCCMSiteServer01 + "." + $DomainFQDN)
$Global:PublicSMTPServer01_FQDN = ($PublicSMTPServer01 + "." + $DomainFQDN)

Open in new window

aikimarkCommented:
Your requirement to limit the changes to the lines between the ############# lines is a bit of a red herring. If you don't actually have any possible changes outside those lines, the PS script can be simplified to this:
$a = Get-Content "C:\Users\Mark\Downloads\Q_29088564.txt"
$a -replace ' = +([''"])([^\1]*?)\1', ' = $1$1'

Open in new window


Or, all on one line:
(Get-Content "C:\Users\Mark\Downloads\Q_29088564.txt") -replace ' = +([''"])([^\1]*?)\1', ' = $1$1'

Open in new window

janhoedtAuthor Commented:
Great! Would you mind explaining what you are doing here?
Would love to understand it but don't :-)
aikimarkCommented:
This is a string replace operation using a regular expression pattern.
' = +([''"])([^\1]*?)\1'

* It looks for " =", followed by 1 or more space characters
* It looks for and captures the next character if it is an apostrophe or quote character.  Since this string is apostrophe delimited, the internal apostrophe has to be doubled.
* It captures zero or more characters that aren't apostrophe-or-quote.
* It looks for the same apostrophe-or-quote character that it found after the " = +".  This is a back reference.

' = $1$1'
If it finds such a pattern, it replaces it with " = " followed by two copies apostrophe-or-quote character that it found/captured.

==========================
While explaining this regex pattern, I realize that I don't need to capture this, since I'm not doing anything with it.
Therefore, the first part of the regex pattern can be simplified to:
' = +([''"])[^\1]*?\1'

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
janhoedtAuthor Commented:
Thanks! I defintely have to dive into regex one day :-)

One more thing I would like to also "anonymize"  in the same textfile

$Global:ADGroupNames = @{
  $Group01 = 'technicians-ourcompany'
  $Group02 = 'marketing-ourcompany'
}
=> should become @{ Group01 = '' etc

I should then do a
$a -replace ..... what exactly?
aikimarkCommented:
Isn't that happening with my posted script?  I see GROUP01 = ''
janhoedtAuthor Commented:
No, didn't see that happening in my variables ....
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.