Export-CMBaseline : Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

TOBSII 0 Reputation points
2023-11-08T15:49:47.8066667+00:00

I have an issue. I have a big script, which searches specific WmiObjects, saves them with the name and the folder in an array, which will be exported later. Everything works, except for Baselines. When I try to export Baselines, i get the error in the tilte. It does export some of them, but not everything (f.e the ones in the screenshots).example

reference

Is there a way to change the Indexing or is there a different problem? I know that my code is not beautiful, not easy to read and that this is very specific but I am really stuck and could use a lot of help since we want this script for our backup solution.

Thank you :)

#Configuration Baselines - local Path     
$DiskPathConfBaselines = "BAS:\ConfigurationBaseline"        

#Configuration Baseline - export Path    
$RootPathBaselineExport = "C:\Temp\ConfigManager-Dump\Configuration Baselines"



#Configuration Baselines
        $Script:ExportArrayConfBaselines = @()

        function Get-Baselines {
            param (
                [string]$rootPath,
                [string]$subfolderPath = ""

            )
    
            # Get the list of subfolders in the current directory
            $currentPath = Join-Path -Path $rootPath -ChildPath $subfolderPath

            If($currentPath[$currentPath.Length -1] -eq "\"){
                $currentPath = $currentPath.TrimEnd($currentPath[-1])
            }

            $subfolders = (Get-ChildItem -Path $currentPath).name

            # Loop through each subfolder
            foreach ($folder in $subfolders) {
        
                $query = ("$subfolderPath\$folder").Replace("\", "/")
                $Script:ExportArrayConfBaselines += Get-CMBaseline -Fast |Where-Object {$_.ObjectPath -Like "*$query*"} | Select-Object LocalizedDisplayName, @{Name='subfolder';Expression={("$subfolderPath\$folder")}}


                Get-Baselines -rootPath $rootPath -subfolderPath "$subfolderPath\$($folder)"
            }
        }

        


        #Configuration Baselines        
function Export-Baselines {         
param (
       [string]$RootPath         
	)                             
	 foreach($BaselineName in $ExportArrayConfBaselines){                        			 			
		$BaselineNameClean = $BaselineName.LocalizedDisplayName -Replace '[^\w\s]', ' '                   

		$NewPath = Join-path -Path $RootPath -ChildPath $BaselineName.subfolder                                     			 

	if(Test-Path  $NewPath){                                 
 		if ($BaselineName.subfolder -eq "\"){                    
			Export-CMBaseline -Name $BaselineName.LocalizedDisplayName -Path "$($NewPath)$($BaselineNameClean).cab"                                       
		}else{                    
 			Export-CMBaseline -Name $BaselineName.LocalizedDisplayName -Path "$($NewPath)\$($BaselineNameClean).cab"                       }                  
	}else{                 
		New-Item -Path $NewPath -ItemType Directory                              
		if ($BaselineName.subfolder -eq "\"){                    
 			Export-CMBaseline -Name $BaselineName.LocalizedDisplayName -Path "$($NewPath)$($BaselineNameClean).cab"                       
		}else{                     
			Export-CMBaseline -Name $BaselineName.LocalizedDisplayName -Path "$($NewPath)\$($BaselineNameClean).cab"                      
			}                 
		}
	}
}     
       
 Export-Baselines -RootPath $RootPathBaselineExport     
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,605 questions
Microsoft Configuration Manager
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. XinGuo-MSFT 20,566 Reputation points
    2023-11-09T08:00:04.9566667+00:00

    Hi,

    Please try using the simple script below to see if you can back up all the baselines.

    #Run these commands on Site A and copy the files.
    $Baselines = Get-CMBaseline
    Foreach($Baseline in $Baselines){
    
        Export-CMBaseline -Id $Baseline.CI_ID -Path "C:\TEMP\$($Baseline.LocalizedDisplayName).cab"
    
    }
    
    #Run this on Site B to import all Baselines
    $Files = Get-ChildItem -Path "C:\TEMP\"
    foreach($CAB in $Files){
    
        $CAB.FullName
        Import-CMBaseline -FileName $CAB.FullName -Verbose -Force
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.