IT Nota

  • Home
  • How To
  • .NET
  • WordPress
  • Contact

How to Add Comma to Each Line Using Visual Studio Code or Sublime Text

Sometimes the simplest thing is really taken for granted and we take the long route to do the simplest thing. We received a CSV with thousands of ID numbers that will be used to update a database table. The data would look like the following:

  87295
  86719
  31695
  94836
  17957
  69783
  57168
  96874
  12853
  92816
  ...
  [deleted for brevity]
  ...

Open a text file in Visual Studio Code

The goal is just to add a comma at the end of each line so it can be used to update data in a database table.

In the past, we would probably write a macro module in Excel, but other than a security risk, it’s overcomplicating a simple task.

Using either Visual Studio Code or Sublime Text, we can accomplish this task in a matter of seconds. No writing macros required.

1. Use the Search and Replace Control

This works the same way in both Visual Studio Code and Sublime Text. The only difference is the shortcuts between PC and Mac.

PC

  1. Press CTRL+H for Replace.
  2. Type in $ in Find, and , in Replace.
  3. Make sure Regular Expression is turned on (ALT+R)
  4. Replace All by pressing CTRL+ALT+ENTER.

Mac

  1. Press Option+Command+F for Replace.
  2. Type in $ in Find, and , in Replace.
  3. Make sure Regular Expression is turned on (Option+Command+R)
  4. Replace All by pressing Option+Command+ENTER.

Visual Studio Code Search and Replace All with Regex

2. Use Multi-Line Editing

Visual Studio Code

  1. Select all by pressing CTRL+A (or Command+A for Mac).
  2. From Selection, choose Add Cursors to Line Ends by pressing SHIFT+ALT+I (or SHIFT+OPTION+I).
  3. Type the Comma character.

Visual Studio Code Selection Add Cursors to Line Ends

Sublime Text

  1. Select all by pressing CTRL+A (or Command+A for Mac).
  2. From Selection, choose Split into Lines by pressing CTRL+SHIFT+L (or COMMAND+SHIFT+L).
  3. Move the cursors at the end of lines by pressing End on Windows or Right Arrow on macOS.
  4. Type the Comma character.

Whatever method you use, the end result will look like the following:

Text file with commas in Visual Studio Code

Further Reading

How to Reassign Shortcut Key for Column Selection in Visual Studio Code
How to Reassign Column Selection Shortcut Keys in Sublime Text
Adding Comma to Each Line Using Sublime Text 2
How to Open Visual Studio Code from command line macOS

October 5, 2021 Filed Under: How To Tagged With: Code Editor, Sublime Text, Visual Studio Code

How to Open Visual Studio Code from command line macOS

When you use Visual Studio Code on Windows, one handy feature is you can always call it from the command line by typing “code .” (without the quotes) in the folder where you want to work on.

But when you try that on macOS Terminal, this is what you get:

zsh: command not found: code

Not to worry, the same feature can be had on macOS but it’s just not installed by default. In order to do so, we need to do a one-time setup from Visual Studio Code.

  1. Launch Visual Studio Code.
  2. Press Cmd ⌘ + Shift ⇧ + P to open the Command Palette.
  3. Type in shell command and select the Shell command: Install ‘code’ command in PATH to install it.

    Visual Studio Code Command Palette - Shell Command code

  4. A screen will pop up stating:

    Code will now prompt with 'osascript' for Administrator privileges to install the shell command.

    Just click OK and authorize it.

Now, the “code .” command works the same way in macOS Terminal as it is on Windows Command Prompt.

If you use VSCodium, you don’t need to do any kinds of setup, it works right away.

Further Reading

How to Use Visual Code as Default Editor in Git
How to Enable Font Ligatures in Visual Studio Code
How to Reassign Shortcut Key for Column Selection in Visual Studio Code
How to Setup Visual Studio Code for Hugo Static Site Generator

Download

Visual Studio Code

August 23, 2021 Filed Under: How To Tagged With: Code Editor, Microsoft, Visual Studio Code

How to Get Application Pool Identity Password With AppCmd

Typically, web application is run using ApplicationPoolIdentity on IIS. However, for various reasons, it’s very common to find large corporations use service accounts to run the application pools.

