PowerShell Day 4
Hi guys ,???? as we all know PowerShell created by Jeffery Snover on 14th November,2006. PowerShell is a task automation and configuration management program from Microsoft which consist of common line shell and its associated scripting language. Today we're combining two topics. They are PowerShell input and output and PowerShell errors.
PowerShell Input and Output
Your PowerShell code will often need to evaluate some type of input. Did you know that you can use the output of cmdlet for the input of your PowerShell functions? You can also perform various functions based on the generated inputs depending on what a cmdlet returns. These varieties are:
PowerShell input from web
Here we use invoke-webRequest to retrieve web content from PowerShell. As a result ,the output content from the above cmdlet is in JSON. JSON is a structured data ,we can take control and convert it to native PowerShell objects. How can you go about using output from an output?
Ways to generate input ;
1.Leverage cmdlet output as input
2.Using read-host input
The read-host cmdlet reads a line from the console (standard input). IThe read-host cmdlet can be used to
One astonishing part is that the read-host cmdlet has a limit of 1022 characters ,it can also accept as input from a user. For example;
For the diagram below, when a value is entered and the key is pressed, the value is stored in the age variables
When the Enter key is pressed, the value is stored as a plaintext string object in the $pwd_string variable.
Add alt text
PowerShell Output
If you desire to display results back to the console or you would like to generate some type of Powershell output. There are few ways to accomplish this trade.
Write-host can output customized strings to the console ,you can customize the output by controlling the start of a new line or NoNewLine can also set up the foreground and background color.
It is also capable of string output, so you need to do is to make sure you are giving it a string ,if not write host will do its best to take in some type of output but not the expected results.
PowerShell Error-types
A program error is defined as an error which occurs during the development or encoding of a computer program, software or application ,which when in operation ,result in a malfunction of incorrect operation of a computer network. Hence if there is an error the software won't continue until the issue is corrected .One thing about PowerShell is that it doesn't behave the way and may terminate errors .
Terminating errors
Terminating errors is an error generated by script ,functions or commands you create and halts the execution of the script so that the commands in the next lines can't be executed.
Case where you want to force PowerShell to terminate ;
领英推荐
ErrorAction
?Common parameter that tells PowerShell what action to take when it encounters an error. The truth is, you’re always using the ErrorAction parameter. You’re just using the default setting when you don’t specify it, which is continue. Continue will display the error to the console and keep processing.
One thing I like about PowerShell is that it makes you smarter .??
#educ8africa ghana
Credits:
https://bit.ly/3WTXCN1