Microsoft Technologies Articles - Page 188 of 204

How to get website SSL certificate validity dates with PowerShell?

Chirag Nagrekar
Updated on 03-Jul-2020 08:20:27

12K+ Views

SSL certificates are a very crucial part of the website. They play a key role in securing the exchange of information on both client and server sides by activating an HTTPS secure connection. In the below article with the PowerShell, we will get the certificate validity date (starting and expiry date) for the certificate using PowerShell.To achieve this, we need to make httpwebrequest but before that, we will ignore SSL warning by the below command.[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }And then we wil make the HTTP web request by calling a .Net class.$url = "https://www.microsoft.com/" $req = [Net.HttpWebRequest]::Create($url)When we check the ... Read More

Explain Try/Catch/Finally block in PowerShell

Chirag Nagrekar
Updated on 06-Jun-2020 13:07:37

8K+ Views

Try/Catch block in PowerShell is to handle the errors which are produced in the script. To be specific, the errors should be terminating errors. The Finally block in the PowerShell is not mandatory to write each time along with Try/Catch but it will be executed regardless the error occurs or not.So when you use the Try block, the Catch block is mandatory but not Finally block.Try/Catch block with Terminating error − Below is the example of Terminating error without finally block.Exampletry{    This is not allowed    "This is Allowed" } catch{    Write-Host "Error occured" -BackgroundColor DarkRed }OutputPS C:\WINDOWS\system32> ... Read More

How to use the ErrorAction parameter in PowerShell?

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

11K+ 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

How to 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

What is 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

What is 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

What is the 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

Explain secure password Encryption with PowerShell.

Chirag Nagrekar
Updated on 16-May-2020 10:59:37

482 Views

Many times we need to use passwords in PowerShell and need to pass it to the credential parameter and a password should be always a secure string, not a plain text. There are few methods to encrypt the password as mentioned below.a) Get-Credential FormatWe have one method where we can store the username and password is through cmdlet Get-Credential. It will provide a GUI prompt. You can store this password into a variable and use it later in the command.$cred = Get-CredentialCredentials are stored into $cred variable. Here is the value of the variable. output below.PS C:\WINDOWS\system32> $cred UserName   ... Read More

What is the difference between –Match, -Like and –Contains Operator in PowerShell?

Chirag Nagrekar
Updated on 15-May-2020 13:05:42

9K+ Views

All the above 3 operators (Match, Like, and Contains) mentioned are the comparison operator in PowerShell. Match and Like operators are almost similar operator only difference is the Wildcard character and the Contains operator is entirely different. We will see the example and understand how they work.ExamplePS C:\WINDOWS\system32> "This is a PowerShell String" -Match "PowerShell" True PS C:\WINDOWS\system32> "This is a PowerShell String" -Like "PowerShell" False PS C:\WINDOWS\system32> "This is a PowerShell String" -Contains "PowerShell" FalseIf you see the output of the above example, the result is True only for the Match statement and reason is when you match the ... Read More

How to write Progress Bar in PowerShell?

Chirag Nagrekar
Updated on 15-May-2020 13:03:20

3K+ Views

When we write a script in PowerShell, users who execute it, expect to see the progress of the script instead of waiting idle and looking at the blank cursor while the script is executing the background task. One way to achieve is to use the Verbose parameter to see the progress but it doesn’t show the graphical progress. To see the graphical progress, you can use Write-Progress cmdlet supported by PowerShell.Write-Progress cmdlet mainly depends on 3 parameters.Activity − Title of the progress bar or you can mention the activity name that is being performed.Status − Subtitle of the Progress bar. ... Read More

Advertisements