PowerShell Issues and Solutions
这篇文章是一个查找表。主要记录了我在使用 PowerShell 时遇到的问题与解决方案,或者使用 Windows 系统时遇到的问题以及使用 PowerShell 的解决方案。
A. Quick Switch between Linux Shell and Windows PowerShell¶
Linux Shell | Windows PowerShell | Note |
---|---|---|
\ |
``` | 单个命令分行 |
; |
; |
连接多个命令,后面的命令会被执行,无论前面的命令是否成功 |
&& |
if (command1) { command2 } |
连接多个命令,后面的命令会被执行,仅当前面的命令成功时 |
| |
| |
管道函数 |
> |
> |
输出重定向,覆盖 |
>> |
>> |
输出重定向,追加 |
--- | --- | --- |
ls |
ls or Get-ChildItem |
|
cd |
cd or Set-Location |
|
pwd |
pwd or Get-Location |
|
cp |
cp or Copy-Item |
|
mv |
mv or Move-Item |
|
rm |
rm or Remove-Item |
|
--- | --- | --- |
cat |
cat or Get-Content |
|
echo |
echo or Write-Output |
|
touch |
New-Item |
|
--- | --- | --- |
mkdir |
New-Item -ItemType Directory |
|
--- | --- | --- |
find |
Get-ChildItem -Recurse |
find 默认是递归的 |
grep |
Select-String |
|
--- | --- | --- |
ps |
ps or Get-Process |
|
kill |
kill or Stop-Process |
|
top |
||
--- | --- | --- |
ifconfig |
ipconfig or Get-NetIPAddress |
Powershell ipconfig 和 Get-NetIPAddress 并不完全等价 |
ping |
ping or Test-Connection |
Powershell ping 和 Test-Connection 并不完全等价 |
--- | --- | --- |
man |
Get-Help |
|
which |
Get-Command |
B. PowerShell Issues and Solutions¶
C. Windows Issues and Solutions with PowerShell¶
计算文件的哈希值¶
使用 certutil
命令计算文件的哈希值。可选的哈希函数有 MD2
,MD4
,MD5
,SHA1
,SHA256
,SHA384
,SHA512
。
PS > certutil -hashfile .\hello.exe SHA256
SHA256 的 .\hello.exe 哈希:
17b6ba1b851e565769ec917b497e60b825f9ee79acd72f3ffed5a66777c79b66
CertUtil: -hashfile 命令成功完成
检查已安装的 .NET SDK 与运行时¶
使用 dotnet
检查已安装的 .NET
SDK 与运行时。
查看与修改 Windows 的 TCP 协议排除端口范围¶
在 Windows 的终端中通过 netsh int ip show excludedportrange protocol=tcp
查询操作系统的 TCP 协议被排除端口范围。
通过 netsh int ip add excludedportrange protocol=tcp numberofports=1 startport=3121
为上面的范围添加排除项。
注意需要先停止 NAT 服务 net stop winnat
。
执行成功后可以看到在 netsh int ip show excludedportrange protocol=tcp
查询的返回结果中,3121
端口右侧出现了一个 *
:
PS > netsh int ip show excludedportrange protocol=tcp
协议 tcp 端口排除范围
开始端口 结束端口
---------- --------
3121 3121 *
5426 5426
50000 50059 *
* - 管理的端口排除.
在 Windows 系统中配置 SSH¶
生成密钥对¶
使用 ssh-keygen
生成 SSH 密钥对。会提示有三个输入项:
- Enter file in which to save the key :输入保存密钥的文件路径,注意路径以
C:\Users\username
为根目录。可以直接按回车缺省该选项,默认路径为C:\Users\username\.ssh\id_xxx
。 - Enter passphrase :输入密码,使用私钥进行连接时,需要输入该密码。可以直接按回车缺省该选项,不设置密码。
- Enter same passphrase again :再次输入密码。
可以使用 -t
参数指定密钥类型;-C
参数指定注释,通常是邮箱地址。
使用私钥访问远程服务器¶
将生成的 id_xxx.pub
文件添加到远程服务器。
使用 ssh -i priv_key -p port username@host
连接远程服务器。其中 -i
参数指定私钥文件,-p
参数指定端口,username@host
指定用户名和主机地址。
如果不想每次都手动输入私钥文件路径和服务器名,可以在 C:\Users\username\.ssh\config
文件中添加配置:
然后使用 ssh server-alias
即可连接到远程服务器。这个方法可以和 VSCode 的 Remote SSH 插件配合使用,十分方便。
也可以使用 ssh-add
将私钥添加到 ssh-agent
服务中,但是需要额外安装 OpenSSH.Server
:
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 # 安装 OpenSSH 服务,然后手动重新启动计算机
Start-Service ssh-agent # 启动 ssh-agent 服务
ssh-add C:\Users\username\.ssh\id_xxx # 添加私钥
D. Documents¶
- 使用 Powershell 管理 Windows功能,官方文档:https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.management/?view=powershell-7.5