日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

win10开机启动ps1脚本

發布時間:2023/12/18 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 win10开机启动ps1脚本 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

導語:需要通過管理員運行powershell,在里面輸入一些命令。打算通過開機啟動cmd腳本,通過cmd腳本 執行ps1腳本

問題:單獨執行腳本會報錯。

解決方式

需要在注冊表的如下位置HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run創建字符Shadow,值為cmd腳本的位置。

以下是cmd腳本鹿筋

C:\Users\Administrator\Desktop\Shadow.cmd

打開powershell,采用unrestricted形式(否則無法執行腳本)

@set Path=%Path%;%SystemRoot%\system32\WindowsPowerShell\v1.0\& powershell -ExecutionPolicy Unrestricted "C:\Users\Administrator\Desktop\init.ps1" exit

init.ps1腳本如下

# 獲取 Windows 和 WSL2 的 ip # $winip = bash.exe -c "ip route | grep default | awk '{print `$3}'" # default via 172.20.16.1 dev eth0 $winip = bash.exe -c "ip route | grep default |cut -d ' ' -f 3" # $wslip = bash.exe -c "hostname -I | awk '{print `$1}'" # 172.20.30.87 172.16.10.174 172.18.0.1 172.17.0.1 $wslip = bash.exe -c "hostname -I |cut -d ' ' -f 1 " $found1 = $winip -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'; $found2 = $wslip -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';if( !($found1 -and $found2) ){# 如果沒找到 wsl 的 ip, 就退出執行echo "The Script Exited, the ip address of WSL 2 cannot be found";exit; }# 你需要映射到局域網中端口 $ports=@(80,81,22222,22,800,11380,11381);# 監聽的 ip,這么寫是可以來自局域網 $addr='0.0.0.0'; # 監聽的端口,就是誰來訪問自己 $ports_a = $ports -join ",";# 移除舊的防火墻規則 iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' " | Out-Null# 允許防火墻規則通過這些端口 iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP" | Out-Null iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP" | Out-Null# 使用 portproxy 讓 Windows 轉發端口 # https://docs.microsoft.com/en-us/windows-server/networking/technologies/netsh/netsh-interface-portproxy for( $i = 0; $i -lt $ports.length; $i++ ){$port = $ports[$i];iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr" | Out-Nulliex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$wslip" | Out-Null }

添加開機啟動

搜索計算機管理 選擇任務計劃任務

以下面模版創建任務

缺點:每次開機會打開ps1這個腳本文件。容易被人手滑修改。

參考

https://www.programminghunter.com/article/19961163124/

https://zhuanlan.zhihu.com/p/20628937

https://blog.minifish.org/posts/powershell-starttup/

https://blog.csdn.net/Jmilk/article/details/50661936

總結

以上是生活随笔為你收集整理的win10开机启动ps1脚本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。