diff --git a/HPE-System-JDT-v0.1.ps1 b/HPE-System-JDT-v0.1.ps1 new file mode 100644 index 0000000..d27665c --- /dev/null +++ b/HPE-System-JDT-v0.1.ps1 @@ -0,0 +1,223 @@ +####################################################################################################### +# +# [NAME]: HPE-System Powershell script3 +# +#[PURPOSE]: +#定时检测本地服务器【Jebsen_DMStoTM1\transfertemp】是否有新文件产生 +#如有新文件产生则将目录剪切到指定目录中【E:\JebsenBIBAK】下 +# +# [AUTHOR]: Wang,Xinyan@hpe +# xinyanw@hpe.com +# +# +# [VERSION HISTORY]: +# 0.1 10/19/2016 - Wang,Xinyan - Initial release +# 0.2 10/24/2016 - Wang,Xinyan - Change detecting files to detecting folders +# +####################################################################################################### + + + +############################################ +#Logic Block 1: | 日志逻辑模块 +#LogError: | 错误日志方法 +#LogDebug: | 调试日志方法 +#LogInfo: | 信息日志方法 +############################################ +$scriptpath = $MyInvocation.MyCommand.Definition +Write-Host $scriptpath +$parentpath = Split-Path -Parent $scriptpath +$script:cfgfilepath = $parentpath + "\HPE-System.cfg" +$logfile = $parentpath + "\HPE-System.log" +Function write2log($logentry) +{ + if(Test-Path $logfile) { + #do nothing + } else { + try { + New-Item $logfile -type file -force + } catch { + + } + } + + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch { + Start-Sleep -m (Get-Random -minimum 30 -maximum 50) + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch { + Start-Sleep -m (Get-Random -minimum 150 -maximum 200) + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch {} + } + } +} + +Function LogError($action, $errmsg) +{ + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[ERROR] - [$($action)] $($errmsg)" + write2log($logentry) +} + +Function LogDebug($debugmsg) +{ + if ($debug -eq 0) + { + return $null + } + + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[DEBUG] - $($debugmsg)" + + write2log($logentry) +} + +Function LogInfo($infomsg) +{ + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[INFO] - $($infomsg)" + write2log($logentry) +} +########################################## +#Logic Block 2: | 读取参数文件逻辑 +#read_cfg: | 读取参数文件方法 +########################################## +$script:catalognames = "" +$script:filenames = "" +Function read_cfg($filename) +{ + LogInfo "Start Reading config file" + Write-Host -ForegroundColor Green $filename + $lines = get-content $filename -ReadCount 0 + foreach($line in $lines) { + if($line -match "^#"){continue} + if($line -match "^\s*;"){continue} + + $param = $line.split("=",2) + + switch ($param[0]) + { + + "host_name_JDT" + { + $script:hostname = $param[1] + + } + <#'catalog_name_PHI' + { + $script:catalognames = $param[1] + } + 'file_name_PHI' + { + $script:filenames = $param[1] + }#> + 'back_path_JDT' + { + $script:back_path = $param[1] + } + 'debug' + { + $script:debug = $param[1] + } + + } + } +} +############################################### +#Logic Block 3:|通过参数文件检测相应路径新文件 +# +############################################### + + +LogInfo "Start detect new files in HPE-System" + +if(Test-Path $cfgfilepath) { + read_cfg($cfgfilepath) + #$catalognameArray = $catalognames.split("|") + #$filenameArray = $filenames.split("|") + #$serverbakpath = Set-Location $back_path_2 + $serverbakpath = $back_path + #foreach ($catalogname in $catalognameArray) { + $serverfilepath = "\\"+$hostname+"\Jebsen_DMStoTM1\transfertemp" + LogInfo "Start to detect new files in $($serverfilepath)" + #foreach ($filename in $filenameArray) { + $folderCount = Get-ChildItem -Path \\DCFIleServer\Jebsen_DMStoTM1\transfertemp -Force | Where-Object { $_.PSIsContainer -eq $true } | Measure-Object | Select-Object -ExpandProperty Count + if($folderCount -eq 0) { + LogInfo "There is no sub folder in this folder" + } else { + LogInfo "Found sub folder and will start to backup one by one" + Get-ChildItem $serverfilepath | ForEach-Object -Process{ + if($_ -is [System.IO.DirectoryInfo]){ + Write-Host($_.name) + $catalogname = $_.name + $serverfolder = $serverfilepath+'\'+$catalogname + LogInfo "Found new folder 【$($serverfolder)】 and start to backup" + if(Test-Path $serverbakpath) { + try { + $nowtime = Get-Date -Format 'yyyy-MM-dd-HH-mm' + + $serverbak = $serverbakpath+"\"+$catalogname+"_"+$nowtime + Write-Host -ForegroundColor Yellow $serverbak + if(Test-Path $serverbak){ + + Write-Host -ForegroundColor Yellow $serverbak + } else { + New-Item $serverbak -type directory + } + Write-Host -ForegroundColor Red $serverfolder + $serverAllfile = $serverfolder+"\*.*" + Copy-Item $serverAllfile $serverbak + try { + LogInfo "backup successfully and delete 【$($filename)】" + Remove-Item $serverAllfile #TBD + } catch {} + + } catch { + LogError "backup failed" + } + } else { + LogInfo "Can not find backup path and will make one" + New-Item $serverbakpath -type directory + try { + LogInfo "Create file directory successfully and start to backup" + $nowtime = Get-Date -Format 'yyyy-MM-dd-HH-mm' + + $serverbak = $serverbakpath+"\"+$catalogname+"_"+$nowtime + if(Test-Path $serverbak){ + Write-Host -ForegroundColor Yellow $serverbak + } else { + New-Item $serverbak -type directory + } + Write-Host -ForegroundColor Red $serverfolder + $serverAllfile = $serverfolder+"\*.*" + Copy-Item $serverAllfile $serverbak + try { + LogInfo "BACKUP successfully and delete 【$($filename)】" + Remove-Item $serverAllfile #TBD + } catch {} + + + } catch {} + } + } + } + } + + #} + #} + +} else { + LogError "Can not find config file in this file path" +} + + diff --git a/HPE-System-KPI-v1.1.ps1 b/HPE-System-KPI-v1.1.ps1 new file mode 100644 index 0000000..37d57e0 --- /dev/null +++ b/HPE-System-KPI-v1.1.ps1 @@ -0,0 +1,217 @@ +####################################################################################################### +# +# [NAME]: HPE-System Powershell script2 +# +#[PURPOSE]: +#定时检测本地服务器指定目录是否有新文件产生,如有新文件产生则将目录剪切到指定目录中【E:\FromDealerBAK】下 +# +# [AUTHOR]: Wang,Xinyan@hpe +# xinyanw@hpe.com +# +# +# [VERSION HISTORY]: +# 0.1 10/14/2016 - Wang,Xinyan - Initial release +# +# +####################################################################################################### + + + +############################################ +#Logic Block 1: | 日志逻辑模块 +#LogError: | 错误日志方法 +#LogDebug: | 调试日志方法 +#LogInfo: | 信息日志方法 +############################################ +$scriptpath = $MyInvocation.MyCommand.Definition +Write-Host $scriptpath +$parentpath = Split-Path -Parent $scriptpath +$script:cfgfilepath = $parentpath + "\HPE-System.cfg" +$logfile = $parentpath + "\HPE-System.log" +Function write2log($logentry) +{ + if(Test-Path $logfile) { + #do nothing + } else { + try { + New-Item $logfile -type file -force + } catch { + + } + } + + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch { + Start-Sleep -m (Get-Random -minimum 30 -maximum 50) + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch { + Start-Sleep -m (Get-Random -minimum 150 -maximum 200) + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch {} + } + } +} + +Function LogError($action, $errmsg) +{ + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[ERROR] - [$($action)] $($errmsg)" + write2log($logentry) +} + +Function LogDebug($debugmsg) +{ + if ($debug -eq 0) + { + return $null + } + + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[DEBUG] - $($debugmsg)" + + write2log($logentry) +} + +Function LogInfo($infomsg) +{ + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[INFO] - $($infomsg)" + write2log($logentry) +} +########################################## +#Logic Block 2: | 读取参数文件逻辑 +#read_cfg: | 读取参数文件方法 +########################################## +$script:catalognames = "" +$script:filenames = "" +Function read_cfg($filename) +{ + LogInfo "Start Reading config file" + Write-Host -ForegroundColor Green $filename + $lines = get-content $filename -ReadCount 0 + foreach($line in $lines) { + if($line -match "^#"){continue} + if($line -match "^\s*;"){continue} + + $param = $line.split("=",2) + + switch ($param[0]) + { + + "host_name_KPI" + { + $script:hostname = $param[1] + + } + 'catalog_name_KPI' + { + $script:catalognames = $param[1] + } + 'file_name_KPI' + { + $script:filenames = $param[1] + } + 'back_path_KPI' + { + $script:back_path_2 = $param[1] + } + 'debug' + { + $script:debug = $param[1] + } + + } + } +} +############################################### +#Logic Block 3:|通过参数文件检测相应路径新文件 +# +############################################### + + +LogInfo "Start detect new files in HPE-System" + +if(Test-Path $cfgfilepath) { + read_cfg($cfgfilepath) + $catalognameArray = $catalognames.split("|") + $filenameArray = $filenames.split("|") + #$serverbakpath = Set-Location $back_path_2 + $serverbakpath = $back_path_2 + foreach ($catalogname in $catalognameArray) { + $serverfilepath = "\\"+$hostname+"\c$\"+$catalogname + LogInfo "Start to detect new files in $($serverfilepath)" + foreach ($filename in $filenameArray) { + $serverfilename = $serverfilepath+"\"+$filename + + if(Test-Path $serverfilename) { + LogInfo "Found new files 【$($filename)】 and start to backup" + if(Test-Path $serverbakpath) { + + try { + $nowtime = Get-Date -Format 'yyyy-MM-dd-HH-mm' + + $serverbak = $serverbakpath+"\"+$catalogname+"_"+$nowtime + Write-Host -ForegroundColor Yellow $serverbak + if(Test-Path $serverbak){ + + Write-Host -ForegroundColor Yellow $serverbak + } else { + New-Item $serverbak -type directory + } + Write-Host -ForegroundColor Red $serverfilepath + $serverAllfile = $serverfilepath+"\*.*" + Copy-Item $serverAllfile $serverbak + try { + LogInfo "backup successfully and delete 【$($filename)】" + Remove-Item $serverfilename #TBD + } catch {} + + } catch { + LogError "backup failed" + } + } else { + LogInfo "Can not find backup path and will make one" + New-Item $serverbakpath -type directory + try { + LogInfo "Create file directory successfully and start to backup" + $nowtime = Get-Date -Format 'yyyy-MM-dd-HH-mm' + + $serverbak = $serverbakpath+"\"+$catalogname+"_"+$nowtime + if(Test-Path $serverbak){ + Write-Host -ForegroundColor Yellow $serverbak + } else { + New-Item $serverbak -type directory + } + Write-Host -ForegroundColor Red $serverfilepath + $serverAllfile = $serverfilepath+"\*.*" + Copy-Item $serverAllfile $serverbak + try { + LogInfo "BACKUP successfully and delete 【$($filename)】" + Remove-Item $serverfilename #TBD + } catch {} + + + } catch {} + } + + + } else { + LogInfo "Can not found any new files" + } + } + } + +} else { + LogError "Can not find config file in this file path" +} + + diff --git a/HPE-System-PHI-v0.1.ps1 b/HPE-System-PHI-v0.1.ps1 new file mode 100644 index 0000000..4bedbde --- /dev/null +++ b/HPE-System-PHI-v0.1.ps1 @@ -0,0 +1,223 @@ +####################################################################################################### +# +# [NAME]: HPE-System Powershell script3 +# +#[PURPOSE]: +#定时检测本地服务器【Porsche_Holding_Interface】是否有新文件产生 +#如有新文件产生则将目录剪切到指定目录中【E:\PHIBAK】下 +# +# [AUTHOR]: Wang,Xinyan@hpe +# xinyanw@hpe.com +# +# +# [VERSION HISTORY]: +# 0.1 10/19/2016 - Wang,Xinyan - Initial release +# 0.2 10/24/2016 - Wang,Xinyan - Change detecting files to detecting folders +# +####################################################################################################### + + + +############################################ +#Logic Block 1: | 日志逻辑模块 +#LogError: | 错误日志方法 +#LogDebug: | 调试日志方法 +#LogInfo: | 信息日志方法 +############################################ +$scriptpath = $MyInvocation.MyCommand.Definition +Write-Host $scriptpath +$parentpath = Split-Path -Parent $scriptpath +$script:cfgfilepath = $parentpath + "\HPE-System.cfg" +$logfile = $parentpath + "\HPE-System.log" +Function write2log($logentry) +{ + if(Test-Path $logfile) { + #do nothing + } else { + try { + New-Item $logfile -type file -force + } catch { + + } + } + + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch { + Start-Sleep -m (Get-Random -minimum 30 -maximum 50) + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch { + Start-Sleep -m (Get-Random -minimum 150 -maximum 200) + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch {} + } + } +} + +Function LogError($action, $errmsg) +{ + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[ERROR] - [$($action)] $($errmsg)" + write2log($logentry) +} + +Function LogDebug($debugmsg) +{ + if ($debug -eq 0) + { + return $null + } + + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[DEBUG] - $($debugmsg)" + + write2log($logentry) +} + +Function LogInfo($infomsg) +{ + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[INFO] - $($infomsg)" + write2log($logentry) +} +########################################## +#Logic Block 2: | 读取参数文件逻辑 +#read_cfg: | 读取参数文件方法 +########################################## +$script:catalognames = "" +$script:filenames = "" +Function read_cfg($filename) +{ + LogInfo "Start Reading config file" + Write-Host -ForegroundColor Green $filename + $lines = get-content $filename -ReadCount 0 + foreach($line in $lines) { + if($line -match "^#"){continue} + if($line -match "^\s*;"){continue} + + $param = $line.split("=",2) + + switch ($param[0]) + { + + "host_name_PHI" + { + $script:hostname = $param[1] + + } + <#'catalog_name_PHI' + { + $script:catalognames = $param[1] + } + 'file_name_PHI' + { + $script:filenames = $param[1] + }#> + 'back_path_PHI' + { + $script:back_path = $param[1] + } + 'debug' + { + $script:debug = $param[1] + } + + } + } +} +############################################### +#Logic Block 3:|通过参数文件检测相应路径新文件 +# +############################################### + + +LogInfo "Start detect new files in HPE-System" + +if(Test-Path $cfgfilepath) { + read_cfg($cfgfilepath) + #$catalognameArray = $catalognames.split("|") + #$filenameArray = $filenames.split("|") + #$serverbakpath = Set-Location $back_path_2 + $serverbakpath = $back_path + #foreach ($catalogname in $catalognameArray) { + $serverfilepath = "\\"+$hostname+"\Porsche_Holding_Interface" + LogInfo "Start to detect new files in $($serverfilepath)" + #foreach ($filename in $filenameArray) { + $folderCount = Get-ChildItem -Path \\DCFIleServer\Porsche_Holding_Interface\ -Force | Where-Object { $_.PSIsContainer -eq $true } | Measure-Object | Select-Object -ExpandProperty Count + if($folderCount -eq 0) { + LogInfo "There is no sub folder in this folder" + } else { + LogInfo "Found sub folder and will start to backup one by one" + Get-ChildItem $serverfilepath | ForEach-Object -Process{ + if($_ -is [System.IO.DirectoryInfo]){ + Write-Host($_.name) + $catalogname = $_.name + $serverfolder = $serverfilepath+'\'+$catalogname + LogInfo "Found new folder 【$($serverfolder)】 and start to backup" + if(Test-Path $serverbakpath) { + try { + $nowtime = Get-Date -Format 'yyyy-MM-dd-HH-mm' + + $serverbak = $serverbakpath+"\"+$catalogname+"_"+$nowtime + Write-Host -ForegroundColor Yellow $serverbak + if(Test-Path $serverbak){ + + Write-Host -ForegroundColor Yellow $serverbak + } else { + New-Item $serverbak -type directory + } + Write-Host -ForegroundColor Red $serverfolder + $serverAllfile = $serverfolder+"\*.*" + Copy-Item $serverAllfile $serverbak + try { + LogInfo "backup successfully and delete 【$($filename)】" + Remove-Item $serverAllfile #TBD + } catch {} + + } catch { + LogError "backup failed" + } + } else { + LogInfo "Can not find backup path and will make one" + New-Item $serverbakpath -type directory + try { + LogInfo "Create file directory successfully and start to backup" + $nowtime = Get-Date -Format 'yyyy-MM-dd-HH-mm' + + $serverbak = $serverbakpath+"\"+$catalogname+"_"+$nowtime + if(Test-Path $serverbak){ + Write-Host -ForegroundColor Yellow $serverbak + } else { + New-Item $serverbak -type directory + } + Write-Host -ForegroundColor Red $serverfolder + $serverAllfile = $serverfolder+"\*.*" + Copy-Item $serverAllfile $serverbak + try { + LogInfo "BACKUP successfully and delete 【$($filename)】" + Remove-Item $serverAllfile #TBD + } catch {} + + + } catch {} + } + } + } + } + + #} + #} + +} else { + LogError "Can not find config file in this file path" +} + + diff --git a/HPE-System-PI-v0.2.ps1 b/HPE-System-PI-v0.2.ps1 new file mode 100644 index 0000000..84da926 --- /dev/null +++ b/HPE-System-PI-v0.2.ps1 @@ -0,0 +1,279 @@ +####################################################################################################### +# +# [NAME]: HPE-System Powershell script +# +#[PURPOSE]: +#定时检测本地服务器指定目录是否有新文件产生,如有新文件产生则将目录备份到指定目录中【E:\ToDealerBAK】并将检测 +#文件复制到所有的【Dealer No】目录下 +# +# [AUTHOR]: Wang,Xinyan@hpe +# xinyanw@hpe.com +# +# +# [VERSION HISTORY]: +# 0.1 10/12/2016 - Wang,Xinyan - Initial release +# 0.2 10/14/2016 - Wang,Xinyan - change 【copy file】 to 【copy the whole directory】 +# 0.3 10/24/2016 - Wang,Xinyan - change backup path to local path/change dealer_no config to mutiple +# +####################################################################################################### + + + +############################################ +#Logic Block 1: | 日志逻辑模块 +#LogError: | 错误日志方法 +#LogDebug: | 调试日志方法 +#LogInfo: | 信息日志方法 +############################################ +$scriptpath = $MyInvocation.MyCommand.Definition +Write-Host $scriptpath +$parentpath = Split-Path -Parent $scriptpath +$script:cfgfilepath = $parentpath + "\HPE-System.cfg" +$logfile = $parentpath + "\HPE-System.log" +Function write2log($logentry) +{ + if(Test-Path $logfile) { + #do nothing + } else { + try { + New-Item $logfile -type file -force + } catch { + + } + } + + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch { + Start-Sleep -m (Get-Random -minimum 30 -maximum 50) + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch { + Start-Sleep -m (Get-Random -minimum 150 -maximum 200) + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch {} + } + } +} + +Function LogError($action, $errmsg) +{ + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[ERROR] - [$($action)] $($errmsg)" + write2log($logentry) +} + +Function LogDebug($debugmsg) +{ + if ($debug -eq 0) + { + return $null + } + + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[DEBUG] - $($debugmsg)" + + write2log($logentry) +} + +Function LogInfo($infomsg) +{ + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[INFO] - $($infomsg)" + write2log($logentry) +} +########################################## +#Logic Block 2: | 读取参数文件逻辑 +#read_cfg: | 读取参数文件方法 +########################################## +$script:catalognames = "" +$script:filenames = "" +Function read_cfg($filename) +{ + LogInfo "Start Reading config file" + Write-Host -ForegroundColor Green $filename + $lines = get-content $filename -ReadCount 0 + foreach($line in $lines) { + if($line -match "^#"){continue} + if($line -match "^\s*;"){continue} + + $param = $line.split("=",2) + + switch ($param[0]) + { + + "host_name_PI" + { + $script:hostname = $param[1] + + } + 'catalog_name_PI' + { + $script:catalognames = $param[1] + } + 'file_name_PI' + { + $script:filenames = $param[1] + } + 'back_path_PI' + { + $script:back_path = $param[1] + } + 'PI_des_path' + { + $script:des_path = $param[1] + + } + 'dealer_no' + { + $script:dealernos = $param[1] + } + 'debug' + { + $script:debug = $param[1] + } + + } + } +} +############################################### +#Logic Block 3:|通过参数文件检测相应路径新文件 +# +############################################### + + +LogInfo "Start detect new files in HPE-System" + +if(Test-Path $cfgfilepath) { + read_cfg($cfgfilepath) + $catalognameArray = $catalognames.split("|") + $filenameArray = $filenames.split("|") + $dealernoArray = $dealernos.split("|") + $serverbakpath = $back_path + $serverdes = "\\"+$hostname+"\"+$des_path + foreach ($catalogname in $catalognameArray) { + $serverfilepath = "\\"+$hostname+"\"+$catalogname + LogInfo "Start to detect new files in $($serverfilepath)" + foreach ($filename in $filenameArray) { + $serverfilename = $serverfilepath+"\"+$filename + Write-Host -ForegroundColor Yellow $serverfilename + if(Test-Path $serverfilename) { + LogInfo "Found new files 【$($filename)】 and start to backup" + if(Test-Path $serverbakpath) { + try { + $nowtime = Get-Date -Format 'yyyy-MM-dd' + + $serverbak = $serverbakpath+"\"+$catalogname+"_"+$nowtime + if(Test-Path $serverbak){ + Write-Host -ForegroundColor Yellow $serverbak + } else { + New-Item $serverbak -type directory + } + Write-Host -ForegroundColor Red $serverfilepath + $serverAllfile = $serverfilepath+"\*.*" + Copy-Item $serverAllfile $serverbak + + if(Test-Path $serverdes) { + try { + LogInfo "backup successfully and start to copy 【$($filename)】" + foreach ($dealerno in $dealernoArray) { + $serverdespath = $serverdes+'\'+$dealerno + if(Test-Path $serverdespath) { + LogInfo "Found the destination path $($serverdespath)" + Copy-Item $serverfilename -destination $serverdespath + }else{ + LogInfo "Cannot find the destination path and will create one" + New-Item $serverdespath -type directory + Copy-Item $serverfilename -destination $serverdespath + } + } + + try { + LogInfo "Copy successfully and delete 【$($filename)】" + #Remove-Item $serverfilename #TBD + } catch {} + } catch { + LogError "Copy Failed" + } + } else { + LogInfo "Can not find copy path and will make one" + New-Item $serverdespath -type directory + try { + LogInfo "create $($serverdespath) successfully and start to copy file" + Copy-Item $serverfilename -destination $serverdespath + try { + LogInfo "Copy successfully and delete 【$($filename)】" + #Remove-Item $serverfilename #TBD + } catch {} + } catch { + LogError "Copy failed" + } + } + } catch { + LogError "backup failed" + } + } else { + LogInfo "Can not find backup path and will make one" + New-Item $serverbakpath -type directory + try { + LogInfo "Create file directory successfully and start to backup" + $nowtime = Get-Date -Format 'yyyy-MM-dd' + + $serverbak = $serverbakpath+"\"+$catalogname+"_"+$nowtime + if(Test-Path $serverbak){ + Write-Host -ForegroundColor Yellow $serverbak + } else { + New-Item $serverbak -type directory + } + Write-Host -ForegroundColor Red $serverfilepath + $serverAllfile = $serverfilepath+"\*.*" + Copy-Item $serverAllfile $serverbak + if(Test-Path $serverdespath) { + try { + LogInfo "backup successfully and start to copy 【$($filename)】" + + Copy-Item $serverfilename -destination $serverdespath + try { + LogInfo "Copy successfully and delete 【$($filename)】" + #Remove-Item $serverfilename #TBD + } catch {} + } catch { + LogError "Copy Failed" + } + } else { + LogInfo "Can not find copy path and will make one" + New-Item $serverdespath -type directory + try { + LogInfo "create $($serverdespath) successfully and start to copy file" + Copy-Item $serverfilename -destination $serverdespath + try { + LogInfo "Copy successfully and delete 【$($filename)】" + #Remove-Item $serverfilename #TBD + } catch {} + } catch { + LogError "Copy failed" + } + } + + } catch {} + } + + + } else { + LogInfo "Can not found any new files" + } + } + } + +} else { + LogError "Can not find config file in this file path" +} + + diff --git a/HPE-System-SI-v0.1.ps1 b/HPE-System-SI-v0.1.ps1 new file mode 100644 index 0000000..ce6e589 --- /dev/null +++ b/HPE-System-SI-v0.1.ps1 @@ -0,0 +1,223 @@ +####################################################################################################### +# +# [NAME]: HPE-System Powershell script3 +# +#[PURPOSE]: +#定时检测本地服务器【Starchase_Interface】是否有新文件产生 +#如有新文件产生则将目录剪切到指定目录中【E:\StarchaseBAK】下 +# +# [AUTHOR]: Wang,Xinyan@hpe +# xinyanw@hpe.com +# +# +# [VERSION HISTORY]: +# 0.1 10/19/2016 - Wang,Xinyan - Initial release +# 0.2 10/24/2016 - Wang,Xinyan - Change detecting files to detecting folders +# +####################################################################################################### + + + +############################################ +#Logic Block 1: | 日志逻辑模块 +#LogError: | 错误日志方法 +#LogDebug: | 调试日志方法 +#LogInfo: | 信息日志方法 +############################################ +$scriptpath = $MyInvocation.MyCommand.Definition +Write-Host $scriptpath +$parentpath = Split-Path -Parent $scriptpath +$script:cfgfilepath = $parentpath + "\HPE-System.cfg" +$logfile = $parentpath + "\HPE-System.log" +Function write2log($logentry) +{ + if(Test-Path $logfile) { + #do nothing + } else { + try { + New-Item $logfile -type file -force + } catch { + + } + } + + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch { + Start-Sleep -m (Get-Random -minimum 30 -maximum 50) + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch { + Start-Sleep -m (Get-Random -minimum 150 -maximum 200) + try { + $logentry | out-file -Filepath $logfile -force -Append -Encoding ASCII + } catch {} + } + } +} + +Function LogError($action, $errmsg) +{ + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[ERROR] - [$($action)] $($errmsg)" + write2log($logentry) +} + +Function LogDebug($debugmsg) +{ + if ($debug -eq 0) + { + return $null + } + + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[DEBUG] - $($debugmsg)" + + write2log($logentry) +} + +Function LogInfo($infomsg) +{ + $dt = Get-Date + $dtstr = $dt.ToString() + + $logentry = "[$($dtstr)]|[INFO] - $($infomsg)" + write2log($logentry) +} +########################################## +#Logic Block 2: | 读取参数文件逻辑 +#read_cfg: | 读取参数文件方法 +########################################## +$script:catalognames = "" +$script:filenames = "" +Function read_cfg($filename) +{ + LogInfo "Start Reading config file" + Write-Host -ForegroundColor Green $filename + $lines = get-content $filename -ReadCount 0 + foreach($line in $lines) { + if($line -match "^#"){continue} + if($line -match "^\s*;"){continue} + + $param = $line.split("=",2) + + switch ($param[0]) + { + + "host_name_SI" + { + $script:hostname = $param[1] + + } + <#'catalog_name_PHI' + { + $script:catalognames = $param[1] + } + 'file_name_PHI' + { + $script:filenames = $param[1] + }#> + 'back_path_SI' + { + $script:back_path = $param[1] + } + 'debug' + { + $script:debug = $param[1] + } + + } + } +} +############################################### +#Logic Block 3:|通过参数文件检测相应路径新文件 +# +############################################### + + +LogInfo "Start detect new files in HPE-System" + +if(Test-Path $cfgfilepath) { + read_cfg($cfgfilepath) + #$catalognameArray = $catalognames.split("|") + #$filenameArray = $filenames.split("|") + #$serverbakpath = Set-Location $back_path_2 + $serverbakpath = $back_path + #foreach ($catalogname in $catalognameArray) { + $serverfilepath = "\\"+$hostname+"\Starchase_Interface" + LogInfo "Start to detect new files in $($serverfilepath)" + #foreach ($filename in $filenameArray) { + $folderCount = Get-ChildItem -Path \\DCFIleServer\Starchase_Interface\ -Force | Where-Object { $_.PSIsContainer -eq $true } | Measure-Object | Select-Object -ExpandProperty Count + if($folderCount -eq 0) { + LogInfo "There is no sub folder in this folder" + } else { + LogInfo "Found sub folder and will start to backup one by one" + Get-ChildItem $serverfilepath | ForEach-Object -Process{ + if($_ -is [System.IO.DirectoryInfo]){ + Write-Host($_.name) + $catalogname = $_.name + $serverfolder = $serverfilepath+'\'+$catalogname + LogInfo "Found new folder 【$($serverfolder)】 and start to backup" + if(Test-Path $serverbakpath) { + try { + $nowtime = Get-Date -Format 'yyyy-MM-dd-HH-mm' + + $serverbak = $serverbakpath+"\"+$catalogname+"_"+$nowtime + Write-Host -ForegroundColor Yellow $serverbak + if(Test-Path $serverbak){ + + Write-Host -ForegroundColor Yellow $serverbak + } else { + New-Item $serverbak -type directory + } + Write-Host -ForegroundColor Red $serverfolder + $serverAllfile = $serverfolder+"\*.*" + Copy-Item $serverAllfile $serverbak + try { + LogInfo "backup successfully and delete 【$($filename)】" + Remove-Item $serverAllfile #TBD + } catch {} + + } catch { + LogError "backup failed" + } + } else { + LogInfo "Can not find backup path and will make one" + New-Item $serverbakpath -type directory + try { + LogInfo "Create file directory successfully and start to backup" + $nowtime = Get-Date -Format 'yyyy-MM-dd-HH-mm' + + $serverbak = $serverbakpath+"\"+$catalogname+"_"+$nowtime + if(Test-Path $serverbak){ + Write-Host -ForegroundColor Yellow $serverbak + } else { + New-Item $serverbak -type directory + } + Write-Host -ForegroundColor Red $serverfolder + $serverAllfile = $serverfolder+"\*.*" + Copy-Item $serverAllfile $serverbak + try { + LogInfo "BACKUP successfully and delete 【$($filename)】" + Remove-Item $serverAllfile #TBD + } catch {} + + + } catch {} + } + } + } + } + + #} + #} + +} else { + LogError "Can not find config file in this file path" +} + + diff --git a/HPE-System.cfg b/HPE-System.cfg new file mode 100644 index 0000000..f0548b9 --- /dev/null +++ b/HPE-System.cfg @@ -0,0 +1,22 @@ +host_name_PI=PCN-TEST +host_name_KPI=DCFIleServer +host_name_PHI=DCFIleServer +host_name_SI=DCFIleServer +host_name_JDT=DCFIleServer +catalog_name_PI=Users\Administrator\Desktop\test|Users\Administrator\Desktop\test2 +catalog_name_KPI=Users\Administrator\Desktop\testscript|Users\Administrator\Desktop\testscript2 +catalog_name_PHI=Users\Administrator\Desktop\testscript|Users\Administrator\Desktop\testscript2 +catalog_name_SI=Users\Administrator\Desktop\testscript|Users\Administrator\Desktop\testscript2 +catalog_name_JDT=Users\Administrator\Desktop\testscript|Users\Administrator\Desktop\testscript2 +back_path_PI=c:\3 +back_path_KPI=d:\test +back_path_PHI=d:\test +back_path_SI=d:\test +back_path_JDT=d:\test +file_name_PI=testfile.txt|testfile2.txt|test3.txt|testfile4.txt|testfile5.txt +file_name_KPI=ASKPI_2_201610.csv|20161014_Reporting_Month_2.csv +file_name_PHI=ASKPI_2_201610.csv|20161014_Reporting_Month_2.csv +file_name_SI=ASKPI_2_201610.csv|20161014_Reporting_Month_2.csv +file_name_JDT=ASKPI_2_201610.csv|20161014_Reporting_Month_2.csv +dealer_no=3 +debug=1