PowerShell 技能连载 - PowerShell 速查表汇编(第 2 部分)

在前一个技能中我们提供了一个很棒的 PowerShell 速查表。让我们来看看 PowerShell 能够怎样下载这些速查表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# enable SSL download
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols

# download page
$url = "https://github.com/PrateekKumarSingh/CheatSheets/tree/master/Powershell"
$page = Invoke-WebRequest -UseBasicParsing -Uri $url
$links = $page.Links |
Where-Object { $_.href -like '*.pdf' } |
Select-Object -Property title, href |
# turn URLs into directly downloadable absolute URLs
ForEach-Object {
$_.href = 'https://github.com/PrateekKumarSingh/CheatSheets/raw/master/Powershell/' + $_.title
$_
}

# create folder on your desktop
$Path = "$home\Desktop\CheatSheets"
$exists = Test-Path -Path $Path
if (!$exists) { $null = New-Item -Path $path -ItemType Directory }

# download cheat sheets
$links | ForEach-Object {
$docPath = Join-Path -Path $Path -ChildPath $_.Title
Start-BitsTransfer -Source $_.href -Destination $docPath -Description $_.title
# alternate way of downloading
# Invoke-WebRequest -UseBasicParsing -Uri $_.href -OutFile $docPath
}

# open folder
explorer $Path

当您运行这段脚本时,PowerShell 将下载所有的速查表并且将它们存放在桌面上一个名为 “CheatSheets” 的新文件夹中。祝您读得愉快!

评论