win10开机启动ps1脚本
生活随笔
收集整理的這篇文章主要介紹了
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" exitinit.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脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 13.SpringBoot学习(十三)—
- 下一篇: 计算机二级网页暂时无法连接,小编教你二级