1

Background

I'm trying to write a small utility that searches through a file to find all instances of lines that look like this:

 [SecuredEndpoint(widgets.UpdateWidget)]

And make it look like this:

 //[SecuredEndpoint(widgets.UpdateWidget)]
 [AllowAnonymous]

So far, I have the following script - haven't written it back to the file yet.. just doing everything in memory first.

#Find and comment out all [SecuredEndpoint] attributes
(Get-Content -path .\TestController.cs -Raw) -replace '\[SecuredEndpoint','//\[SecuredEnpoint' | Foreach-Object {
        $_ # send the current line to output
        if ($_ -match "//[SecuredEndpoint") 
        {
            #Add Lines after the selected pattern 
            "[AllowAnonymous]"
        }
    }

It correctly comments out all the various the SecuredEndpoint lines, but it doesn't insert a new line afterwards. So the results look like this:

    [Route("widgets/")]
    [HttpGet]
    //[SecuredEnpoint(mywidget.GetAll)]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredEnpoint(mywidget.Create)]
    public List<Widgets> GetWidgets()
    {
    }

I don't get any errors.

Questions

What am i missing in the logic to make it insert the new line? Once that's fixed, I'm assuming I can do something like this to actually save the contents back to the file:

(Get-Content -path .\TestController.cs -Raw) -replace '\[SecuredEndpoint','//[SecuredEnpoint' | Foreach-Object {
        $_ # send the current line to output
        if ($_ -match "//\[SecuredEndpoint") 
        {
            #Add Lines after the selected pattern 
            "[AllowAnonymous]"
        }
    } Set-Content .\TestController.cs

Please and thanks!

5
  • 1
    I'm surprised you don't receive a parsing error - "//[SecuredEndpoint" is not a valid regex pattern, it needs to be "//\[SecuredEnpoint" Commented Feb 9, 2023 at 16:03
  • 1
    You could use a literal replacement method .Replace('[SecuredEndpoint(widgets.UpdateWidget)]', "//[SecuredEndpoint(widgets.UpdateWidget)]`n[AllowAnonymous]") and read the file as single multiline string with -Raw Commented Feb 9, 2023 at 16:11
  • @SantiagoSquarzon would you mind posting an answer with the sample code? This way I can make sure I'm following your suggestion properly and accept your answer? Please and thanks! Commented Feb 9, 2023 at 18:15
  • Also in case it wasn't clear, the text after each instance of '[SecuredEndpoint(' can be different so the replace method needs to look for the pattern [SecuredEndpoint*'. Updating question to be clearer Commented Feb 9, 2023 at 18:26
  • The literal replacement method won't work in that case. Doesn't allow wildcards or regex. Commented Feb 9, 2023 at 18:30

2 Answers 2

2

The following regex replacement might do the trick:

(Get-Content .\TestController.cs -Raw) -replace '\[SecuredEnpoint\(.+?\)]', "// `$0`n[AllowAnonymous]"

See https://regex101.com/r/WZDaEx/2 for regex details.

If you want to match SecuredEnpoint using case sensitive comparison use -creplace instead.

If there could be already commented [SecuredEnpoint(...)] lines and you want to avoid adding a new layer of comments you could use the following:

(Get-Content .\TestController.cs -Raw) -replace '(?<!//\s*)\[SecuredEnpoint\(.+?\)]', "// `$0`n[AllowAnonymous]"

See https://regex101.com/r/6gPgws/1 for regex details.

Sign up to request clarification or add additional context in comments.

4 Comments

;-} HA... @Santiago Squarzon, beat me to it. I just posted a similar one, with a minor change, but deleted it when I saw yours. ;-} Well, that and you led with the initial prudent answer, that my post was adjusting a bit, to what you just showed in your second response.
@postanote mind as well undelete it if you have it already ;)
;-} well.... ok. done.
@postanote I upvoted your answer to thank you! thanks to you both!
2

A quick refactoring of '@Santiago Squarzon' helpful answer. Try it this way.

Well, in case there are multiple files that may require updating as well, with other mixed string content.

'   
    [Route("widgets/")]
    [HttpGet]
    //[SecuredEnpoint(mywidget.GetAll)]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredEnpoint(mywidget.Create)]
    public List<Widgets> GetWidgets()
    {
    }
' | 
Out-File -FilePath 'D:\Temp\TestController.cs' -Force

'   
    [Route("widgets/")]
    [HttpGet]
    //[SecuredRemoteEnpoint(mywidget.GetAll)]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredLocalEnpoint(mywidget.Create)]
    public List<Widgets> GetWidgets()
    {
    }
' | 
Out-File -FilePath 'D:\Temp\TestController1.cs' -Force

Clear-Host
(Get-ChildItem -Path 'D:\Temp' -Filter '*TestController*.cs').
ForEach(
    {
        "********* Updating $(($PSItem).Name) content *********"

        Get-Content -Path $PSItem.Fullname 

        (Get-Content $PSItem.Fullname -Raw) -replace '\[Secured.*\]', "`$0`n`t[AllowAnonymous]" | 
        Set-Content -Path $PSItem.Fullname

        Get-Content -Path $PSItem.Fullname 
    }
)

# Results
<#
********* Updating TestController.cs content *********
   
    [Route("widgets/")]
    [HttpGet]
    //[SecuredEnpoint(mywidget.GetAll)]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredEnpoint(mywidget.Create)]
    public List<Widgets> GetWidgets()
    {
    }

   
    [Route("widgets/")]
    [HttpGet]
    //[SecuredEnpoint(mywidget.GetAll)]
    [AllowAnonymous]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredEnpoint(mywidget.Create)]
    [AllowAnonymous]
    public List<Widgets> GetWidgets()
    {
    }


********* Updating TestController1.cs content *********
   
    [Route("widgets/")]
    [HttpGet]
    //[SecuredRemoteEnpoint(mywidget.GetAll)]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredLocalEnpoint(mywidget.Create)]
    public List<Widgets> GetWidgets()
    {
    }

   
    [Route("widgets/")]
    [HttpGet]
    //[SecuredRemoteEnpoint(mywidget.GetAll)]
    [AllowAnonymous]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredLocalEnpoint(mywidget.Create)]
    [AllowAnonymous]
    public List<Widgets> GetWidgets()
    {
    }
#>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.