Found 463 Articles for PowerShell

How to use –Timeout parameter in Restart-Computer cmdlet in PowerShell?

Chirag Nagrekar
Updated on 24-Jul-2020 10:36:28

1K+ Views

As the name describes, with –Timeout parameter, you can restrict time for validations (PowerShell, WinRM, and WMI connectivity check after reboot) for –Wait and –For parameters and if the checks don’t complete during that time then it returns an error. This timeout value is in seconds. You can specify this parameter with either –Wait or –For parameter but with –For parameter you need to include –Wait parameter as well.When -Timeout is specified with –Wait parameter, the overall checks (3 validations: PowerShell, WMI, and WINRM connectivity) then overall check time is restricted in seconds and when it is used with –For ... Read More

How to use -For parameter in Restart-Computer cmdlet in PowerShell?

Chirag Nagrekar
Updated on 24-Jul-2020 10:35:30

240 Views

In the –Wait parameter 3 main checks are validated − PowerShell, WMI, and WINRM connectivity. So whenever you specify the –Wait parameter, you have to wait until all three checks pass, however, if any of the checks fail then script freezes and can not further execute. If you know which specific check you want to perform then you can specify that value with the –For parameter as shown below.Here, for the example, we will run a single WMI check for the remote server.Restart-Computer Test1-Win2k12 -Wait -For Wmi -Force-For parameter must be used with –Wait parameter. Here, again if something goes ... Read More

How to use –Wait parameter in Restart-Computer cmdlet in PowerShell?

Chirag Nagrekar
Updated on 24-Jul-2020 10:33:36

2K+ Views

When we use Restart-Computer command with the remote computer names, PowerShell restarts the mentioned remote computers without any checks or validations if the server has come up or not. This requirement is fulfilled with the - Wait parameter. Whenever the - Wait parameter specified, PowerShell performs the below 3 checks on the remote computer when the computer is restarting.This can be noticed in the Progress bar of the PowerShell console.PowerShell − If the computer can run the Powershell command on the remote machine.WMI − Performs the WMI query on the remote computer using the Win32_ComputerSystem command.WINRM − Checks the remote ... Read More

How to restart a remote system using PowerShell?

Chirag Nagrekar
Updated on 24-Jul-2020 10:30:17

2K+ Views

To restart the remote computer, you need to use the Restart-Computer command provided by the computer name. For example, Restart-Computer -ComputerName Test1-Win2k12The above command will restart computer Test1-Win2k12 automatically and if you have multiple remote computers to restart then you can provide multiple computers separated with comma (, ).For example, Restart-Computer -ComputerName Test1-Win2k12, Test2-Win2k12Restart signal will be sent to both the computers at a time in the above example.You can also use the Pipeline to restart remote computers. For example, "Test1-Win2k12", "Test2-Win2k12" | Restart-Computer -VerboseOR(Get-Content C:\Servers.txt) | Restart-Computer -Verbose Few servers have dependencies on the other servers so main servers ... Read More

How to generate a strong password using PowerShell?

Chirag Nagrekar
Updated on 13-Jul-2020 05:39:25

458 Views

To generate a strong password with the PowerShell, we can use 8 or more characters password. In the below example, we are going to use the 8 character password, you can change the number of characters as per your convenience.This script you can bind into windows forms and provide it to the IT helpdesk to set the password for new users.In this example, we will use the Random integers between 33 and 126 and convert it to the Character, considering them they are the ASCII values for characters.You can find more about ASCII values in the table provided in the ... Read More

How to copy Items to the destination path with the differentcredentials in PowerShell?

Chirag Nagrekar
Updated on 13-Jul-2020 05:37:34

2K+ Views

To copy the items with the different credentials you need to use the credential parameter in the Copy-Item cmdlet.For example,Copy-Item C:\temp\* -Destination \Remoteshare\c$\temp -Credential (Get- Credential)Or$creds = (Get-Credential) Copy-Item C:\temp\* -Destination \Remoteshare\c$\temp -Credential $creds

How to Zip / Unzip files or folders using PowerShell?

Chirag Nagrekar
Updated on 13-Jul-2020 05:36:01

2K+ Views

Now it is easy to ZIP or extract (unzip) the files or folders using PowerShell. PowerShell has added features of the Archive module (Microsoft.PowerShell.Archive) from PowerShell 5.1 version.If you have PowerShell older version (version 4.0 or less) then you can download and install the module from the website or through the command line. However, only download and installing or manually copying the Archive module to the Module folder won’t work because it needs some dependent DLL files which are provided by .Net framework 4.5 so you need to consider upgrading .Net framework as well if it is below the 4.5 ... Read More

How to create a User Menu in PowerShell?

Chirag Nagrekar
Updated on 03-Jul-2020 08:26:54

3K+ Views

When you are writing a script and if you want to provide the user to select one option among multiple values and based on it to execute the command, we can generally use the Switch command and for it, we will ask the user choice in the below script.There is another .Net method to create a user menu, we will see it after the example from the description mentioned above.a. Switch command methodFor the Switch command-based user selection, we will first display the message for the user and then give him a choice for the selection through Read-Host command as ... Read More

How to get website SSL certificate validity dates with PowerShell?

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

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

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

Advertisements