IIS Application Pools example

Sometimes the challenge comes when you need to move the website to a different server and you’re not given the proper credential to set it up on the new environment. What’s worse, it’s also not uncommon to hear that sometimes not one soul in the company knows the password.

At that point, your options are usually to either have the password reset (a big No-No for a production application), or have a new service account created (also another headache if the service account is tied to other applications or batch jobs for logical grouping).

But there’s a simple trick that’s quite handy to get password from iis application pool by using AppCmd that is commonly used to migrate IIS websites.

Steps

  1. We know that the AppCmd can be used to export the Application Pool information to an XML file. And we can also export only a single AppPool. So in this case, just as an example, we want to export AppPool ITNOTA by typing the following in Command Prompt:

    C:\> %windir%\System32\inetsrv\appcmd list apppool "ITNOTA" /config /xml > C:\Temp\ItnotaAppPool.xml
    

    Export single AppPool by name to an XML file with appcmd.exe

  2. Once that was done, let’s open up ItnotaAppPool.xml file from C:\Temp folder. You would see something similar to the following:

    <?xml version="1.0" encoding="UTF-8"?>
    <appcmd>
      <APPPOOL APPPOOL.NAME="ITNOTA" PipelineMode="Integrated" RuntimeVersion="4.0" state="Started">
        <add name="ITNOTA" autoStart="true" managedRuntimeVersion="v4.0">
          <processModel identityType="SpecificUser"
                        userName="ITNOTA\Admin_Service_Account"
                        password="###MyAccountPassword###"
                        idleTimeoutAction="Suspend" />
          <recycling>
            <periodicRestart>
              <schedule>
              </schedule>
            </periodicRestart>
          </recycling>
          <failure />
          <cpu />
          <environmentVariables>
          </environmentVariables>
        </add>
      </APPPOOL>
    </appcmd>
    

    IIS AppPool export with identity password

  3. If you look at line 7 (highlighted), the password is there in clear text.

    Note: This XML is modified and compacted just to demonstrate where you can find the password ONLY if you use custom identity. If you use ApplicationPoolIdentity, there’s no password associated with it in the XML file.

In theory, you don’t need to look at the password to make the website run. Because importing the XML will transfer all the information exactly the same way as the current setting. So other than updating the IP address, the identity most of the time doesn’t need any modifications. But this trick is always useful, when you also need to retrieve a “lost” password for a service account that’s been there for years.

Further Reading

How to Migrate IIS Websites to a Different Server
Getting Started with AppCmd.exe
Exporting & Importing App Pools and Websites configuration between multiple IIS instances

August 20, 2021 Filed Under: How To Tagged With: IIS, Internet Information Services, Microsoft, Windows Server

How to Migrate IIS Websites to a Different Server

Setting up a website on IIS is very easy to do. However, it can get very tedious when we have to migrate multiple IIS websites to a new server. For a really brand new server, the easiest to migrate all those settings of course is to clone the server and then we can reconfigure the IP addresses for each web instance from IIS on the new server. But what if that option is not possible?

In this post, we’ll discuss two ways to migrate multiple IIS websites from one server to another. Ideally, this is done to an imaged server that does not have any websites set up yet. Otherwise, more tweaking is needed so we do not override the already existing sites on the new server.

Method 1: Using Shared Configuration in IIS

This method is the simplest to implement, but only use this if you have a brand new server. DO NOT use this method when migrating websites in addition to an already existing website (or websites) on the destination server.

Please be aware though if you migrate websites from one version of IIS to a newer one, the process can be messy but doable as long as you’re aware of all the gotchas.

  1. Launch IIS Manager and click on the main server node and click on Shared Configuration.

    IIS Shared Configuration

  2. In Shared Configuration screen, click the Export Configuration link.

    IIS Export Configuration option

  3. In the Export Configuration window, enter a Physical Path where you want the configuration files stored. Make sure whichever path you use, it’s an existing folder. Otherwise, you will get an error message that the path does not exist.

    Enter a strong Encryption keys password then click OK.

    IIS Configuration encryption keys password

  4. Check your export configuration folder, there should be three files created for you:

    1. administration.config
    2. applicationHost.config
    3. configEncKey.key

    IIS migration files export folder

  5. Copy all three files to the new server.

  6. Launch IIS Manager on the new server and click on Share Configuration.

    IIS Shared Configuration

  7. This time, check on the Enable shared configuration option and type in all required information to import the configuration from the first server. Click on Apply after you’re done.

    IIS Enable Shared Configuration Import - Physical path

  8. Optional: Typically you don’t need to user User name and Password, unless if you need your credential to access the Physical path where you store the export files. In this case, you can type in your user name (NTID or a service account) and password.

    IIS Enable Shared Configuration with username and password

  9. After clicking Apply link from either step 7 or step 8 (not both), you will be asked to enter your Encryption Keys Password you entered from step 3.

  10. If you entered the correct Encryption Keys Password, you’ll see a notice such as this:

    IIS Enable Shared Configuration notice

    Your existing IIS encryption keys will be backed up in the current configuration directory on your local computer. To restore these keys at a later time, turn off shared configuration.
    

    Just click the OK button.

  11. Finally, you’ll see a succesful message below:

    IIS Enable Shared Configuration successfully saved

    The changes have been successfully saved.
    
    You must close and reopen IIS Manager for it to recognize the configuration changes that you have made.
    

    Just click OK button again.

  12. Close and reopen IIS Manager to see the changes take effect.

That’s it.

Method 2: Using AppCmd.exe

From the Microsoft’s own documentation:

AppCmd.exe is the single command line tool for managing IIS 7 and above. It exposes all key server management functionality through a set of intuitive management objects that can be manipulated from the command line or from scripts.

One of the things you can do with AppCmd is to search, manipulate, export, and import IIS and ASP.NET configuration and configure application pools. So with that, what are the steps to migrate websites to a different server?

  1. From the server where you want to export all the websites, launch Command Prompt and type the following:

            C:\> %windir%\System32\inetsrv\appcmd list apppool /config /xml > C:\Temp\AppPools.xml
            
  2. This command exports all Application Pools in the server, so if you never delete the default ones, you might run into an issue when importing them to the new server. It’s harmless but at some point, you will need to delete them before you can successfully import it.

  3. Once we completed exporting the Application Pools, we still need to export the website settings separately.

    Back in Command Prompt, type in the following:

            C:\> %windir%\System32\inetsrv\appcmd list site /config /xml > C:\Temp\Websites.xml
            
  4. Copy the two XML files to the new server. As an example here, the assumption here is the files are copied to the same folder on the new server (C:\Temp).

  5. On the new server, launch Command Prompt and type the following:

            C:\> %windir%\System32\inetsrv\appcmd add apppool /in < C:\Temp\AppPools.xml
            
  6. If successful, you can right away import the websites by typing the following still in the Command Prompt:

            C:\> %windir%\System32\inetsrv\appcmd add site /in < C:\Temp\Websites.xml
            

That’s all there is to it. You might still need to do some adjustments in terms of changing the IP addresses etc. But at least the bulk majority of the settings are already transferred and doing it this way will save you a lot of time instead of doing it one by one.

Further Reading

How do you migrate an IIS 7 site to another server?
Exporting & Importing App Pools and Websites configuration between multiple IIS instances
Getting Started with AppCmd.exe
How to Get Application Pool Identity Password With AppCmd

August 18, 2021 Filed Under: How To Tagged With: IIS, Internet Information Services, Microsoft, Windows Server

How to Use RegEx to Replace Text in JavaScript

Just to build on top of the code from the previous post. This time, there is a page on a Web Forms application with a text that needs to be eliminated, but we no longer have the ability to re-compile. Fortunately, the change is cosmetic so we can resort to using plain vanilla JavaScript to solve this.

Again, the provided HTML, although it was from a real legacy application, it has been cut short so it’s not too distracting. What we need to do here is to remove Obsolete Service: and its content from the list of current services as displayed below:

HTML search string to be removed with JavaScript

This part of HTML was generated dynamically by a Web Forms and at this point recompiling is not an option. So what we wanted to do is to let the application still generates the page and we intercept the result with JavaScript and modify it after the fact.

