Use the ErrorAction Parameter in PowerShell

Chirag Nagrekar
Updated on 27-May-2020 09:32:39

10K+ Views

Like ErrorActionPreference variable, ErrorAction parameter works similarly. ErrorAction parameter supported in Advance functions and most of the built-in cmdlets in PowerShell. It is useful to convert the non-terminating error to the terminating error and then you can handle them with try/catch blocks.Supported Values and Examples, Continue − This is the default value of the ErrorAction parameter and Error will be displayed and commands listed in Pipeline will be executed further.Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist -ErrorAction Continue Write-Host "`nHello World" -BackgroundColor DarkGreenOutput Get-WmiObject : The RPC server is unavailable. At line:1 char:1 + Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist -ErrorA ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ... Read More

Use the ErrorActionPreference Variable in PowerShell

Chirag Nagrekar
Updated on 27-May-2020 09:30:49

7K+ Views

ErrorActionPreference variable in PowerShell is to control the non-terminating errors by converting them to terminating errors. Error handling depends upon which value you assign to $ErrorActionPreference variable.The values are as below.Continue − This is the default value of the variable and when the error occurs, an error is displayed in the PowerShell console, and the script continues the execution.Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist Write-Host "Hello World"Output Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist Write-Host "Hello World" Get-WmiObject : The RPC server is unavailable. At line:2 char:1 + Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException + FullyQualifiedErrorId ... Read More

Terminating and Non-Terminating Errors in PowerShell

Chirag Nagrekar
Updated on 27-May-2020 09:27:29

2K+ Views

Powershell generates two types of errors when it executes script or commands. Terminating errors and Non-Terminating errors.Terminating error − This error generated by script, functions, or commands you create and it stops or halts the execution of the script so that the commands in the next lines can’t be executed. To handle this error proper mechanism is needed otherwise there would be an error message displayed.For example, PS C:\WINDOWS\system32>> This-commandnotexist This-commandnotexist : The term 'This-commandnotexist' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path ... Read More

Use of Error Variable in PowerShell

Chirag Nagrekar
Updated on 27-May-2020 09:25:09

3K+ Views

Error variable in PowerShell is to view the errors generated in the current PowerShell session. We can say that the $Error variable is the container that stores all the errors and the latest error will be displayed first. For the example below, we will set the $Errorview to Category view to minimizing the error display content. By default $ErrorView is a Normal view.$ErrorView = "Categoryview"Now we will see the $error variable example, PS C:\WINDOWS\system32> asdds ObjectNotFound: (asdds:String) [], CommandNotFoundException PS C:\WINDOWS\system32> Get-process asddsd ObjectNotFound: (asddsd:String) [Get-Process], ProcessCommandExceptionHere, there is one wrong command and one wrong input we have written so ... Read More

Use of LastExitCode and Variable in PowerShell

Chirag Nagrekar
Updated on 27-May-2020 09:24:41

6K+ Views

$LastExitCode in Powershell is the number that represents the exit code/error level of the last script or application executed and $? (Dollar hook) also represents the success or the failure of the last command. In general, both represent the same but the output method is different. The first command output is in the Number format (0 and 1) and the latter command is for Boolean (True or False) output.For example, PS C:\WINDOWS\system32> $LASTEXITCODE 0 PS C:\WINDOWS\system32> $? TrueAs you see the output, 0 represents the success status of the $LastExitCode command and $True for the $?.Now if the command doesn’t ... Read More

Data Stream as Disjoint Intervals in C++

Arnab Chakraborty
Updated on 27-May-2020 06:27:14

234 Views

Suppose we have a data stream input of integers, these are like a1, a2, ..., an, ..., we have to summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the input integers are 1, 3, 8, 2, 7, ..., then the summary will be −[1, 1][1, 1], [3, 3][1, 1], [3, 3], [8, 8][1, 3], [8, 8][1, 3], [7, 8].To solve this, we will follow these steps −Make a set called numsin the initializer, set low := -inf and high := infFrom the addNum method that takes num as input, insert num into the ... Read More

Self Crossing in C++

Arnab Chakraborty
Updated on 27-May-2020 06:14:58

285 Views

Suppose we have an array x of n numbers. We start at point (0, 0) and moves x[0] units to the north direction, then x[1] units to the west direction, x[2] units to the south direction , x[3] units to the east direction and so on. In other words, after each move our direction changes counter-clockwise. We have to devise an one-pass algorithm with O(1) extra space to determine whether our path crosses itself, or not.So if the array is like − [3, 4, 2, 5]Answer will be true.To solve this, we will follow these steps −insert 0 at the ... Read More

Patching Array in C++

Arnab Chakraborty
Updated on 27-May-2020 06:12:24

379 Views

Suppose we have an array nums and one number. We can add elements in the array, such that any number in range [1, n] (both are inclusive) can be formed by the sum of some elements in the array. We have to find the minimum number of required patches. So when the array is like [1, 4] and given number is n = 7, then output will be 1, as initially the nums are [1], [4] and [1, 4] = 5, now if we add 2 into array, then the nums will be [1], [2], [4], [1, 2], [1, 4], ... Read More

Remove Duplicate Letters in C++

Arnab Chakraborty
Updated on 27-May-2020 06:10:06

197 Views

Suppose we have a string consisting of only lowercase letters. We have to remove all duplicate letters such that all letters only occur once. And we have to display the result in the smallest lexicographic sequence. So if the input is like “abccb”, then the result will be “abc”To solve this, we will follow these steps −ans := one empty stringDefine one stack stDefine an array onStack of size 26Define one map mn := size of sfor initializing i := 0, when i < n, increase i by 1 do −increase m[s[i]] by 1for initializing i := 0, when i ... Read More

Count of Smaller Numbers After Self in C++

Arnab Chakraborty
Updated on 27-May-2020 06:06:19

401 Views

Suppose we have an array nums, we have to find another array called count, in this count array, the count[i] stores the number of smaller elements to the right of nums[i].So if the input is like: [5, 2, 7, 1], then the result will be [2, 1, 1, 0].To solve this, we will follow these steps −Define one method called update(), this will take index, array bit and nwhile index 0, do −ans = ans + bit[index]index = index – (index AND - index)return ansFrom the main method, do the following −n := size of numsDefine an array res ... Read More

Advertisements