This is the 12th day of my participation in the Novembermore Challenge
What are PowerShell scripts?
PowerShell scripts are text files that contain command and control constructs. The command is a call to cmdlet. Control constructs are programming features that PowerShell provides, such as loops, variables, parameters, comments, and so on.
PowerShell script files have the.ps1 file extension. You can create and save these files using any text editor.
PowerShell consists of command line shell and scripting language. It began as a framework for automating administrative tasks in Windows. PowerShell has evolved into a cross-platform tool for performing a variety of tasks.
How to execute the script
.\myScript.ps1
Copy the code
PowerShell technology
PowerShell has many features written in typical programming languages. You can define variables, use branches and loops, capture command-line arguments, write functions, add comments, and so on. Our script needs to have three functions: variables, loops, and parameters.
Declare variables with $and assign values with =. Such as:
$loc = "East US"
$iterations = 3
Copy the code
Variables can hold objects. For example, the following definition sets the adminCredential variable to the object returned by the GET-Credential cmdlet.
$adminCredential = Get-Credential
Copy the code
To get the value stored in a variable, use the $prefix and its name, as follows:
$loc = "East US"
New-AzResourceGroup -Name "MyResourceGroup" -Location $loc
Copy the code
cycle
PowerShell has multiple loops: “For”, “Do… While “, “For… Each “, etc.
The core syntax is shown below; The example runs two iterations, each of which outputs the value I. The comparison operators are written as -lt for “less than”, -le for “less than or equal to”, -eq for “equal to”, -ne for “not equal to”, and so on.
Greater than is equal to gt, equal; Not equal, less than, less than or equal to equal or less than, greater than, greater than or equal to equal or greater than
For ($i = 1; $i -lt 3; $i++)
{
$i
}
Copy the code
parameter
When executing a script, arguments can be passed on the command line. You can provide a name for each parameter to help the script extract the value. Such as:
.\setupEnvironment.ps1 -size 5 -location "East US"
Copy the code
In this script, you capture values into variables. In this example, the parameters are matched by name:
param([string]$location, [int]$size)
Copy the code
You can omit the name from the command line. Such as:
.\setupEnvironment.ps1 5 "East US"
Copy the code
In this script, when parameters are not named, you rely on position to match:
param([int]$size, [string]$location)
Copy the code
To find the command
A cmdlet (pronounced “command-let”) is a compiled command. Cmdlets can be developed in.NET or.NET Core and invoked as commands in PowerShell. Thousands of CMDlets are provided with PowerShell installations. The hard part is knowing what they are and what problems they can solve for you.
Cmdlets are named according to the verb-noun naming standard.
Use get-help to discover commands
Use get-help cmdlet to learn about cmDlets
-
Enter the help command:
help Get-FileHash Copy the code
A more streamlined version of help output will now be displayed. This output looks like the following text:
NAME Get-FileHash SYNTAX Get-FileHash [-Path] <string[]> [[-Algorithm] {SHA1 | SHA256 | SHA384 | SHA512 | MD5}] [<CommonParameters>] Get-FileHash [-LiteralPath] <string[]> [[-Algorithm] {SHA1 | SHA256 | SHA384 | SHA512 | MD5}] [<CommonParameters>] Get-FileHash [-InputStream] <Stream> [[-Algorithm] {SHA1 | SHA256 | SHA384 | SHA512 | MD5}] [<CommonParameters>] PARAMETERS -Algorithm <string> Copy the code
You can use the arrow keys to move the results vertically by row. To view the results page by page, use the space bar.
Use get-member to discover objects
The get-member cmdlet will be passed based on the command so that the output can be filtered. A typical command-line call to get-member looks like the following example:
Get-Process -Name 'name-of-process' | Get-Member
Copy the code
This command first generates the object result by calling get-process. The results by using a pipe (|) passed as input to the Get – Member. In the return result, you will get a table containing the Name, MemberType, and Definition columns. You also get the type of the returned object.
To explore the PowerShell
An important feature of PowerShell is its built-in help system, which makes it easy to access information about PowerShell commands. If you’re having trouble writing, you can find Help on commands or PowerShell concepts by using the get-help command. For example, to see all the details of the write-output command, type and run the following command:
Get-Help -Name 'Write-Output' -Full
Copy the code
Began to practice
Use the techniques you’ve learned to print one message and then collect user input for reuse in a second message. Earlier, you displayed a message with just one line of code. You then request input from the user and reuse this value to print a custom message. Use both technologies to address this challenge.
No matter what you do, the code should generate the following output. In this example, replace the placeholder texts
and
with values of your choice.
Today's date is <current date>.
Today is the day <your name> began their PowerShell programming journey.
Copy the code
Your code should look something like this
$date = Read-Host "What is today's date"
$name = Read-Host "Please enter your name"
Write-Output "Today's date is $date."
Write-Output "Today is the day $name began their PowerShell programming journey."
Copy the code