0
我試圖修復一個腳本,似乎我在這裏失去了線程。我總是在我的日誌文件中獲得成功的結果,說實話,這是一個很好的結果,但即使我刪除了源文件夾,它也會得到成功:(我不是代碼編寫者,但是,我有一些功能可以工作,但仍然不起作用的功能如下: 1-當Source文件夾或文件不存在時,它應該創建一個帶有Fail值的註冊表項,所以SCCM將會將其作爲檢測方法來評估部署並將其寫入日誌文件:C:\ logs \ CopyTNSNames_1.0.logPowershell腳本來備份和複製本地文件夾上的TNSNames.ora文件
在這裏,您是我的代碼,並隨意使用它,因爲它可以很好地複製文件從一個網絡共享,它會創建一個帶有時間戳的備份,當然一旦修復:
# Function
Function writetolog ($nTxt){
$date = Get-Date
$sapce = ' '
$log_path = 'C:\logs\'
$log_name = $log_path + 'CopyTNSNames_1.0.log'
if(!(Test-Path -Path $log_path)){New-Item -ItemType directory -Path $log_path}
Add-content $log_name -value $date$sapce$nTxt
}
Function code_retour ($rc){
$date = Get-Date
$rc_ok = 0,3010
$Script_returnCode = 0
$CodeDeRetour = "Return code : ",$rc
if($rc_ok -contains $rc){
writetolog $CodeDeRetour
return $rc
}Else{
writetolog $CodeDeRetour
writetolog 'The script has failed'
writetolog ''
writetolog "**************************"
writetoreg ('Fail',$rc)
exit $rc}
}
Function writetoreg($status){
$date = Get-Date
$status_rc = 'RC = ' + $status[1]
$regpath_MyCompany = 'hklm:software\MyCompany'
$regpath_Tier = 'hklm:software\MyCompany\Applications'
$regpath_App = 'hklm:software\MyCompany\Applications\CopyTNSNames_1.0'
$regpath_DT = 'hklm:software\MyCompany\Applications\CopyTNSNames_1.0\Result'
if(!(Test-Path $regpath_MyCompany)){New-Item -Path $regpath_MyCompany}
if(!(Test-Path $regpath_Tier)){New-Item -Path $regpath_Tier}
if(!(Test-Path $regpath_App)){New-Item -Path $regpath_App}
if(!(Test-Path $regpath_DT)){New-Item -Path $regpath_DT}
If($status[0] -eq 'Succes'){
$result = 'Succes'
Set-ItemProperty -Path $regpath_DT -Name $result -Value $date}
If($status[0] -eq 'Failure'){
$result = 'Failure Timestamp ' + $date
Set-ItemProperty -Path $regpath_DT -Name $result -Value $status_rc}
}
cls
Function CopyOra
{
$PathFolder = "\\partage\ORAFiles\Source"
$DestinationFolder = "c:\Apps\Oracle\11.2\network\admin\"
if (!(test-path $DestinationFolder))
{
New-Item $DestinationFolder -ItemType Directory
"$(Get-Date) : Created folder $DestinationFolder" | Out-File "C:\Logs\CopyTNSNames_1.0" -Append
}
$Dir = get-childitem $PathFolder | Sort-Object Time -descending
$LOGFilesFound = $Dir | where { $_.extension -eq ".ora" }
"$(Get-Date) : processing files: $LOGFilesFound" | Out-File "C:\Logs\CopyTNSNames_1.0" -Append
write-host "$(Get-Date) : processing files: $LOGFilesFound"
foreach ($LOGFile in $LOGFilesFound)
{
write-host $PathFolder\$LOGFile
Try
{
If (Test-Path $DestinationFolder$LOGFile)
{
"$(Get-Date) : fichier trouvé $DestinationFolder$LOGFile ... Copie de backup en cours." | Out-File "C:\Logs\CopyTNSNames_1.0" –Append
$now=Get-Date -format "dd-MM-yyyy HH-mm"
$newFileName = "$LOGFile-$now"
copy-item -path "$DestinationFolder$LOGFile" -destination "$DestinationFolder$newFileName" -Container -Force:$true -Recurse
"$(Get-Date) : $DestinationFolder$newFileName créé." | Out-File "C:\Logs\CopyTNSNames_1.0" –Append
} else
{
write-host "$DestinationFolder$LOGFile non trouvé sur le poste"
"$(Get-Date) : $DestinationFolder$newFileName non trouvé localement." | Out-File "C:\Logs\CopyTNSNames_1.0" –Append
}
copy-item -path "$PathFolder\$LOGFile" -destination "$DestinationFolder$LOGFile" -Container -Force:$true -Recurse
"$(Get-Date) : Copy done $PathFolder\$LOGFile" | Out-File "C:\Logs\CopyTNSNames_1.0" –Append
}
Catch
{
"$(Get-Date) : copie non possible..." | Out-File "C:\Logs\" –Append
$Script_returnCode = code_retour $returnCode.ExitCode
write-host $returnCode.ExitCode
}
}
}
#Run main function
CopyOra
writetolog ""
writetolog "End of script"
writetolog "**************************"
writetoreg ("Success",$Script_returnCode)
exit $Script_returnCode
希望你能幫助我解決這個問題。 此致敬禮。
Hi和感謝您的答覆。 – derspinne
似乎無法在註冊表中寫入。 2017-02-14 13:56:51處理文件: 2017-02-14 13:56:51 c:\ Apps \ Oracle \ 11.2 \ network \ admin \ nontrouvélocalement。 2017-02-14 13:56:51 Copieeffectuéede \\ partage \ ORAFiles \ Source \ Tnsnames.ora 2017-02-14 13:56:51返回代碼:0 2017-02-14 13:56: 51 Impossible d'Écriredans la base de registre,fonction writetoreg 2017-02-14 13:56:51 Erreurdétectée...Détail:Impossible de lier l'argument auparamètre«路徑»,car il a la valeur Null。 , 親切的問候。 – derspinne
你現在對你的問題進行解釋。你不能寫入註冊表的這一部分 – Esperento57