Get Website Links Using Invoke-WebRequest in PowerShell

Chirag Nagrekar
Updated on 18-Jan-2021 07:29:34

5K+ Views

To get the links present on the website using PowerShell, we can first retrieve the data from the webpage using the Invoke-WebRequest cmdlet.$req = Invoke-WebRequest -uri "https://theautomationcode.com" $reqOutputTo retrieve only links we can use that property and there you will also find some sub-properties like InnerHTML, Innertext, href, etc as shown in the output.$req = Invoke-WebRequest -uri "https://theautomationcode.com" $req.LinksOutputinnerHTML : Scripts innerText : Scripts outerHTML : Scripts outerText : Scripts tagName : A href : https://theautomationcode.com/scripts/ We need only links so we will use the href property.$req.Links | Select -ExpandProperty hrefOutputhttps://theautomationcode.com/2020/11/ https://theautomationcode.com/author/chiragce17/ ... Read More

Download Images Using Invoke-WebRequest in PowerShell

Chirag Nagrekar
Updated on 18-Jan-2021 07:26:10

3K+ Views

To download the images from the webpage using the Invoke-WebRequest command, we can use the images property from the result to retrieve the images URL, and later we can use the method to download them at the specific location. Consider we have the URI: https://theautomationcode.com to retrieve the images.Once you run the below command, you can see the Images property there.Invoke-WebRequest -Uri "https://theautomationcode.com/feed/"To retrieves the images URL, $req = Invoke-WebRequest -Uri "https://theautomationcode.com/feed/" $req.Images | Select -ExpandProperty srcOutputhttps://i1.wp.com/theautomationcode.com/wp-content/uploads/2020/11/image-9.png?resize=178%2C60&ssl=1 https://i0.wp.com/theautomationcode.com/wp-content/uploads/2020/11/image-10.png?resize=640%2C68&ssl=1All the above URLs point to the images, so we can download that.$wc = New-Object System.Net.WebClient $req = Invoke-WebRequest -Uri "https://theautomationcode.com/feed/" $images = ... Read More

Use Array Splatting in PowerShell

Chirag Nagrekar
Updated on 18-Jan-2021 07:23:45

753 Views

Splatting is the method to pass the collection of parameters as a single unit so it will be easier for the command to read. Array splatting uses the splat values which do not require parameter names. The values must be in positional number order in the array.We have a below copy example, in which we are copying one file from the source to the destination. Now we are not specifying the parameters here because we will use the positional parameter for the source path and the destination path.If we check the help for those parameters, we will come to know ... Read More

Use Hashtable Splatting in PowerShell

Chirag Nagrekar
Updated on 18-Jan-2021 07:21:18

797 Views

Splatting is the way to pass the collection of the parameters to the command as a single value. It uses the Hashtable splatting means that we can pass the Name and Value pair combination. We can use the named positional parameter for this with the values we want to provide.For example, First, we will check how we run the Copy-Item command here without splatting, $params = @{    Path = 'C:\Temp\25Aug2020.txt'    Destination = 'C:\test1'    Verbose = $true    Force = $true } Copy-Item @paramsAnother Example, $hash = @{    From = 'harris@Microsoftmail.com'    To = 'Jacob@MicrosoftMail.com'    SMTP ... Read More

What is Splatting in PowerShell

Chirag Nagrekar
Updated on 18-Jan-2021 07:20:14

371 Views

PowerShell splatting is a method to pass the collection of the parameters as a single command unit which makes the command shorter and easier for the user to read commands. Splatting uses the symbol (@) instead of ($) which tells the user that splatting is used and PowerShell is passing a set of values instead of a single value.Splatting in PowerShell was included from the v3.0 onwards and you can pass all parameters in the command.For example, $params = @{    Path = 'C:\Temp\25Aug2020.txt'    Destination = 'C:\test1'    Verbose = $true    Force = $true } Copy-Item @paramsThe splatting ... Read More

Remove Smallest Subarray to Make Array Sum Divisible in JavaScript

AmitDiwan
Updated on 18-Jan-2021 05:14:33

133 Views

We are required to write a JavaScript function that takes in an array of positive integers as the first argument and a positive integer as the second argument.The function should figure out and return the length of the smallest subarray that we should delete from the original array in order to make its sum divisible by the number specified by the second argument.For example −If the input is −const arr = [3, 8, 2, 6]; const num = 9;Then the output should be −const output = 2Because the subarray that needs to be deleted is [8, 2]ExampleFollowing is the code ... Read More

Matching Odd and Even Indices with Values in JavaScript

AmitDiwan
Updated on 18-Jan-2021 05:12:03

219 Views

We are required to write a JavaScript function that takes in an array of numbers. The array given as an input to the function have two special properties −The length of the array will always be an even number.The number of even numbers and the number of odd numbers in the array will always be equal (i.e., both being equal to the half of the length of array)The function should shuffle the elements of the array such that all the even values occupy even indices and all the odd values occupy odd indices.Note that there may be more than one ... Read More

Format String to Separate Identical Characters in JavaScript

AmitDiwan
Updated on 18-Jan-2021 05:08:54

132 Views

We are required to write a JavaScript function that takes in a character string as the first and the only argument.The function should try and re-organize the characters present in the string such that no two identical characters are placed adjacent to each other.If there exists at least one such combination then our function should return that combination string otherwise our function should return an empty string.For example −If the input string is −const str = 'add';Then our function can output −const output = 'dad';ExampleFollowing is the code −const str = 'add'; const formatString = (str = '') => { ... Read More

Finding Intersection of Arrays with Repetitive Entries in JavaScript

AmitDiwan
Updated on 18-Jan-2021 05:06:49

128 Views

We are required to write a JavaScript function that takes in two arrays of numbers, lets call them arr1 and arr2.The function should build a third array based on the two input arrays that contains all the elements that are common to both arr1 and arr2.Note that if there are more than one same element that are present in both the arrays then we have to consider all such instances of that element.For example −If the input arrays are −const arr1 = [1, 2, 2, 4, 4, 5, 6]; const arr2 = [3, 2, 4, 2, 4, 9];Then the output ... Read More

Alternative Sorting of an Array in JavaScript

AmitDiwan
Updated on 18-Jan-2021 05:04:45

621 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first and the only argument.The job of our function is to sort the elements present in the array in an alternate fashion.By alternate we mean the following −Suppose we have an array arr that contains only four elements for the time being. Then our function should shuffle the elements of the array such that −arr[0] < arr[1] > arr[2] < arr[3]Note that there can be more than one possible solution for a given array, we are just required to return any of the ... Read More

Advertisements