2017-02-10 114 views
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 

希望你能幫助我解決這個問題。 此致敬禮。

回答

0

我已經正確重建您攔截錯誤的腳本,請始終使用日誌功能,並刪除不必要的代碼

試試這個:

Function writetolog ($nTxt){ 
$log_path = 'C:\logs\' 
New-Item -ItemType directory -Path $log_path -Force | Out-Null 

$log_Text="{0} {1}" -f (Get-Date), $nTxt 
$log_name = $log_path + 'CopyTNSNames_1.0.log' 
Add-content $log_name -value $log_Text 
} 

Function code_retour ($rc){ 
$rc_ok = 0 
$CodeDeRetour = "Return code : $rc" 

if($rc_ok -eq $rc) 
{ 
    writetolog $CodeDeRetour 
    writetoreg ("Success",$rc) 
    writetolog "" 
    writetolog "End of script" 
    writetolog "**************************" 
    exit $rc 
} 
Else 
{ 
    writetolog $CodeDeRetour 
    writetolog 'The script has failed' 
    writetolog '' 
    writetolog "**************************" 
    writetoreg ('Fail',$rc) 
    exit $rc 
} 

} 

Function writetoreg($status){ 

try 
{ 
$date = Get-Date 
$status_rc = 'RC = ' + $status[1] 
$regpath_DT = 'hkcu:software\MyCompany\Applications\CopyTNSNames_1.0\Result' 

if(!(Test-Path $regpath_DT)) {New-Item -Path $regpath_DT -force} 

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 
} 
} 
catch [System.Exception] 
{ 
    writetolog "Impossible d'écrire dans la base de registre, fonction writetoreg" 
    $ErrorMessage = $_.Exception.Message 
    $FailedItem = $_.Exception.ItemName 
    writetolog "Erreur détectée...Détail : $ErrorMessage, $FailedItem" 
} 


} 



Function CopyOra 
{ 
Try 
{ 

    $PathFolder = "\\partage\ORAFiles\Source" 
    $DestinationFolder = "c:\Apps\Oracle\11.2\network\admin\" 

    if (!(test-path $DestinationFolder)) 
    { 
     New-Item $DestinationFolder -ItemType Directory | Out-Null 
     writetolog "Created folder $DestinationFolder" 
    } 


    $LOGFilesFound = get-childitem $PathFolder -file | where Extension -eq ".ora" | Sort Time -descending 

    foreach ($LOGFile in $LOGFilesFound) 
    { 
     writetolog "processing files: $($_.Name)" 


     If (Test-Path "$DestinationFolder$LOGFile") 
     { 
      writetolog "fichier trouvé $DestinationFolder$LOGFile ... Copie de backup en cours." 

      $newFileName ="{0}-{1:dd-MM-yyyy HH-mm}{2}" -f $LOGFile.BaseName, (Get-Date), $LOGFile.Extension 

      copy-item "$DestinationFolder$LOGFile" "$DestinationFolder$newFileName" 

      writetolog "$DestinationFolder$newFileName créé." 

     } 
     else 
     { 
      write-host "$DestinationFolder$LOGFile non trouvé sur le poste" 
      writetolog "$DestinationFolder$newFileName non trouvé localement." 

      copy-item "$PathFolder\$LOGFile" "$DestinationFolder$LOGFile" 
      writetolog "Copie effectuée de $PathFolder\$LOGFile" 
     } 


    } 

} 

Catch 
{ 
     $ErrorMessage = $_.Exception.Message 
     $FailedItem = $_.Exception.ItemName 
     writetolog "Erreur détectée...Détail : $ErrorMessage, $FailedItem" 
     code_retour -1 
} 

} 

$ErrorActionPreference ='Stop' 

cls 

CopyOra 

code_retour 0 
+0

Hi和感謝您的答覆。 – derspinne

+0

似乎無法在註冊表中寫入。 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

+0

你現在對你的問題進行解釋。你不能寫入註冊表的這一部分 – Esperento57

相關問題