最近从一名前端程序员转行到了数据库方面,并首次接触到了Power Shell这一工具
现在有这样的需求,在某个目录下有其他程序生成的dtsx文件,需要有选择地把它们拷贝到部署目录下面
需要拷贝的文件列表写在xml文件里
形式如下:
<batch>
<add filepath="test.txt" virtualPath="PositionRisk" />
<add filepath="test1.txt" virtualPath="PositionRisk" />
<add filepath="test2.txt" virtualPath="PositionRisk" />
</batch>
filepath属性表示目录下文件名,virtualPath表示目标路径下的子文件夹
完整代码如下
$scriptPath=$MyInvocation.MyCommand.Definition | split-path;
$rootPath ="$scriptPath.\..\..\" | convert-path
$deployConfigPath="$scriptPath.\deployment.config"
$deployPath = "C:\Users\likaiboy\Desktop\des\etl\PositionRisk"
if(test-path $deployPath){
remove-item $deployPath -Force -Recurse
}
new-item -path "C:\Users\likaiboy\Desktop\des\etl" -name "PositionRisk" -type directory
$doc=new-object System.xml.XmlDocument
$doc.load($deployConfigPath)
$root=$doc.DocumentElement
$fileNodes=$root.SelectNodes("//add")
function CopyFile($item, $desPath){
copy-item -Path $item -Destination $desPath
}
for($i=0 ;$i -lt $fileNodes.Count ;$i++){
$fileName = $fileNodes.item($i).filepath
$filePath = "$rootPath\install\PositionRisk\$fileName"
$virtualPathAttr =$fileNodes.item($i).virtualPath
$virtualPath = "C:\Users\likaiboy\Desktop\des\etl\$virtualPathAttr"
if( (test-path $filePath) -and (test-path $virtualPath)){
CopyFile $filePath $virtualPath
}else{
"source file do not exists"
}
}