Skip to content

Instantly share code, notes, and snippets.

@gsw945
Created July 4, 2023 03:25
Show Gist options
  • Save gsw945/e7f79482de8321b58d6c67825ac41b52 to your computer and use it in GitHub Desktop.
Save gsw945/e7f79482de8321b58d6c67825ac41b52 to your computer and use it in GitHub Desktop.
PowerShell code snippets

dotnet 全局 tool 全部更新

foreach ($package in $(dotnet tool list --global | Select-Object -Skip 2)) {
    Write-Host "dotnet tool update --global $($package.Split(" ", 2)[0])"
    dotnet tool update --global $($package.Split(" ", 2)[0])
}

文件下载

Add-Type @"
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

public static class Downloader {
    public static async Task DownloadAsync(string url)
    {
        url = WebUtility.HtmlDecode(url);
        var filename = url.Split('/').Last();
        if (File.Exists(filename))
        {
            await Task.CompletedTask;
            return;
        }
        using var handler = new HttpClientHandler()
        {
            Proxy = new WebProxy(new Uri(`"socks5://127.0.0.1:7890`")),
        };
        using var httpClient = new HttpClient(handler);
        using (var s = await httpClient.GetStreamAsync(new Uri(url)))
        {
            using (var fs = new FileStream(filename, FileMode.CreateNew))
            {
                await s.CopyToAsync(fs);
            }
        }
    }

    public static void DownloadFile(string url)
    {
        Task.WaitAll(DownloadAsync(url));
    }
}
"@

$files = @("https://fonts.gstatic.com/s/roboto/v30/KFOlCnqEu92Fr1MmSU5fCRc4EsA.woff2",
    "https://fonts.gstatic.com/s/roboto/v30/KFOlCnqEu92Fr1MmSU5fABc4EsA.woff2",
    "https://fonts.gstatic.com/s/roboto/v30/KFOlCnqEu92Fr1MmWUlfBBc4.woff2")

$files | ForEach-Object {
    [Downloader]::DownloadFile($_)
}

默认编码

Write-Host ([System.Text.Encoding]::GetEncoding(0).CodePage)
Write-Host ([System.Text.Encoding]::GetEncoding(0).EncodingName)

颜色

Add-Type -TypeDefinition @"
using System.IO;
using System;

public class Program
{
   public static void Main()
   {
      // Get an array with the values of ConsoleColor enumeration members.
      ConsoleColor[] colors = (ConsoleColor[]) ConsoleColor.GetValues(typeof(ConsoleColor));
      // Save the current background and foreground colors.
      ConsoleColor currentBackground = Console.BackgroundColor;
      ConsoleColor currentForeground = Console.ForegroundColor;

      // Display all foreground colors except the one that matches the background.
      Console.WriteLine("All the foreground colors except {0}, the background color:",
                        currentBackground);
      foreach (var color in colors) {
         if (color == currentBackground) continue;

         Console.ForegroundColor = color;
         Console.WriteLine("   The foreground color is {0}.", color);
      }
      Console.WriteLine();
      // Restore the foreground color.
      Console.ForegroundColor = currentForeground;

      // Display each background color except the one that matches the current foreground color.
      Console.WriteLine("All the background colors except {0}, the foreground color:",
                        currentForeground);
      foreach (var color in colors) {
         if (color == currentForeground) continue;

         Console.BackgroundColor = color;
         Console.WriteLine("   The background color is {0}.", color);
      }

      // Restore the original console colors.
      Console.ResetColor();
      Console.WriteLine("\nOriginal colors restored...");
   }
}
"@

[Program]::Main()

Write-Host [System.ConsoleColor]::DarkRed

Git Diff

$items = git diff --name-only --cached | Where-Object -FilterScript {
    ($_ -Match '\.fbx$') -OR ($_ -Match '\.fbx.meta$')
}
$items | ForEach-Object {
    $Pattern = '(?<suffix>\.fbx(\.meta)?)$'
    if (-NOT ($_ -CMatch $Pattern)) {
        Write-Host $Matches
        Write-Host "suffix: $($Matches['suffix'])"
    } else {
        Write-Host $_
    }
}

判断命令是否存在

$rclone = (Get-Command -ErrorAction Ignore -Type Application -TotalCount 1 -Name "rclone").Path
Write-Host ($null -eq $clone)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment