![]() The`n at the start and end of the string is to insert a blank line before and after that output, just to give it some visual separation between the input line and the list of names & numbers. It simply says how many names and phone numbers the script is going to generate, and asks the user to be patient while the script does its work. The Write-Host line is pretty straightforward. That's just a placeholder comment to show you where the next section (where the real work gets done) is going to fit. Please be patient.`n"ĭon't worry, we're not going to leave you on your own to figure out the random name & number generator code. Write-Host "`nGenerating $UserInput names & phone numbers. Once the user has properly given a number as input, we want the script to announce that it's about to start actually doing its work and then get about doing it. Here, we're making a very short-hand reference for the Get-Random command which is going to be used a lot later on. These can be useful to help us get acquainted with the new interface (e.g.: PowerShell has built-in aliases like dir -> Get-ChildItem and cat -> Get-Content) or to make short-hand references for commonly-used commands. ![]() One more preparation to make is an alias that we'll be really glad to have later on.Īliases are used to create alternate names for commands. If all things are in order, the script will continue on. However, the PowerShell ISE behaves a bit differently so you may want to keep this in if you plan on running the script from there. At the regular PowerShell console, Remove-Variable is not really needed for this particular purpose because variables set by scripts are normally discarded when the script exits. Then, Remove-Variable will clean up all the variables we've created and Exit will quit the script. If $MissingFiles exists, and is non-zero, Write-Host will post a message telling you how many files were missing and that the script will abort. So if Test-Path returns false (that is, the file we're looking for does not exist), it will be converted to true so that the if statement will execute its script block. The exclamation point in there is a not operator, which reverses the response of Test-Path before passing it on to the if statement. Test-Path is a simple command that, when given a file path, returns a basic true or false response to tell us if the path points to something that exists. Within that script block, the if statement uses Test-Path to see if the file we're looking for is where it belongs. This chunk of script sends the $RequiredFiles array into a ForEach-Object block. ![]() Write-Host "$_ not found." -ForegroundColor Red We'll use this, along with the $ScriptFolder variable, in the next piece where we check to be sure those files are present. The second line creates an array of file names that are required for the script to run properly. Normally, this is dumped to the console but it can also be redirected by other commands. Write-Output actually generates output. ![]() This is used to present messages to the user, and is not included in the script's output if the output gets redirected. This can be used to display the contents of a text file at the console or, as in this script, pass the contents along the pipeline to be used with other commands. It gets the contents of a specified object - usually a file. Get-Content does what it says on the tin.If anything goes wrong with the script block specified for try, the catch block will run. catch statements help with error handling. Once something happens that causes the condition to no longer be true when the script block is finished, the loop exits. while statements allow you to continuously repeat a script block so long as a certain condition is met.Switch statements also use the $_ variable to refer to the current item being processed. You can also, optionally, specify a default block which will only run if no other conditions are matched. Switch will check an object against several conditions, and run whatever script blocks are specified for conditions that the object matches. switch statements are like if statements with more choices.else statements allow you to perform an operation only if certain conditions are met, and (optionally) specify what should be done when that condition is not met. Within a ForEach-Object script block, the $_ variable is used to refer to the current item being processed. ForEach-Object takes an array, or list, of objects and performs the specified operation on each of them. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |