如何编写一个 PowerShell 脚本
PowerShell 脚本的后缀是 .ps1
前提:
ps1 脚本可以帮忙我们快速修改文件内容,还不需要调用文件的底层 api,方便快捷
在编写 CMakeLists 时发现,项目不能够很好的使用 vcpkg tool chain,哪怕是在命令行中指定 vcpkg.cmake
如果只是简单的项目,vcpkg tool chain 可以正常工作,但是在稍微复杂一些的项目中,比如依赖的 vcpkg 的库过多,就会发现在编译时提示找不到相关的库
不过总的来说,这些都不是本文的重点,重点是如何编写好一个 ps1 脚本
正文:
我们需要读取一个文件的内容,并修改文件中的某个变量名,以及在特定的上下文中插入自定义字符串
# 读取项目文件内容
$scriptDir = $PSScriptRoot
$vcxprojPath = Join-Path $scriptDir "\build\project.vcxproj"
$vcxprojContent = Get-Content $vcxprojPath # 定义常量
$configurationPlatformDebug = '"''$(Configuration)|$(Platform)''==''Debug|Win32''"'
$configurationPlatformRelease = '"''$(Configuration)|$(Platform)''==''Release|Win32''"' # 添加 Vcpkg
$targetLine1 = ' <GenerateManifest Condition=' + $configurationPlatformRelease + '>true</GenerateManifest>'
$targetLine2 = ' </PropertyGroup>' # 修改 Debug 配置
$targetLine3 = ' <OutDir Condition=' + $configurationPlatformDebug + '>' + (Join-Path $scriptDir "build\Debug\") + '</OutDir>'
$targetLine4 = ' <IntDir Condition=' + $configurationPlatformDebug + '>project.dir\Debug\</IntDir>'
$targetLine5 = ' <TargetName Condition=' + $configurationPlatformDebug + '>project</TargetName>' # 修改 Release 配置
$targetLine6 = ' <OutDir Condition=' + $configurationPlatformRelease + '>' + (Join-Path $scriptDir "build\Release\") + '</OutDir>'
$targetLine7 = ' <IntDir Condition=' + $configurationPlatformRelease + '>project.dir\Release\</IntDir>'
$targetLine8 = ' <TargetName Condition=' + $configurationPlatformRelease + '>project</TargetName>' $targetLine9 = ' <PropertyGroup Condition=' + $configurationPlatformRelease + ' Label="Configuration">' $targetLine10 = ' <TargetExt Condition=' + $configurationPlatformRelease + '>.exe</TargetExt>' # 要替换的新文本
$newAttributes = @"
<PropertyGroup Label="Vcpkg" Condition=$configurationPlatformDebug>
<VcpkgConfiguration>Debug</VcpkgConfiguration>
</PropertyGroup>
<PropertyGroup Label="Globals">
<VcpkgTriplet Condition="'`$(Platform)'=='Win32'">x86-windows-static</VcpkgTriplet>
</PropertyGroup>
"@ $newTargetLines3 = ' <OutDir Condition=' + $configurationPlatformDebug + '>$(SolutionDir)bin\$(Configuration)\</OutDir>'
$newTargetLines4 = ' <IntDir Condition=' + $configurationPlatformDebug + '>$(SolutionDir)temp\$(Configuration)\$(ProjectName)\</IntDir>'
$newTargetLines5 = ' <TargetName Condition=' + $configurationPlatformDebug + '>$(SolutionName)_d</TargetName>' $newTargetLines6 = ' <OutDir Condition=' + $configurationPlatformRelease + '>$(SolutionDir)bin\Project\$(PJVersion)\</OutDir>'
$newTargetLines7 = ' <IntDir Condition=' + $configurationPlatformRelease + '>$(SolutionDir)temp\$(Configuration)\$(ProjectName)\</IntDir>'
$newTargetLines8 = ' <TargetName Condition=' + $configurationPlatformRelease + '>$(SolutionName)</TargetName>' $newTargetLines9 = ' <ConfigurationType>DynamicLibrary</ConfigurationType>' $newTargetLines10 = ' <TargetExt Condition=' + $configurationPlatformRelease + '>.dll</TargetExt>' $foundLines = @() # 因为只遍历一遍,所以要按先后顺序放置待修改的行
for ($i = 0; $i -lt $vcxprojContent.Length - 1; $i++) {
$line = $vcxprojContent[$i] # 检查是否找到目标行
if ($line -eq $targetLine9) {
$foundLines += $i + 1
} if ($line -eq $targetLine3) {
$foundLines += $i
} if ($line -eq $targetLine4) {
$foundLines += $i
} if ($line -eq $targetLine5) {
$foundLines += $i
} if ($line -eq $targetLine6) {
$foundLines += $i
} if ($line -eq $targetLine7) {
$foundLines += $i
} if ($line -eq $targetLine8) {
$foundLines += $i
} if ($line -eq $targetLine10) {
$foundLines += $i
} if ($line -eq $targetLine1 -and $vcxprojContent[$i + 1] -eq $targetLine2) {
$foundLines += $i + 2
} } # 判断是否找到所有待替换的行
if ($foundLines.Count -eq 9) {
# 替换目标行
$vcxprojContent[$foundLines[0]] = $newTargetLines9
$vcxprojContent[$foundLines[1]] = $newTargetLines3
$vcxprojContent[$foundLines[2]] = $newTargetLines4
$vcxprojContent[$foundLines[3]] = $newTargetLines5
$vcxprojContent[$foundLines[4]] = $newTargetLines6
$vcxprojContent[$foundLines[5]] = $newTargetLines7
$vcxprojContent[$foundLines[6]] = $newTargetLines8
$vcxprojContent[$foundLines[7]] = $newTargetLines10
# 在目标行后面插入新文本
$vcxprojContent = [System.Collections.ArrayList]($vcxprojContent -split "`r`n")
$vcxprojContent.Insert($foundLines[8], $newAttributes)
# 将修改后的内容保存回文件
$vcxprojContent | ForEach-Object { $_ } | Set-Content $vcxprojPath Write-Host "Target lines replaced successfully in project."
} else {
Write-Host "Specific lines not found in the file in project."
}
我们还可以通过 ps1 脚本改文件编码格式
比如 visual studio 默认接受 UTF-16 编码的 rc 文件,而我们使用 CMake 中的 configure_file 函数生成的文件默认是 UTF-8 编码
那么我们可以使用 -Encoding 来改变
# project.rc 转为 UTF-16 编码
$rcPath = Join-Path $scriptDir "\build\project.rc"
# 读取 UTF-8 编码的文件内容
$content = Get-Content -Path $rcPath -Encoding UTF8
# 将内容以 UTF-16 编码保存
Set-Content -Path $rcPath -Value $content -Encoding Unicode
补充:
时间匆忙,无法对每个 ps1 的函数一一讲解,有兴趣的可以查阅文档来了解其作用
如何编写一个 PowerShell 脚本的更多相关文章
- 编写一个BAT脚本协助运维人员遇到问题时候调测数据库是否有效连接成功的操作攻略
简单摘要: 1.内网系统出现故障需要排查 2.运维人员不熟悉数据库操作,没法通过连接数据库和执行SQL语句的方式排查数据库及数据是否正常 3.解决方案:编写一个bat脚本,运维人员双击运行即可. ...
- 工程师技术(五):Shell脚本的编写及测试、重定向输出的应用、使用特殊变量、编写一个判断脚本、编写一个批量添加用户脚本
一.Shell脚本的编写及测 目标: 本例要求两个简单的Shell脚本程序,任务目标如下: 1> 编写一个面世问候 /root/helloworld.sh 脚本,执行后显示出一段话“Hello ...
- shell编写一个判断脚本
shell编写一个判断脚本 4.1问题 本例要求在虚拟机server0上创建/roo ...
- 从0开始的Python学习013编写一个Python脚本
通过之前的学习我们已经了解了Python的很多基础运用了,现在我们尝试着做一个有使用价值的小脚本. 问题 需求: 我想要一个可以给我备份重要文件的程序. 需求分析: 首先文件是有存储路径,文件的路径和 ...
- python+pytest接口自动化(12)-自动化用例编写思路 (使用pytest编写一个测试脚本)
经过之前的学习铺垫,我们尝试着利用pytest框架编写一条接口自动化测试用例,来厘清接口自动化用例编写的思路. 我们在百度搜索天气查询,会出现如下图所示结果: 接下来,我们以该天气查询接口为例,编写接 ...
- 分享我编写的powershell脚本:ssh-copy-id.ps1
问:通过[字符串界面].如何从win,通过ssh,连接到sshd?答:在任意版本win中,通过cmd.exe,powershell.exe中调用ssh.exe,连接sshd. 问:通过[pow ...
- 编写一个python脚本功能-备份
版本一 解决方案当我们基本完成程序的设计,我们就可以编写代码了,它是对我们的解决方案的实施.版本一例10.1 备份脚本——版本一 #!/usr/bin/python # Filename: backu ...
- 1.编写一个shell脚本
一.shell和shell脚本 在linux系统下,以 #/bin/bash开头的文本会被shell解释器进行解释. shell是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操 ...
- sql server编写一个语句脚本自动清空各表数据以初始化数据库
问题:有时已有项目要移植,例如原来在广州地区使用的某系统,突然说惠州那边也要用这套一样的系统.或者,在demo环境下弄了一些测试数据.然后要清空全部表数据.如果表比较多的话,逐个表手工编写脚本就太麻烦 ...
- Python编写一个Python脚本
我想要一个可以为我的所有重要文件创建备份的程序.(下面测试环境为python2.7) 1.backup_ver1.py #!/usr/bin/python import os import time ...
随机推荐
- [转帖]可直接拿来用的kafka+prometheus+grafana监控告警配置
kafka配置jmx_exporter 点击:https://github.com/prometheus/jmx_exporter,选择下面的jar包下载: 将下载好的这个agent jar包上传到k ...
- [转帖]Kingbase实现Oracle userenv函数功能
目录 1. 问题 2. 文档概述 3. Oracle userenv()函数功能调研 3.1. 函数名称/函数原型 3.2. 函数功能 3.3. 参数介绍 3.3.1. Parameter 3.4. ...
- [转帖]python读取配置文件获取所有键值对_python总结——处理配置文件(ConfigParser)
python处理ConfigParser 使用ConfigParser模块读写ini文件 (转载) ConfigParserPython 的ConfigParser Module中定义了3个类对INI ...
- 非root用户搭建sftp以及进行简要使用的介绍
sftp的简介 关于sftp sftp是Secure FileTransferProtocol的缩写,安全文件传送协议,可以为传输文件提供一种安全的加密方法. sftp与 ftp有着几乎一样的语法和功 ...
- elementui中表格表头设置背景色
参考的地址: https://www.cnblogs.com/lljun/p/11551128.html 今天在设置表格的表头的时候,我通过类的时候 发现无法设置表格的表头设置不了颜色: 经过查找:原 ...
- 通过图片地址获取图片的base64,再通过base64获取二进制数据
class Program { static void Main(string[] args) { string base64 = getFileBase64("D:\\Users\\Vat ...
- 获取Unity和UGUUI内置组件的属性名
需求来源 在阅读UGUI的源码时,发现Unity对于私有字段才加了[[SerializeField]]标签,而public的没有,且在Editor扩展中,也是查找带序列化标签的私有字段进行修改,那么在 ...
- 【Java】引用传递?值传递?
引用传递和值传递,从上学那会儿就开始强调的概念,不管你是计算机相关专业还是自学Java,一定听过这么一句话: 方法调用参数如果是对象,那就是引用传递,如果是基本数据类型,就是值传递. 比如:funct ...
- Elasticsearch Relevance Engine---为AI变革提供高级搜索能力[ES向量搜索、常用配置参数、聚合功能等详解]
Elasticsearch Relevance Engine---为AI变革提供高级搜索能力[ES向量搜索.常用配置参数.聚合功能等详解] 今天要介绍的 Elasticsearch Relevance ...
- C/C++ 实现FTP文件上传下载
FTP(文件传输协议)是一种用于在网络上传输文件的标准协议.它属于因特网标准化的协议族之一,为文件的上传.下载和文件管理提供了一种标准化的方法,在Windows系统中操作FTP上传下载可以使用WinI ...