There are two different types of the variable we can use inside foreach parallel loop. One that is declared inside and the other that is declared outside of the foreach parallel loop.Please note − We are discussing here Foreach-Object Parallel loop, featured in PowerShell version 7. For a normal foreach loop inside and outside variables are the same.Variable declared inside the Foreach parallel loop can be used directly with its name. For example, Example$vms = "TestVm1", "TestVM2", "TestVm3" $Vms | ForEach-Object -Parallel{ $var1 = $_ Write-Output "Testing VM: $var1" }OutputTesting VM: TestVm1 Testing VM: TestVM2 Testing VM: TestVm3In ... Read More
Wait-Process cmdlet in PowerShell is used to wait for the process to stop before the execution moves on to the next step.ExampleWe have a snipping tool application running and we need to wait for the process to stop first and then move on to the next step.PS C:\> Get-Process SnippingTool | Select Name, Id, CPU Name Id CPU ---- -- --- SnippingTool 7440 2.0625To wait for the process to stop first we will use the Wait-Process command. You can provide ProcessName or ID.Write-Output "Waiting for the Process to Stop" ... Read More
Timeout.exe is actually a cmd command which can also be used in PowerShell. Let see the help related to Timeout command.timeout /?If we see the timeout parameters list we can use /T which indicates the Time in seconds and /NoBreak command, which Ignores any key for the specified time.ExampleWrite-Output "Timeout is for 10 seconds" Timeout /T 10 Write-Output "This line will be executed after 10 seconds if not interuptted"OutputPS C:\> C:\Temp\TestPS1.ps1 Timeout is for 10 seconds Waiting for 5 seconds, press a key to continue ...Please note: In the above example, the user can interrupt the timeout seconds using any key to disallow ... Read More
To send email using PowerShell, there are multiple methods but there is a simple command called SendMailMessage. This command is a part of the module called Microsoft.PowerShell.UtilityTo send email using the specific SMTP server we need to add the SMTP server parameter.Send-MailMessage ` -From 'User1@TestDomain.com' ` -To 'User2@TestDomain.com' ` -Subject 'Test Email' ` -SmtpServer 'Smtp.TestDomain.com'In the above example, an email will be sent from the -From parameter, a user to -To parameter users with the subject name ‘Test Email’ with the specified SMTP server name.If you have multiple users then you can separate them using a ... Read More
To retrieve the azure VMs using PowerShell, we can use Get-AzVM commands but before that make sure you logged in using Azure Credentials in the console. When you type this command, you will get the list of all VMs in the specified subscription.To check which all properties are supported you can use theGet-AzVM | gm -MemberType PropertiesYou can select different properties from there using the Select-Object command (Alias: Select). To retrieve the VMs from the specific ResourceGroup, use the below command.Get-AzVM -ResourceGroupName AUTOMATIONTESTRG2If your VM is in a different subscription then you need to switch the subscription and need to ... Read More
Before Installing the Azure cmdlets for PowerShell, it is recommended to upgrade it to the PowerShell version 7.X to leverage the new features.To install the PowerShell cmdlets for Azure, you need to download and install the AZ module.Install-Module -Name Az -AllowClobber -Scope CurrentUserTo install it for all the users, Install-Module -Name Az -AllowClobber -Scope AllUsersIf the AzureRM module is already installed, you first need to uninstall it because both modules AzureRM and AZ cannot reside in the same console and the AzureRm module is going to decommission soon. So anyway we need to upgrade it to the latest AZ Module.To ... Read More
To connect the azure account with PowerShell, we can use the Connect-AZAccount command. If we check the command parameters from the below URL, there are multiple methods we can connect to the azure account but in this article, we will use the simple methods to connect.Using the Interactive console to connect portalUsing DeviceLogin method.Using Credentials method.Using the Interactive console method to connect the portal.When we use the Connect-AZAccount directly without any parameter, it will open a popup for the azure portal credential.You need to enter your Azure credentials there.Using Device Login method.In this method, Connect-AZAccount uses the parameter -DeviceLogin. Once ... Read More
The default residual plot can be created by using the model object name in base R but that is not very attractive. To create a residual plot with better looking aesthetics, we can use resid_panel function of ggResidpanel package. It is created in the same way as the residual plot in base R, also it results in all the relevant graph in one window.ExampleConsider the below data frame −Live Demo> x y df dfOutputx y 1 0.48508894 0.217379409 2 0.75113573 -0.657179470 3 -0.13075185 -0.549613217 4 -0.26867557 1.156736294 5 0.40407850 0.640387394 6 -0.23816272 -0.807847198 7 -0.57278583 0.600249694 8 -0.78222676 -0.711133218 9 ... Read More
If we have two string vectors, each containing more than two values then it becomes a little difficult to create the combinations for each string value in those two vectors. For this purpose, we can make use of do.call function paste0 and expand.grid as shown in the below examples.ExampleLive Demo> x1 y1 do.call(paste0, expand.grid(x1, y1))Output[1] "AK" "BK" "CK" "DK" "EK" "FK" "GK" "HK" "IK" "JK" "AL" "BL" "CL" "DL" "EL" [16] "FL" "GL" "HL" "IL" "JL" "AM" "BM" "CM" "DM" "EM" "FM" "GM" "HM" "IM" "JM" [31] "AN" "BN" "CN" "DN" "EN" "FN" "GN" "HN" "IN" "JN" "AO" "BO" "CO" ... Read More
To get the top values in an R data frame, we can use the head function and if we want the values in decreasing order then sort function will be required. Therefore, we need to use the combination of head and sort function to find the top values in decreasing order. For example, if we have a data frame df that contains a column x then we can find top 20 values of x in decreasing order by using head(sort(df$x, decreasing=TRUE), n=20).ExampleConsider the CO2 data frame in base R −Live Demo> str(CO2)OutputClasses ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame': 84 obs. of ... Read More