一、基础文件及文本操作
1、压缩和解压缩:此处用7z
1)前提:需要安装7z压缩软件;
2)语法:
7z a 压缩文件名 目录 压缩格式 a:标识压缩 x: 标识解压缩
3)实例:
7z a $zipfile .\* -tzip 7z x *.zip -oD:\JenkinsWS\FW_730\ERP -aoa
2、文本拷贝
1)语法:
Copy-Item 源目录 目标目录 -exclude 排除的文件名1,文件名2 -Force -recurse -exclude:排除某些文件不拷贝 -Force:复制只读文件 -recurse:递归复制,不弹出确认提示 ......其他参数可根据需要自行查找;
2)实例:
Copy-Item $ENV:WORKSPACE\src\04_网站配置\* $ENV:PackagePath\Mysoftzxtb\src\ -exclude MysoftVersion.config,web.config -Force -recurse
3、文件删除
1)语法:
Remove-Item 目录和文件名 -Recurse
2)实例:
Remove-Item $ENV:MasterPath\模板文件\* -Recurse
4、文件目录操作:
1)目录存在性判断
a、语法:Test-Path 目录或者文件地址 -PathType Leaf
-path :目录地址 -PathType :指定目录下文件类型,leaf:普通文件或注册表项;Container:目录或者注册表键值
b、实例:
if(Test-Path $zipPath -PathType Leaf){ echo "::::::: 跳过下载,使用本地文件 $($zipPath) " }
2)目录读取:
a、语法:Get-ChildItem 获取路径下的目录和文件
Get-ChildItem 目录路径
b、实例:
cd $SQLPath #获取目录下的子目录,排除文件 $ListDir = Get-ChildItem .\ | ?{$_.PsIsContainer -eq $true} echo $ListDir foreach($item in $ListDir){ $Path = "$SQLPath\$item" cd $Path $sqlDirStatus = Test-Path $Path\sql if(!$sqlDirStatus){ md sql }else{ Remove-Item $Path\sql\* -Recurse } Copy-Item -Path "$Path\*.sql" -Destination "$Path\sql" -Recurse }
5、正则匹配
二、config文件读取和写入
1、读取文件:
a、语法:
读取config文件内容:Get-Content config文件路径 读取xml对象中的节点:$XmlContent.MysoftVersion.Products.Product(有点类似于Xpath或Json节点的选取)
b、实例:
#从MysoftVersion.config文件中获取当前版本号 function GetProductVersionFromMysoftVersion{ param([string]$app) [Xml]$XmlContent = Get-Content "$ENV:sitePathAll\MysoftVersion.config" $xml = $XmlContent.MysoftVersion.Products.Product | Where-Object {$_.ProductKey -eq "$($app)"} $aa = $xml | Out-String if($aa -eq ""){ echo "MysoftVersion.config文件中不存在此应用$($app)的配置!" exit 1; break; } $CurrentVersion = $xml.ProductVersion.InnerText $bb = $CurrentVersion | Out-String if($bb -ne ""){ return $CurrentVersion; }else{ $CurrentVersion = $xml.ProductVersion; return $CurrentVersion; } }
2、写入config文件:
a、语法:
给xml对象的节点赋值:$xml.ProductVersion.InnerText = $Version; 将整个xml对象保存到文件:$XmlContent.save("$ENV:sitePathAll\MysoftVersion.config")
b、实例:
#将最新版本号存储到MysoftVersion.config中 function InsertVersionToMysoftVersion{ param([string]$app) echo "=================== 将最新版本号存储到MysoftVersion.config中 ===================" $Version = GetProductVersionFromRdc -app $($app) [Xml]$XmlContent = Get-Content "$ENV:sitePathAll\MysoftVersion.config" $xml = $XmlContent.MysoftVersion.Products.Product | Where-Object {$_.ProductKey -eq "$($app)"} $CurrentVersion = $xml.ProductVersion.InnerText $bb = $CurrentVersion | Out-String if($bb -ne ""){ $xml.ProductVersion.InnerText = $Version; #xml对象被更改后,保存到config文件中 $XmlContent.save("$ENV:sitePathAll\MysoftVersion.config") }else{ $xml.ProductVersion = $Version #xml对象被更改后,保存到config文件中 $XmlContent.save("$ENV:sitePathAll\MysoftVersion.config") } }
三、dll文件版本号读取
#获取dll文件版本号; $dllVersion=(Get-Item $ENV:sitePathAll\bin\$($dllname)).VersionInfo.FileVersion;
四、xml文件读写
同config文件
五、Excel读写
1、语法:
#创建一个Excel对象 $excel = New-Object -ComObject Excel.Application #打开Excel工作簿 $workbook=$excel.Workbooks.open($($ExcelPath)) #打开Excel指定Sheet页签 $WorkSheet = $workbook.Sheets.Item("系统管理") #获得指定单元格的值 $WorkSheet.Cells.Item($i,$j).Value() #给指定单元格赋值 $sheet.Cells.Item($i+1,3)="配置中心"; #设置单元格自适应宽度 $range = $sheet.usedRange $range.EntireColumn.AutoFit() | out-null #保存打开的工作薄 $workbook.Save() #退出 $excel.Quit() #释放 $excel = $null [GC]::Collect()
2、实例:
[Xml]$XmlContent = Get-Content "D:\JenkinsWS\SetupDir\src\04_网站配置\MysoftVersion.config" $xml = $XmlContent.MysoftVersion.Products.product $excel = New-Object -ComObject Excel.Application #打开Excel表格 $workbook=$excel.Workbooks.open("D:\JenkinsWS\SetupDir\明源云ERP系统V3.5各产品版本号.xls") $Name = Get-Random -Maximum 100 $workbook.Worksheets.Add().name="Sheet$Name" $sheet = $excel.Worksheets.Item("Sheet$Name") $LineTitle = @("ERP版本号","发布日期","子系统/增值模块","子系统版本号","发布说明") for($i=0;$i -lt $LineTitle.Length;$i++){ $sheet.Cells.Item(1,$i+1)=$lineTitle[$i] } $i=2; foreach($item in $xml){ #echo "$i" $sheet.Cells.Item($i,3)=$item.ProductName; echo "$item.ProductName"; $i = $i+1; } $i=2; foreach($item in $xml){ if($item.ProductVersion.versionSources){ $version = $item.ProductVersion.InnerText; echo "$i" $sheet.Cells.Item($i,4)=$version; $i = $i+1; }else{ $version = $item.ProductVersion; $sheet.Cells.Item($i,4)=$version; $i = $i+1; } } $sheet.Cells.Item($i+1,3)="配置中心"; $sheet.Cells.Item($i+1,4)=$ENV:pzzx_version; $i++; $sheet.Cells.Item($i+1,3)="数据服务中心"; $sheet.Cells.Item($i+1,4)=$ENV:sjztfw_version; $i++; $sheet.Cells.Item($i+1,3)="调度服务"; $sheet.Cells.Item($i+1,4)=$ENV:ScheduleService_version; $i++; $sheet.Cells.Item($i+1,3)="文档服务"; $sheet.Cells.Item($i+1,4)=$ENV:mysoftDoc_version; $i++; $sheet.Cells.Item($i+1,3)="报表服务"; $sheet.Cells.Item($i+1,4)=$ENV:ReportServices_verison; $i++; $sheet.Cells.Item($i+1,3)="报表设计器(RTS)"; $sheet.Cells.Item($i+1,4)="无"; $i++; $sheet.Cells.Item($i+1,3)="系统管理"; $sheet.Cells.Item($i+1,4)="同平台"; #设置单元格自适应宽度 $range = $sheet.usedRange $range.EntireColumn.AutoFit() | out-null $workbook.Save() $excel.Quit() $excel = $null [GC]::Collect()