6

Reading Data from the Command Line in PowerShell–#SQLNewBlogger

 2 years ago
source link: https://voiceofthedba.com/2021/11/22/reading-data-from-the-command-line-in-powershell-sqlnewblogger/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Reading Data from the Command Line in PowerShell–#SQLNewBlogger

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

In every language I’ve coded in, there is a way to read input from the command line. From INPUT in BASIC, readln in PASCAL, scanf in C, read in LISP, input() in Python, and more, every language can do this.

I knew PowerShell could, and I wanted to find out how to do it. I’m building a CLI tool for SQL Saturday, and I am looking to get user input.

A quick search led me to Read-Host. I probably should have guessed this, but it does what I need. A quick example of where I started. This code gets an event number from a user and then prints if back out, along with a string created from the number:

$EventNumber = Read-Host -Prompt "Event Number:"
$DataFilename = "SQLSat$($EventNumber).yml"
# write the data
write-host("Event: $EventNumber")
write-host("File: $DataFilename")

I can run this, as shown here, and put in a number.  I typed in the first 1022 below:

I’ll use this to get information from the user and then produce a YAML file that will contain what’s needed to publish, or update, an event.

SQLNewBlogger

This post took me about 10 minutes to assemble. I was already working through this process and stopped to jot down an idea, save some code, and make a screenshot. That was about 2 minutes out of my day, and then I went on with coding. Later, I wrote this.

You could do the same thing. Take a minute out of your daily work, sketch a quick post, and then finish it later.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK