Skip to content

Instantly share code, notes, and snippets.

@DilanLivera
Last active August 24, 2023 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DilanLivera/adf1826009daf387d72d7e3c609e4f69 to your computer and use it in GitHub Desktop.
Save DilanLivera/adf1826009daf387d72d7e3c609e4f69 to your computer and use it in GitHub Desktop.
PowerShell scripts

Health status check

The following is a PowerShell script to call list of services and display the results in a table.

function Test-ServiceHealth {
    param (
        [Parameter(Mandatory=$true)]
        [System.Collections.ArrayList]$services
    )

    $results = @()

    foreach ($service in $services) {
        try {
            $response = Invoke-WebRequest $service.Url
            $status = "OK"
        }
        catch {
            $response = $_.Exception.Response
            $status = "ERROR"
        }

        $results += [PSCustomObject]@{
            Name = $service.Name
            Url = $service.Url
            Status = $status
            StatusCode = $response.StatusCode
            ResponseTime = $response.Headers."Request-Processing-Time"
        }
    }

    $results | Format-Table -AutoSize
}

$services = @(
    @{Name="Service1"; Url="http://localhost/Service1/health"},
    @{Name="Service2"; Url="http://localhost/Service2/health"},
    @{Name="Service3"; Url="http://localhost/Service3/health"}
)

Test-ServiceHealth -services $services

Retrieve the license information from .NET packages

PowerShell script to retrieve the license information from .NET packages.

# This script requires the dotnet-project-licenses tool to be installed
# For installation instructions refer to https://github.com/tomchavakis/nuget-license

# Update the following paths to refer to the locations of the solutions that you want to extract package license info for
$solutionFilesRootFolder = "C:\repos"
$solutionFilePaths = Get-ChildItem -Path $solutionFilesRootFolder -Recurse -Filter "*.sln" | Select-Object -ExpandProperty FullName

$allPackages = @()

foreach ($solutionFilePath in $solutionFilePaths) 
{
    Write-Output "Processing ${solutionFilePath}"
    dotnet-project-licenses -u -j -i $solutionFilePath | Out-Null 
    $packages = Get-Content "licenses.json" -Raw | ConvertFrom-Json

    foreach ($package in $packages) { $allPackages += $package }
}

$allUniquePackages = $allPackages | 
    Group-Object 'PackageName','PackageVersion', 'PackageUrl', 'LicenseType', 'LicenseUrl' | 
    ForEach-Object { $_.Group | Select-Object 'PackageName','PackageVersion', 'PackageUrl', 'LicenseType', 'LicenseUrl' -First 1 } | 
    Sort-Object 'PackageName','PackageVersion', 'PackageUrl', 'LicenseType', 'LicenseUrl'

# $allUniquePackages | Format-Table

[pscustomobject]@{ Packages = $allUniquePackages } |
    ConvertTo-Json |
    Set-Content -Path "C:\temp\package_version_data.json"

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment