Categories: DatabasesNews

Creating an HTML URL from a PowerShell String–#SQLNewBlogger from Blog Posts – SQLServerCentral

2 min read

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

I wrote about getting a quick archive of SQL Saturday data last week, and while doing that, I had some issues building the HTML needed in PowerShell. I decided to work through this a bit and determine what was wrong.

My original code looked like this:

$folder = "E:DocumentsgitSQLSatArchiveSQLSatArchiveSQLSatArchiveClientApppublicAssetsPDF"
$code = ""
$list = Get-ChildItem -Path $folder
ForEach ($File in $list) {
#write-host($File.name)
$code = $code + "<li><a href=$($File.Name)>$($File.BaseName)</a></li>"
}
write-host($code)

This gave me the code I needed, which I then edited in SSMS to get the proper formatting. However, I knew this needed to work.

I

had used single quotes and then added in the slashes, but that didn’t work. This code:

$folder = "E:DocumentsgitSQLSatArchiveSQLSatArchiveSQLSatArchiveClientApppublicAssetsPDF"
$code = ""
$list = Get-ChildItem -Path $folder
ForEach ($File in $list) {
#write-host($File.name)
$code = $code + '<li><a href="/Assets/PDF/$($File.Name)" >$($File.BaseName)</a></li>'
}
write-host($code)

produced this type of output:

<li><a href="/Assets/PDF/$($File.Name)" >$($File.BaseName)</a></li>

Not exactly top notch HTML.

I decided that I should look around. I found a post on converting some data to HTML, which wasn’t what I wanted, but it had a clue in there. The double quotes.

I needed to escape quotes here, as I wanted the double quotes around my string. I changed the line building the string to this:

$code = $code + "<li><a href=""/Assets/PDF/$($File.Name)"" >$($File.BaseName)</a></li>"

And I then had what I wanted:

<li><a href="/Assets/PDF/1019.pdf" >1019</a></li>

Strings in PoSh can be funny, so a little attention to escaping things and knowing about variables and double quotes is helpful.

SQLNewBlogger

This was about 15 minutes of messing with Google and PoSh to solve, but then only about 10 minutes to write up.

A good example that shows some research, initiative, and investigation in addition to solving a problem.

The post Creating an HTML URL from PowerShell String–#SQLNewBlogger appeared first on SQLServerCentral.

Share
Published by

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago