Hello all ^^,
I’m scripting something for change the scedule of a NetBackup Policy.
I’m using a windows 2016 server with Netbackup v 8.1.1
My problem is than 2 Global Variable seems to loose the value when the script change of function.
The variables is $Global:PolicyName and $Global:StopTime.
They take teir value in the GetInfoSched Function, and when i leave this function the variable is $null.
#Déclaration des path utilisés $PathConfig = "C:\Admin\ConfigPolicies" $PathNBUcmd = "C:\Program Files\Veritas\NetBackup\bin" $PathNBUadmincmd = "C:\Program Files\Veritas\NetBackup\bin\admincmd" $PathResult = "c:\Admin\Result" #Déclaration des Variables Globales $Global:Startime = $null $Global:StopTime = $null $Global:ChoixMenu = $null $Global:Boucle = $null $Global:ScheduleLabel = $null $Global:ChoixSched = $null $Global:PolicyName $Global:Jour = $null Set-Location $PathNBUadmincmd #Faire un Backup de la configuration des schedules de ttes les policies #Menu des Opérations disponibles Function Menu(){ Write-Host "Opération disponible :" Write-Host "1. Changer le jour et heure d'un Schedule d'une Policy" Write-Host "2. Remettre les schedules a l'origine" $Global:ChoixMenu = Read-Host "Selectioner l'operation souhaité" } $Var = "PolicyName" Function GetInfosSch(){ T("Entrer GetInfoSch", "PolicyName", $PolicyName) $CritereSelection = Read-Host "Donner votre critere de sélection" $ListPN = .\bppllist.exe -allpolicies -U | Find /I "Policy Name" ForEach($Policy in $ListPN){ $PolicyName = ($Policy.split(":")[1]).replace(" ","") | Where{$_ -like "*$CritereSelection*"} } #$Start = Read-Host "Date et Heure de début de sauvegarde, ex: mm/jj/YYYY 10:30" #$Stop = Read-Host "Date et Heure de fin, ex: mm/jj/YYYY 11:30" $Start = "02/04/2020 16:00" $Stop = "02/04/2020 16:30" $Jour = (Get-Date -Date $Start.split(" ")[0] -UFormat "%A").Replace(" ","") $StartTime = New-TimeSpan -Start ($Start.Split(" ")[0]) -End $Start $StartTime = $StartTime.TotalSeconds $tdiff = New-TimeSpan -Start $Start -End $Stop $StopTime = $tdiff.TotalSeconds #Recuperation des informations des schedules de la Policy $Schedules = .\bpplsched.exe RGS_MGMT_P -U | Where{$_ -like "*Schedule*"} #Choix et Récuperation des PolicyName et ScheduleLabel $Sched1 = ($Schedules[0].split(":")[1]).replace(" ","") $Sched2 = ($Schedules[1].split(":")[1]).replace(" ","") Write-Host "1. $Sched1" Write-Host "2. $Sched2" $ChoixSched = Read-Host "Sélectionner le schedule a modifier" Switch($ChoixSched){ 1{$Global:ScheduleLabel = $Sched1} 2{$Global:ScheduleLabel = $Sched2} } Write-Host "#============================================================================# " Write-Host "Récapitulations des informations avant modifications des Schedules:" Write-Host "Critere de sélection: $CritereSelection" Write-Host "PolicyName: $PolicyName" Write-Host "Schedule Label: $ScheduleLabel" Write-Host "Jour: $Jour" Write-Host "Date et Heure start backup: $Start" Write-Host "Date et Heure stop backup: $Stop" Write-Host "StartTime en seconde: $StartTime" Write-Host "StopTime en seconde: $StopTime" $Global:Boucle = Read-Host "Si tout est ok, Yes ou Non" T("Sortie GetInfoSch","PolicyName",$PolicyName) } #A revoir ! Function BackupSched(){ T("Entrée Backup","PolicyName",$PolicyName) Set-Location $PathNBUadmincmd Write-Host "Passe dans la fonction BackupSched" $ListPN = .\bppllist.exe -allpolicies -U | Find /I "Policy Name" Write-Host "$ListPN" $Date = Get-Date -UFormat "-%d-%m-%Y" $TP = Test-Path -Path "$PathConfig\AllPolicies.txt" If($TP -eq "*True*"){Remove-Item "$PathConfig\AllPolicies.txt"} ForEach($Policy in $ListPN){ $Policy = ($Policy.split(":")[1]).replace(" ","") Write-Host "Policy: $policy" $PolicyList = $Policy | Out-File "$PathConfig\AllPolicies.txt" -Append .\bpplsched.exe $Policy -l | Out-File "$PathConfig\Config-$Policy$Date.txt" } Write-Host "Policy: $Policy" Write-Host "Fin de la fonction Backup" T("Sortie Backup","PolicyName",$PolicyName) } # A Verifier !!! Function ModificationSched(){ T("Entrée ModifSched","PolicyName",$PolicyName) Write-Host "Début de la fonction Modification" Write-Host "PolicyName: $Global:PolicyName" Write-Host "ScheduleLabel: $ScheduleLabel" Write-Host "StartTime: $StartTime" Write-Host "StopTime: $StopTime" Switch($Jour){ Dimanche{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 $StartTime $StopTime -1 0 0 -2 0 0 -3 0 0 -4 0 0 -5 0 0 -6 0 0} Lundi{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 0 0 -1 $StartTime $StopTime -2 0 0 -3 0 0 -4 0 0 -5 0 0 -6 0 0} Mardi{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 0 0 -1 0 0 -2 $StartTime $StopTime -3 0 0 -4 0 0 -5 0 0 -6 0 0} Mercredi{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 0 0 -1 0 0 -2 0 0 -3 $StartTime $StopTime -4 0 0 -5 0 0 -6 0 0} Jeudi{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 0 0 -1 0 0 -2 0 0 -3 0 0 -4 $StartTime $StopTime -5 0 0 -6 0 0} Vendredi{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 0 0 -1 0 0 -2 0 0 -3 0 0 -4 0 0 -5 $StartTime $StopTime -6 0 0} Samedi{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 0 0 -1 0 0 -2 0 0 -3 0 0 -4 0 0 -5 0 0 -6 $StartTime $StopTime} } Write-Host "Modification éffectué" T("Sortie ModifSched","PolicyName",$PolicyName) } Function RollBack(){ $PolicyList = Get-Content "$PathConfig\AllPolicies.txt" ForEach($Policy in $PolicyList){ $ConfigRef = Get-Content "$PathConfig\Config-$Policy*.txt" $ConfigDiff = .\bpplsched.exe $Policy -l $PolicyName = $Policy If(Compare-Object -ReferenceObject $ConfigRef -DifferenceObject $ConfigDiff){ Write-Host "Les fichiers sont Differents" $Diff = Compare-Object -ReferenceObject $ConfigRef -DifferenceObject $ConfigDiff $DiffCsv = $Diff | Export-Csv "$PathConfig\config.csv" -Delimiter "," $Diff = Import-Csv "$PathConfig\config.csv" -Delimiter "," $Conf = $Diff[1].InputObject $i=0 $TabVal = @() $TabPos = @() While($i -le 14){ If($Diff.split(" ")[$i] -notlike "0"){ $Val = $Diff.split(" ")[$i] $TabVal += $Val $TabPos += $i } $i++ } #Rechercher le schedule Label qui a été modifié Switch($TabPos[1]){ 1{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 $StartTime $StopTime -1 0 0 -2 0 0 -3 0 0 -4 0 0 -5 0 0 -6 0 0} 3{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 0 0 -1 $StartTime $StopTime -2 0 0 -3 0 0 -4 0 0 -5 0 0 -6 0 0} 5{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 0 0 -1 0 0 -2 $StartTime $StopTime -3 0 0 -4 0 0 -5 0 0 -6 0 0} 7{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 0 0 -1 0 0 -2 0 0 -3 $StartTime $StopTime -4 0 0 -5 0 0 -6 0 0} 9{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 0 0 -1 0 0 -2 0 0 -3 0 0 -4 $StartTime $StopTime -5 0 0 -6 0 0} 11{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 0 0 -1 0 0 -2 0 0 -3 0 0 -4 0 0 -5 $StartTime $StopTime -6 0 0} 13{.\bpplschedwin $PolicyName $Global:SheduleLabel -0 0 0 -1 0 0 -2 0 0 -3 0 0 -4 0 0 -5 0 0 -6 $StartTime $StopTime} } } Else{ Write-Host "Les fichiers sont identiques" } } } Function T([string]$orig, [string]$var, [string]$val){ Write-Host "#####################################################" Write-Host "Oigine: $Orig" Write-Host "Entrer dans la fonction T" Write-Host "$var : $val" Write-Host "PolicyName: $Global:PolicyName" Write-Host "ScheduleLabel: $ScheduleLabel" Write-Host "StartTime: $StartTime" Write-Host "StopTime: $StopTime" Write-Host "#####################################################" } ###############Deroulement du Script################## do{ Menu GetInfosSch } Until($Global:Boucle -like "*Yes*") if ($Global:ChoixMenu -eq 1){ BackupSched ModificationSched } Else { RollBack } Set-Location "C:\Admin"
Thx for help.