Given a string str, a character and a positive integer N. The string str is repeated indefinitely. The goal is to find the count of occurrences of character in str in first N characters of repetitions.If str is “abac”, character is ch=‘b’ and N is 10.In first 10 characters of “abacabacabacabac…….” b occurs twice.Note − Take str and character ch within the same case.Let us understand with examples.For ExampleInputstr = "TPTTTT" ch = 'T' n = 12OutputCount of occurrences of a character in a repeated string are: 10ExplanationThe number of ‘T’ in str is 5. Length of str is 6. ... Read More
Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive process.A recursive function is the one which has its own call inside it’s definition.If str1 is “I know that you know that i know” str2=”know”Count of occurences is − 3Let us understand with examples.For ExampleInputstr1 = "TPisTPareTPamTP", str2 = "TP";OutputCount of occurrences of a substring recursively are: 4ExplanationThe substring TP occurs 4 times in str1.Inputstr1 = "HiHOwAReyouHiHi" str2 = "Hi"OutputCount of occurrences of a substring recursively are: 3ExplanationThe substring Hi occurs 3 times in str1.Approach used ... Read More
To use the PSCustomObject inside the Foreach Parallel loop, we first need to consider how we are using the variables inside the loop.$Out = "PowerShell" ForEach-Object -Parallel{ Write-Output "Hello.... $($using:Out)" }So let see if we can store or change a value in the $out variable.Example$Out = @() ForEach-Object -Parallel{ $using:out = "Azure" Write-Output "Hello....$($using:out) " }OutputLine | 4 | $using:out = "Azure" | ~~~~~~~~~~ | The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept | assignments, such as a ... Read More
When we simply write the Invoke-Command, it shows the output on the console.ExampleInvoke-Command -ComputerName Test1-Win2k12 -ScriptBlock {Get-Service}OutputIt shows the output along with the computer name.Now let's say you want to sort the output or you want to work with the output you need to store it. It is similar like we store the output in the variable but we can’t store the output inside the scriptblock and display it outside.$ser = @() Invoke-Command -ComputerName Test1-Win2k12 -ScriptBlock {$ser = Get-Service} Write-Output "Services output" $serYou won’t get any output of the above command because Invoke-Command is known to work on the remote computer. ... Read More
To check the logged-in Azure user account in the console using PowerShell, you can check the context of the Azure and for that Get-AZContext command is used.ExampleGet-AzContextOutputIf you are already logged in with multiple user accounts then there may be chances that there are multiple contexts available, to list all the available context, use the below command,ExampleGet-AzContext -ListAvailableOutputYou can choose the context using the Select-AZContext command.
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP