How PowerShell Pipeline Works - Part 2

Chirag Nagrekar
Updated on 16-Oct-2020 09:49:39

164 Views

In part-1 we have seen the PowerShell pipeline functionality using the ValueFromPipeline property. There is another cmdlet property known as ValueFromPipelineByPropertyName, which is also useful to know the PowerShell pipeline functionality.Like part-1 command, we can get this property name using the same Get-Command but the filter parameter we will use for the property is ValueFromPipelineByPropertyName.The below example is for the Stop-Service cmdlet.(Get-Command Stop-Service).ParameterSets.parameters | where{$_.ValueFromPipelineByPropertyName -eq 'True'} | Select Name, ParameterTypeOutputName ParameterType ---- ------------- Name System.String[]This means you can use Name property to stop the service. So here we will use Get-Service and its Name property to retrieve services and ... Read More

How PowerShell Pipeline Works - Part 1

Chirag Nagrekar
Updated on 16-Oct-2020 09:44:27

190 Views

PowerShell is made easier with the Pipeline structure. With the Pipeline structure, we can pass the input of the left side command output or string to the right side of the command as the input. For example, Get-Service | Out-File c:\services.txtIn the above example, we are passing Get-Service output as an object to the Out-File command which is the right side of the Pipeline as the Input. Before going into detail about how the Pipeline works, we need to understand that every command we write produces output and that output is already formatted by the PowerShell using Pipeline.For example, Get-Process ... Read More

Block Ports on Windows Using PowerShell

Chirag Nagrekar
Updated on 16-Oct-2020 09:39:26

3K+ Views

To block the port using PowerShell on the Windows OS, we need to change the firewall settings using the New-NetFirewallRule command.ExampleWe need to block the port 5985 on the computer. The below code will block all TCP Incoming requests on the 5985 port on the local computer.New-NetFirewallRule -DisplayName "Block WINRM HTTP Port" `                     -Direction Inbound `                     -LocalPort 5985 `                     -Protocol TCP `                     -Action Block To ... Read More

Open a Port in Windows using PowerShell

Chirag Nagrekar
Updated on 16-Oct-2020 09:35:20

2K+ Views

To open a port in the Windows Operating system, we need to know a few things. LikeFor which Profile we need to open port (Public, private, or Domain)? - OptionalWhich port do we need to open (port Number)?The direction of the port – Inbound (i.e Incoming requests) or Outbound (i.e. Outgoing requests).Protocol by name (TCP, UDP, ICMPv4, or ICMPv6) or Number (0-255).Once we have all the details we can open the port. In the below example, we need to open a port 5985 (WINRM HTTP) port on the computer which is currently blocked. So we will use the below command.New-NetFirewallRule -DisplayName "Allow WINRM HTTP Port" ` ... Read More

Change Files and Folders Attributes Using PowerShell

Chirag Nagrekar
Updated on 16-Oct-2020 09:28:49

10K+ Views

There are multiple files and folders attribute supported by the Windows Operating System. To check which attributes that files and folders support use DOS command attrib /?You can see the attributes listed like Read-Only, Archive, etc. You can set the attribute using PowerShell.For example, we have a file called TestFile.txt and its attribute is ReadOnly and we need to change it to the Archive.PS C:\> (Get-ChildItem C:\Temp\TestFile.txt).Attributes ReadOnlyChange Attribute code −$file = Get-ChildItem C:\Temp\TestFile.txt $file.Attributes = 'Archive'So we have set the attribute to the ‘Archive’ from ‘ReadOnly’ and when you check it, the attribute should be changed.PS C:\> (Get-ChildItem C:\Temp\TestFile.txt).Attributes ... Read More

Retrieve Files and Folders Attributes Using PowerShell

Chirag Nagrekar
Updated on 16-Oct-2020 09:20:11

3K+ Views

To retrieve files and folders attributes using PowerShell, you can use Get-Item or Get-ChildItem command. For example, We have a file called testfile.txt to get its attributes, PS C:\> Get-ChildItem C:\Temp\TestFile.txt |Select Name, Attributes Name Attributes ---- ---------- TestFile.txt ArchiveSo this file has the Archive attribute. To retrieve multiple files and folders' attributes, just refer to the folder name instead of the file name.Get-ChildItem C:\Temp -Recurse -Force | Select Name, FullName, Attributes Name ... Read More

Maximum Sum Bitonic Subarray in C++

Ayush Gupta
Updated on 15-Oct-2020 14:00:02

587 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the maximum sum bitonic subarray in C++.Bitonic Subarray is a special subarray in which the element strictly increase first and then strictly decreases after reaching a certain point.Let’s take an example to understand the problem, Inputarr[] = {4, 2, 3, 7 ,9, 6, 3, 5, 1}Output30ExplanationThe bitonic subarray is [2, 3, 7, 9, 6, 3]. Sum = 2 + 3 + 7 + 9 + 6 + 3 = 30Solution ApproachThe solution is similar to that in the bitonic subsequence problem. We ... Read More

Maximum Sum Bi-Tonic Sub-Sequence in C++

Ayush Gupta
Updated on 15-Oct-2020 13:56:52

200 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the maximum sum Bi-tonic subsequence in C++.Bi-tonic subsequence is a special sequence whose elements first increase and then decrease.Let’s take an example to understand the problem, Inputarr[] = {4, 2, 3, 7, 9, 6, 3, 5, 1}Output33ExplanationThe Bi-tonic subsequence which gives the largest sum is {2, 3, 7, 9, 6, 5, 1} Sum = 2 + 3 + 7 + 9 + 6 + 5 + 1 = 33Solution ApproachTo find the maximum sum bitonic subsequence, we will create two arrays, incSeq[] ... Read More

Maximum Sum from a Tree with Adjacent Levels Not Allowed in C++

Ayush Gupta
Updated on 15-Oct-2020 13:45:55

155 Views

In this problem, we are given a binary tree consisting of positive numbers. Our task is to create a program to find the Maximum sum from a tree with adjacent levels not allowed in C++.Code DescriptionHere, we will find the maximum sum of node of the tree in such a way that the sum does not contain nodes from two adjacent levels of the tree.Let’s take an example to understand the problem, Output21ExplanationTaking root as starting level, sum = 5 + 3 + 8 + 1 = 17 Taking sub-child of root as starting level, sum = 2 + 6 ... Read More

Maximum Sum from Three Arrays in C++

Ayush Gupta
Updated on 15-Oct-2020 12:34:25

340 Views

In this problem, we are given three arrays arr1[], arr2[], and arr3[] all of size N. Our task is to create a program to find the Maximum sum from three arrays such that picking elements consecutively from same is not allowed in C++.Problem DescriptionWe will find the maximum sum by choosing N elements. i=th element can be chosen of the sum from the i-th element of the array i.e. ith sum is from arr1[i]/ arr2[i]/ arr3[i]. Also, keep in mind that we cannot choose two consecutive elements that can be chosen from the same array.Let’s take an example to understand ... Read More

Advertisements