$cwd = (Get-Location).Path
$backupDir = $cwd


function backup {
	# For some reason the output of wsl is returned with null's between each char. STrip these out here.
	$dist = $args[0].Trim()
	$cs = $dist.ToCharArray() |where{$_ -ne 0} 
	$distro = $cs -join ""

	$file = Join-Path -Path $backupDir -ChildPath "$($distro.ToLower()).tar"

	if ($dist -eq '') {
		Write-Host "wsl --export '$distro' '$file'"
		# Start-Process -FilePath "powershell.exe" -ArgumentList "C:\Users\chris\Desktop\run.ps1 $distro $file" -Wait
		Start-Process -FilePath "wsl.exe" -ArgumentList "--export  $distro $file" -Wait
	}
}

$TempFile = New-TemporaryFile
$TempFile = "test.txt"
Start-Process -FilePath "wsl.exe" -ArgumentList "--list --quiet" -RedirectStandardOutput $TempFile -Wait
$distros =Get-Content -Path $TempFile
echo $distros

foreach ($distro in $distros) {
	backup($distro)
}