--- title: PowerShell date: 2023-10-07 11:25:08 tags: --- # 文件 ## 新建文件 ```powershell New-Item <文件名>.<扩展名后缀> New-Item my.ini ``` ## 删除文件 ```powershell Remove-Item <文件名>.<扩展名后缀> Remove-Item my.ini ``` ## 对文件添加内容 ```powershell Set-Content <文件名>.<扩展名后缀> -value "<内容>" Set-Content my.ini -value "" ``` ## 新建文件夹 ```powershell New-Item data -ItemType Directory ``` # 做 sudo 命令 在Windows系统上sudo对应的就是管理员权限了。 一般使用Powershell时,并不会管理员启动,当执行需要权限的命令(比如net start mysql),就需要以管理员打开新的窗口。 为了一步到位,这里给powershell创建一个`alias` -> `sudo` 来运行需要管理员权限的命令。 在文档目录中(在`powershell`执行`$profile`即可输出此文件路径),新建文件夹`WindowsPowerShell`,新建文件`Microsoft.PowerShell_profile.ps1`。 > 此文件是在启动Powershell时执行的脚本。set-alias 在退出后就会失效,所以放到启动脚本中。 追加如下代码,**然后重启Powershell窗口。** ```text function _sudo { $ss = "$args ; pause" Start-Process powershell -Verb runAs -ArgumentList $ss } set-alias -name sudo -value _sudo ``` 保存后发现无法加载,因为默认不加载外部脚本,管理员权限下 powershell 运行: ```text set-ExecutionPolicy RemoteSigned ``` # 实用脚本 ## 自动延长VS社区版过期时间 > 该脚本作用为 自动检查VS社区版是否在一天内过期,如是则自动延长30天 > > 需事先从Github上Clone开源项目[beatcracker/VSCELicense: PowerShell module to get and set Visual Studio Community Edition license expiration date in registry (github.com)](https://github.com/beatcracker/VSCELicense) > > 然后放置在指定目录,并解除脚本执行限制,以管理员身份执行如下命令: > > ```powershell > Set-ExecutionPolicy RemoteSigned > ``` > > 输入 A 即可 ```powershell # 加载 VSCELicense 模块 try { Import-Module -Name 'C:\VSCELicense\VSCELicense.psd1' -ErrorAction Stop } catch { Write-Error "无法加载模块 'C:\VSCELicense\VSCELicense.psd1',请确认路径是否正确。错误信息: $_" exit 1 } # 获取许可证信息 try { $output = Get-VSCELicenseExpirationDate -ErrorAction Stop | Out-String } catch { Write-Error "执行 Get-VSCELicenseExpirationDate 时出错: $_" exit 1 } # 使用正则表达式提取版本号和过期日期 $version = $null $expDateStr = $null if ($output -match '(?m)^\s*(\d{4})\s+(\d{2}/\d{2}/\d{4}\s+\d{2}:\d{2}:\d{2})') { $version = $matches[1].Trim() $expDateStr = $matches[2].Trim() Write-Host "找到版本: $version" Write-Host "过期日期: $expDateStr" } else { Write-Error "无法从输出中找到版本和过期日期" exit 1 } # 将过期日期字符串解析为 DateTime 对象 try { $expirationDate = [datetime]::ParseExact($expDateStr, 'dd/MM/yyyy HH:mm:ss', $null) } catch { Write-Error "日期格式解析失败: $expDateStr. 错误: $_" exit 1 } # 获取当前时间并设置阈值 $currentTime = Get-Date $threshold = $currentTime.AddDays(1) Write-Host "当前时间: $currentTime" Write-Host "过期时间: $expirationDate" Write-Host "阈值时间(当前时间 + 1天): $threshold" # 判断是否在1天内过期 -le 表示小于等于 想起了 Mybatis-Plus 条件构造器中的 le if ($expirationDate -le $threshold) { Write-Host "许可证即将过期,正在执行 Set-VSCELicenseExpirationDate 命令..." try { Set-VSCELicenseExpirationDate -Version $version -ErrorAction Stop Write-Host "Set-VSCELicenseExpirationDate 命令执行成功。" } catch { Write-Error "执行 Set-VSCELicenseExpirationDate 时出错: $_" exit 1 } } else { Write-Host "许可证未在一天内过期,无需操作。" } ``` 输出如下: ![image-20250718112238088](D:\source\repos\XiaodaBlogSource\source\_posts\PowerShell\image-20250718112238088.png) 可结合任务计划程序进行使用,免去手动执行的烦恼 ![image-20250718142611106](D:\source\repos\XiaodaBlogSource\source\_posts\PowerShell\image-20250718142611106.png) ### 🧩 步骤 1:打开任务计划程序 1. 按下 `Win + R`,输入 `taskschd.msc`,回车。 2. 在左侧的“任务计划程序库”中,右键选择“创建任务”。 ### 🧩 步骤 2:设置任务基本信息 - 常规 选项卡: - 名称:例如 `Check-VS-LicenseExpiration` - 描述(可选):每天早上 7 点检查许可证过期状态 - 勾选“不管用户是否登录都要运行” - 勾选“使用最高权限” ![image-20250718142545538](D:\source\repos\XiaodaBlogSource\source\_posts\PowerShell\image-20250718142545538.png) ### 🧩 步骤 3:设置触发器 1. 切换到 **触发器** 选项卡。 2. 点击 **新建** 。 3. 设置: - 开始时间:`07:00:00` - 每天 - 勾选“启用此触发器” 4. 点击 **确定** 。 ![image-20250718142556458](D:\source\repos\XiaodaBlogSource\source\_posts\PowerShell\image-20250718142556458.png) ### 🧩 步骤 4:设置操作 1. 切换到 **操作** 选项卡。 2. 点击 **新建** 。 3. 设置: - 操作:`启动程序` - 程序/脚本:`powershell.exe` - 参数(可选):`-ExecutionPolicy RemoteSigned -File "C:\VSCELicense\Check-VS-LicenseExpiration.ps1` - 起始于(可选):`C:\VSCELicense`(确保路径正确) > ✅ **说明** : > > - `-ExecutionPolicy RemoteSigned`:允许本地脚本运行,避免执行策略限制。 > - `"C:\VSCELicense\Check-VS-LicenseExpiration.ps1"`:替换为你实际的脚本路径。 > - 如果脚本路径包含空格,请用双引号包裹。 ### 🧩 步骤 5:设置条件(可选) 1. 切换到 **条件** 选项卡。 2. 可以取消勾选“只有在计算机使用交流电源时才启动此任务”,确保任务在任何电源模式下都能运行。 ### 🧩 步骤 6:设置设置(可选) 1. 切换到 **设置** 选项卡。 2. 勾选“如果任务失败,按以下频率重新启动”并设置时间间隔(如每 1 分钟)。 3. 勾选“如果任务运行时间超过以下时间,则停止任务”并设置合理时间(如 1 小时)。 ### 🧩 步骤 7:保存任务 1. 点击 **确定** 。 2. 如果弹出用户账户控制(UAC)提示,输入管理员凭据确认。