Powershell 3 Cmdlets Hackerrank Solution

$result = $data | Where-Object $_ -gt 0 | Measure-Object | Select-Object -ExpandProperty Count

Here's a PowerShell function that solves the problem: powershell 3 cmdlets hackerrank solution

function Execute-Cmdlet 
    param (
        [string]$cmdlet,
        [string]$argument
    )
switch ($cmdlet) 
        "Get-ChildItem" 
            if ($argument) 
                Get-ChildItem -Path $argument
             else 
                Get-ChildItem
"Get-Process" 
            if ($argument) 
                Get-Process -Name $argument
             else 
                Get-Process
"Get-Service" 
            if ($argument) 
                Get-Service -Name $argument
             else 
                Get-Service
default 
            Write-Host "Invalid cmdlet"
$lines = @($input)
$n = [int]$lines[0]
$heights = $lines[1].Trim() -split ' ' | ForEach-Object  [int]$_

$max = ($heights | Measure-Object -Maximum).Maximum ($heights | Where-Object $_ -eq $max ).Count | Write-Output $result = $data | Where-Object $_ -gt 0

Chained cmdlets: Measure-ObjectWhere-Object → implicit output. $lines = @($input) $n = [int]$lines[0] $heights = $lines[1]


$sum = 0
$arr | ForEach-Object  $sum += $_ 

PowerShell 3 allows simplified command aliases for shorter code:

ps |
    where WorkingSet -gt 50MB |
    select ProcessName, Id, @N="WorkingSet_MB"; E=[math]::Round($_.WorkingSet/1MB,2) |
    sort WorkingSet_MB -Desc |
    select -First 5 |
    ft -Auto

Discover more from SharePains

Subscribe now to keep reading and get access to the full archive.

Continue reading