PowerShell 技能连载 - 通过 SSL 和 BitsTransfer
有一个很实用的内置方法可以下载文件,甚至支持 SSL 连接。该方法是 Start-BitsTransfer
。它也会显示一个进度条,指示实际的下载状态。以下是一个可用的例子:
1 | $url = 'https://mars.nasa.gov/system/downloadable_items/41764_20180703_marsreport-1920.mp4' |
有一个很实用的内置方法可以下载文件,甚至支持 SSL 连接。该方法是 Start-BitsTransfer
。它也会显示一个进度条,指示实际的下载状态。以下是一个可用的例子:
1 | $url = 'https://mars.nasa.gov/system/downloadable_items/41764_20180703_marsreport-1920.mp4' |
Invoke-WebRequest
可以下载文件,但是对 HTTPS URL 可能会遇到麻烦。要使用 SSL 连接,您可能需要改变缺省的设置。以下是一个可用的示例:
1 | $url = 'https://mars.nasa.gov/system/downloadable_items/41764_20180703_marsreport-1920.mp4' |
以下是一行可以转储所有禁用了所有设置的组策略对象的代码:
1 | Get-Gpo -All | Where-Object GpoStatus -eq AllSettingsDisabled |
这个示例需要 Microsoft 免费的 RSAT 工具。
Get-EventLog
总是需要您通过 LogName
明确地指定一个事件日志。您无法使用通配符,并且无法一次性浏览所有事件日志。
然而,可以使用这个技巧:
1 | PS> Get-EventLog -LogName * |
所以显然,-LogName
终究不支持通配符。然而,您现在看到的不再是事件日志项,而是一个摘要视图。不过您仍然可以访问以下的事件日志条目:
1 | PS> Get-EventLog -LogName * | Select-Object -ExpandProperty Entries -ErrorAction SilentlyContinue |
这将从所有的日志中转储所有事件日志条目。在这儿,您可以添加自定义过滤器。要查看近 48 小时所有事件日志错误,请试试这段代码:
1 | # take events not older than 48 hours |
您可能经常使用 Get-EventLog
来转储事件日志信息,例如:
1 | PS> Get-EventLog -LogName System -EntryType Error -Newest 6 |
但是,如果您想创建有用的报告,请确保将输出表格格式化,并启用换行:
1 | PS> Get-EventLog -LogName System -EntryType Error -Newest 6 | Format-Table -AutoSize -Wrap |
您现在可以方便地将结果输送到 Out-File
并创建有意义的文本报告。同时,设置其 Width
参数,以调整报告文件的宽度。
如果您不知道某个日志的确切名字,只需要将 "*"
赋给 -LogName
:
1 | PS> Get-EventLog -LogName * |
PowerShell 控制台从 5.0 版开始发布了一个名为 PSReadLine
的模块,它不仅可以对命令做语法着色,还有更多的功能。它包含持久化的命令历史,并且可以将自定义命令绑定到键盘快捷方式上。
请看这个示例:
1 | Set-PSReadlineKeyHandler -Chord Ctrl+H -ScriptBlock { |
当您在 PowerShell 控制台中运行这段代码(它不能在 PowerShell ISE 中运行!),按下 CTRL
+ H
打开一个网格视图窗口,这个窗口中列出了所有命令行历史。您可以轻松地选择一个命令并执行它。
显然,这不仅是一个示例。您可以将任何脚本块绑定到未使用的键盘快捷方式,例如提交变更到 Git,或是打开喜爱的滚动新闻条。
PowerShell 6 (PowerShell Core) 终于支持 SSH 了:您可以使用 SSH 来连接非 Windows 机器来进行 PowerShell 远程操作。
如果只是需要用 SSH 连接到交换机或者其它设备,那么可以使用免费的模块。该模块为所有 PowerShell 添加了大量有用的新的 SSH 命令。以下是如何下载和安装该模块的方法:
1 | Install-Module -Name posh-ssh -Repository PSGallery -Scope CurrentUser |
要列出所有新的命令,请运行以下代码:
1 | PS C:\> (Get-Command -Module posh-ssh).Name |
在前一个技能中我们演示了如何读取和改变 Lenovo 计算机的 BIOS 设置。例如,以下代码禁止 WakeOnLan:
1 | #requires -RunAsAdministrator |
如果某个 BIOS 设置是被密码保护的,以下代码演示如何更改一个受 BIOS 密码保护的设置:
1 | #requires -RunAsAdministrator |
请注意该密码仅在该设置项受 BIOS 密码保护的情况下生效。如果实际中没有密码而您输入了密码,它并不会被验证,而且改动会生效。
在前一个技能中我们介绍了如何在 PowerShell 中管理 Lenovo BIOS。通常,只需要管理单个设置。请注意某些操作需要管理员特权。
以下是转储所有可用设置名称的代码。请注意这些名字是大小写敏感的:
1 | $currentSetting = Get-WmiObject -Class Lenovo_BiosSetting -Namespace root\wmi |
一旦您知道了想要操作的设置项的名称,就可以用这段代码来读取设置:
1 | $Settingname = "WakeOnLAN" |
以下代码转储某个指定设置的所有合法值:
1 | #requires -RunAsAdministrator |
以下是如何将一个设置改为一个新的值(例如,禁止 WakeOnLan):
1 | #requires -RunAsAdministrator |
在前一个技能中,我们解释了如何转储 Lenovo 计算机的 BIOS 设置。要调整设置,您需要了解某个设置支持的各种选项。以下是一段转储某个(Lenovo 电脑的)BIOS 设置的所有可选项的代码:
1 | #requires -RunAsAdministrator |
请注意这段代码需要管理员特权。并且该设置名称是大小写敏感的。结果类似这样:
Disable
ACOnly
ACandBattery
Enable
这可能是一个显示如何获取当前 BIOS 设置,以及合法设置的列表的复杂示例:
1 | #requires -RunAsAdministrator |
结果类似如下:
CurrentSetting Status Active AvailableSettings
-------------- ------ ------ -----------------
WakeOnLAN ACOnly True {Disable, ACOnly, ACandBattery,...
WakeOnLANDock Enable True {Disable, Enable}
EthernetLANOptionROM Enable True {Disable, Enable}
IPv4NetworkStack Enable True {Disable, Enable}
IPv6NetworkStack Enable True {Disable, Enable}
UefiPxeBootPriority IPv4First True {IPv6First, IPv4First}
WiGigWake Disable True {Disable, Enable}
WirelessAutoDisconnection Disable True {Disable, Enable}
MACAddressPassThrough Disable True {Disable, Enable}
USBBIOSSupport Disable True {Disable, Enable}
AlwaysOnUSB Enable True {Disable, Enable}
TrackPoint Automatic True {Disable, Automatic}
TouchPad Automatic True {Disable, Automatic}
FnCtrlKeySwap Disable True {Disable, Enable}
FnSticky Disable True {Disable, Enable}
FnKeyAsPrimary Disable True {Disable, Enable}
BootDisplayDevice LCD True {LCD, USBTypeC, HDMI, DockDisplay}
SharedDisplayPriority DockDisplay True {HDMI, DockDisplay}
TotalGraphicsMemory 256MB True {256MB, 512MB}
BootTimeExtension Disable True {Disable, 1, 2, 3...}
SpeedStep Enable True {Disable, Enable}
AdaptiveThermalManagementAC MaximizePerformance True {MaximizePerformance, Balanced}
AdaptiveThermalManagementBattery Balanced True {MaximizePerformance, Balanced}
CPUPowerManagement Automatic True {Disable, Automatic}
OnByAcAttach Disable True {Disable, Enable}
PasswordBeep Disable True {Disable, Enable}
KeyboardBeep Enable True {Disable, Enable}
AMTControl Enable True {Disable, Enable, Disable}
USBKeyProvisioning Disable True {Disable, Enable}
WakeByThunderbolt Enable True {Disable, Enable}
ThunderboltSecurityLevel UserAuthorization True {NoSecurity, UserAuthorization,...
PreBootForThunderboltDevice Disable True {Disable, Enable, Pre-BootACL}
PreBootForThunderboltUSBDevice Disable True {Disable, Enable}
LockBIOSSetting Disable True {Disable, Enable}
MinimumPasswordLength Disable True {Disable, 4, 5, 6...}
BIOSPasswordAtUnattendedBoot Enable True {Disable, Enable}
BIOSPasswordAtReboot Disable True {Disable, Enable}
BIOSPasswordAtBootDeviceList Disable True {Disable, Enable}
PasswordCountExceededError Enable True {Disable, Enable}
FingerprintPredesktopAuthentication Enable True {Disable, Enable}
FingerprintReaderPriority External True {External, InternalOnly}
FingerprintSecurityMode Normal True {Normal, High}
FingerprintPasswordAuthentication Enable True {Disable, Enable}
SecurityChip Enable True {Active, Inactive, Disable, Ena...
TXTFeature Disable True {Disable, Enable}
PhysicalPresenceForTpmProvision Disable True {Disable, Enable}
PhysicalPresenceForTpmClear Enable True {Disable, Enable}
BIOSUpdateByEndUsers Enable True {Disable, Enable}
SecureRollBackPrevention Enable True {Disable, Enable}
WindowsUEFIFirmwareUpdate Enable True {Disable, Enable}
DataExecutionPrevention Enable True {Disable, Enable}
VirtualizationTechnology Enable True {Disable, Enable}
VTdFeature Enable True {Disable, Enable}
EthernetLANAccess Enable True {Disable, Enable}
WirelessLANAccess Enable True {Disable, Enable}
WirelessWANAccess Enable True {Disable, Enable}
BluetoothAccess Enable True {Disable, Enable}
USBPortAccess Enable True {Disable, Enable}
MemoryCardSlotAccess Enable True {Disable, Enable}
SmartCardSlotAccess Enable True {Disable, Enable}
IntegratedCameraAccess Enable True {Disable, Enable}
MicrophoneAccess Enable True {Disable, Enable}
FingerprintReaderAccess Enable True {Disable, Enable}
ThunderboltAccess Enable True {Disable, Enable}
NfcAccess Enable True {Disable, Enable}
WiGig Enable True {Disable, Enable}
BottomCoverTamperDetected Disable True {Disable, Enable}
InternalStorageTamper Disable True {Disable, Enable}
ComputraceModuleActivation Enable True {Disable, Enable, Disable}
SecureBoot Disable True {Disable, Enable}
SGXControl SoftwareControl True {Disable, Enable, SoftwareControl}
DeviceGuard Disable True {Disable, Enable}
BootMode Quick True {Quick, Diagnostics}
StartupOptionKeys Enable True {Disable, Enable}
BootDeviceListF12Option Enable True {Disable, Enable}
BootOrder USBCD:USBFDD:NVMe0:HDD0:USBHDD:PCILAN True {HDD0, HDD1, HDD2, HDD3...}
NetworkBoot USBFDD True {HDD0, HDD1, HDD2, HDD3...}
BootOrderLock Disable True {Disable, Enable}