文章目录
powershell一键启动
一键启动脚本内容(powershell,兼容windows powershell和powershell7+)
irm https://gitee.com/xuchaoxin1375/scripts/raw/main/PS/Tools/Tools.psm1|iex
sleep 0.5
help Start-HTTPServer
#可以指定参数选择服务器根目录和端口
start-httpServer
一键启动依赖于互联网,会从gitee下载代码运行
如果要离线运行,参考最后一节,或者部署上述脚本仓库scripts: 实用脚本集合,以powershell模块为主(针对powershell 7开发) 支持一键部署,改善windows下的shell实用体验
如果部署相关仓库pwsh模块,可以使用Start-HttpserverBG
来启动一个后台httpServer进程
可以配合一下方法查询相关进程
检查相关进程
@("*pwsh*","*powershell*")|%{
ps -Name $_ } |?{
$_.CommandLine -like '*httpServer*'}
例如:
@("*pwsh*","*powershell*")|%{
ps -Name $_ } |?{
$_.CommandLine -like '*httpServer*'}
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName
------ ----- ----- ------ -- -- -----------
84 45.52 116.32 1.91 12084 2 pwsh
杀死查询结果
ps -Id 12084 |kill
源码
可以跳过不看,如果源码不可用,则可以将下面代码拷贝到powershell的配置文件或者自动导入模块中,然后用powershell命令行收入Start-HttpServer
也能离线(不联网)启动本地服务器
更直接的用法是将内容复制保存到一个.ps1
脚本文件中,然后通过右键,选择以powershell方式运行(建议用pwsh而非自带的powershell.exe)即可启动服务
windows powershell.exe对于中文(utf8)编码的脚本可能会乱码报错,因此下面的代码纯英文
# If you need to set the service start path, modify the parameters on the last line.
# By default, the desktop is set as the server directory, and the port is 8080.
function Format-FileSize
{
param([long]$Size)
if ($Size -gt 1GB) {
return "{0:N2} GB" -f