Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Central Management - Service - Error during installation when providing existing certificate: Cannot index into a null array #143

Closed
2Deep2Dive opened this issue May 21, 2020 · 4 comments
Assignees
Labels
5 - Released The area addressed in the ticket has been released in the product and is generally available. Bug Tickets that represent defects/bugs. CentralManagement Issues affecting Central Management Edition - Business

Comments

@2Deep2Dive
Copy link

2Deep2Dive commented May 21, 2020

Hi Everyone,

There is an error in the code which throws this Cannot index into a null array
the issues is produced when starting the installation while providing an existing certificate thumbprint

Here the thumbprint is checked and the value is assigned to the variable, the issue is with the else part, at this point the $existingCertificatesCoun is null since there was no values assigned to it, then a switch is called checking the value of this variable, in this case the switch fails since the value is null

I believe the Else should include the switch for the logic to function properly

If ($pp['CertificateThumbprint']) {
$certificateHash = $pp['CertificateThumbprint']
} Else {
$existingCertificates = (Get-ChildItem -Path Cert:\LocalMachine\TrustedPeople, Cert:\LocalMachine\My | Where-Object { $_.Subject -like "CN=$certificateDnsName*" } | Sort-Object -Property NotBefore | Select-Object *)
$existingCertificatesCount = ($existingCertificates | Measure-Object).Count
}
Switch ($existingCertificatesCount) {
0 {
Write-Output "Creating self signed certificate"

# you can only generate a new certificate in 'My'
# necessary to branch based on PowerShell version, since not all parameters are supported in earlier versions
If($PSVersionTable.PSVersion.Major -le 4) {
  $newCert = New-SelfSignedCertificate -CertStoreLocation cert:\LocalMachine\My -DnsName $certificateDnsName
} Else {
  $newCert = New-SelfSignedCertificate -CertStoreLocation cert:\LocalMachine\My -DnsName $certificateDnsName -KeyUsage KeyEncipherment,DigitalSignature -NotAfter (Get-Date).AddYears(10)
}
# move the certificate to 'TrustedPeople'
$certPath = Get-ChildItem -Path 'Cert:\\LocalMachine\\My' | Where-Object subject -like "*$certificateDnsName"
$null = Move-Item -Path $certPath.PsPath -Destination 'Cert:\\LocalMachine\\TrustedPeople'
$certificateHash = $newCert.Thumbprint

}
1 {
Write-Warning "A single self-signed Certificate matching Subject $certificateDnsName was found. Using this to continue installation"
$certificateHash = $existingCertificates[0].Thumbprint
}
default {
Write-Warning "Multiple self-signed Certificates were found matching Subject $certificateDnsName. Using the oldest Certificate to continue installation."
$certificateHash = $existingCertificates[0].Thumbprint
Write-Warning "Using $certificateHash"
}
}

Cheers,
Mohamed

References

@robweber
Copy link

I had this some problem, I can confirm that putting the switch statement within the ELSE block as suggested will fix it.

@ferventcoder ferventcoder transferred this issue from chocolatey/choco-wiki May 23, 2020
@ferventcoder ferventcoder changed the title ERROR: Cannot index into a null array caused by error on the chocolateyinstall.ps1 CentralManagement - Service installation - ERROR: Cannot index into a null array caused by error on the chocolateyinstall.ps1 May 23, 2020
@ferventcoder
Copy link
Member

Howdy folks, just moved this over to #143 as it is not a problem with the wiki. Updated the issue name as well. Thanks for finding this.

@steviecoaster
Copy link

Internal Issue

@ferventcoder ferventcoder added the CentralManagement Issues affecting Central Management label May 24, 2020
@ferventcoder ferventcoder added 2 - Working Tickets that are currently being worked on. Bug Tickets that represent defects/bugs. Edition - Business labels May 24, 2020
@ferventcoder ferventcoder changed the title CentralManagement - Service installation - ERROR: Cannot index into a null array caused by error on the chocolateyinstall.ps1 Central Management - Service installation - ERROR: Cannot index into a null array caused by error on the chocolateyinstall.ps1 May 24, 2020
@ferventcoder ferventcoder changed the title Central Management - Service installation - ERROR: Cannot index into a null array caused by error on the chocolateyinstall.ps1 Central Management - Service installation - error when providing existing certificate: Cannot index into a null array May 26, 2020
@ferventcoder ferventcoder added 1 - Ready Tickets that are on deck/assigned. All ready to go. and removed 2 - Working Tickets that are currently being worked on. labels May 26, 2020
@ferventcoder
Copy link
Member

This has been fixed for Chocolatey Central Management v0.2.0. Thanks for letting us know about the issue!

@ferventcoder ferventcoder added 4 - Done Issues that have been completed and are ready to be released. and removed 1 - Ready Tickets that are on deck/assigned. All ready to go. labels May 26, 2020
@gep13 gep13 changed the title Central Management - Service installation - error when providing existing certificate: Cannot index into a null array Central Management - Service - Error during installation when providing existing certificate: Cannot index into a null array May 28, 2020
@ferventcoder ferventcoder added 5 - Released The area addressed in the ticket has been released in the product and is generally available. and removed 4 - Done Issues that have been completed and are ready to be released. labels Jun 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5 - Released The area addressed in the ticket has been released in the product and is generally available. Bug Tickets that represent defects/bugs. CentralManagement Issues affecting Central Management Edition - Business
Projects
None yet
Development

No branches or pull requests

4 participants