<table>
  <tr bgcolor="#e4f4ff">
    <td valign="top">
      <span id="lblservicemsg"><b>Current Services:</b></span>
    </td>
    <td valign="top">
      <span id="lblCurrentServices" class="formtxt"><strong>Mobile: </strong> MobileCarrier <br><strong>Internet:
                </strong> Internet Provider <br><strong>Obsolete Service: </strong> ABN39482 <br><strong>Messaging: </strong>
                iMessage<br></span>
    </td>
    <td valign="top">
      <input type="submit" name="btnAddServices" value="Edit Services"
             onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;btnAddServices&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))"
             id="btnAddServices" class="formbtn" style="width:176px;">
    </td>
  </tr>
</table>

Studying the pattern above, it looks like the Obsolete Service: begins with a <strong> tag and ends with a <br>. However, it’s also known that between the closing </strong> tag and <br>, there could be more than one data point. So we can do a search using JavaScript to match the text Obsolete Service: and we need to use Regular Expression to match all entries between the closing </strong> tag and <br>.

Using the same logic of JavaScript to find the text in the previous post, we just need to grab the HTML text using innerHTML. Then we need to remove the instance of Obsolete Service using Regular Expression for the pattern matching.

We can use RegExr: Learn, Build, & Test RegEx website to try to match the pattern we’re looking for. And from trial and error, we found that to include all strings until the next occurence of <br> is by using (.+?)<br>. So the final pattern to use to capture what we want is <strong>Obsolete Service:(.+?)<br>.

RegEx pattern test for JavaScript

At this point, we can copy the pattern and paste it to our JavaScript code by substituting it with the variable searchText as such: `<strong>${searchText}(.+?)<br>` and assign the value to a constant regString. So whenever the script finds a match of this pattern, it will replace it with an empty string.

  const searchText = 'Obsolete Services:'

  document.addEventListener('DOMContentLoaded', function () {

    const spanTags = document.querySelectorAll('span')

    for (let i=0; i<spanTags.length; i++) {
      if (spanTags[i].innerHTML == searchText) {

        const elementId = spanTags[i].id

        // Get innerHTML
        const element = document.getElementById(elementId)
        const getInnerHTML = element.innerHTML
            
        // Use Regular Expression
        const regString = `<strong>${searchText}(.+?)<br>`
        let regex = new RegExp(regString)

        const replaceText = getInnerHTML.replace(regex, '')
        element.innerHTML = replaceText
      }
    }
  })

That’s it. Now, when the page is loaded, Obsolete Service is removed from the page.

Before

HTML search string to be removed with JavaScript

After

HTML string removed with JavaScript

Further Reading

How to Get HTML Elements with textContent in JavaScript
How to Search for IPv4 Addresses with RegEx in Visual Studio Code

August 10, 2021 Filed Under: How To Tagged With: JavaScript

« Previous Page
Next Page »
Buy me a coffee Support this site
Buy Me a Coffee?

Categories

  • .NET
  • Coding
  • Cybersecurity
  • Database
  • How To
  • Internet
  • Multimedia
  • Photography
  • Programming
  • Resources
  • Review
  • Tips and Tricks
  • Uncategorized
  • Use Case
  • WordPress
  • Writing

Recent Posts

  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency
  • How to Search for a String in All Tables in a Database

Recent Posts

  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency
  • How to Search for a String in All Tables in a Database

Tags

.NET .NET Core AdSense ASP.NET Cdonts Dll Classic ASP Code Editor ETL FSharp Genesis Framework Git Google HP Asset Manager HTML5 Hugo IIS Information Security Internet Internet Information Services iOS JAMStack Linux macOS Microsoft Microsoft SQL Server MVC PHP PowerShell Python Simple Mail Transfer Protocol Smtp Server SQL SQL Server SSIS SSMS SSRS Sublime Text Visual Studio Visual Studio Code VPN Windows Windows 8 Windows 10 Windows 2012 Windows Server

Copyright © 2011-2025 IT Nota. All rights reserved. Terms of Use | Privacy Policy | Disclosure