I am fetching files from a particular folder by passing a version number of that files, but if I do not pass the version(hit enter) the values goes null and the code not execute.
In my code when I do not pass the version(hit enter), it fetches all the files from that folder which is incorrect.
If I pass null
value to variable $basepkg
the code should not execute if($basepkg -ne $null -And $pkgtype -eq 'A')
this line of code but it does.
Write-Host "Removing existing files"
Remove-Item -path F:\temp\Packages\* -Recurse -Force
$pkg = Read-Host 'Input the package version'
$basepkg = Read-Host 'Provide the Policy Base Package version' #Passing null value here
$pkgtype = Read-Host 'Please specify the package type - A OR B'
if($basepkg -ne $null -And $pkgtype -eq 'A')
{
Get-ChildItem -Path "F:\temp1\Packages" -Recurse | Where-Object { $_.Name -match $basepkg } | Where-Object { $_.Name -match '`*basetemplate*'} | Where-Object { $_.Name -match '`*-A*'}| Copy-Item -Destination "F:\temp\Packages" -Force
}
elseif($basepkg -ne $null -And $pkgtype -eq 'B')
{
Get-ChildItem -Path "F:\temp1\Packages" -Recurse | Where-Object { $_.Name -match $basepkg } | Where-Object { $_.Name -match '`*basetemplate*'} |Where-Object { $_.Name -match '`*-B*'} | Copy-Item -Destination "F:\temp\Packages" -Force
}
else
{
Break
}
if($pkgtype -eq 'A')
{
Write-Host "Extracting files"
Get-ChildItem -Path "F:\temp1\Packages" -Recurse | Where-Object { $_.Name -match $pkg } | Where-Object { $_.Name -match 'policy*'} | Where-Object { $_.Name -match '`*-A*'} | Copy-Item -Destination "F:\temp\Packages" -Force
}
elseif($pkgtype -eq 'B')
{
Write-Host "Extracting files"
Get-ChildItem -Path "F:\temp1\Packages" -Recurse | Where-Object { $_.Name -match $pkg } | Where-Object { $_.Name -match 'policy*'} | Where-Object { $_.Name -match '`*-B*'} | Copy-Item -Destination "F:\temp\Packages" -Force
}
else
{
Write-Host "Please select the correct package type by re-running the script"
Start-Sleep -s 5
exit
}
$pkgCount = Get-ChildItem -Path "F:\temp\Packages" -Recurse -File | Measure-Object | %{$_.Count}
if($pkgCount -eq $null)
{
Write-Host "Package not found, please confirm the package version"
Start-Sleep -s 5
exit
}
Write-Host "Files Extracted"
Read-Host
does not return $null
but ''
(empty string) when you press enter. To check for both use [string]::IsNullOrEmpty($basepkg)
:
if(-not [string]::IsNullOrEmpty($basepkg) -And $pkgtype -eq 'A'){...}