how to fix this error : Method invocation failed because | [Deserialized.Microsoft.IIs.PowerShell.Framework.ConfigurationElement#bindings#binding] does not contain a | method named 'AddSslCertificate'.

EL MEDIOUNI Sohaib 0 Reputation points
2024-04-17T15:12:53.5433333+00:00

Hello, I'm trying to automate the creation of the SSL certificate in the iis server i wrote a powershell that do this but i m getting this error :
Line |

1 | Import-Module -Name WebAdministration -Force -SkipEditionCheck

| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

| Could not load type 'System.Management.Automation.PSSnapIn' from assembly 'System.Management.Automation,

| Version=7.4.0.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

InvalidOperation: D:\Venafi\installpfx.ps1:21

Line |

21 | $binding.AddSslCertificate($thumbprint, "My")

| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

| Method invocation failed because

| [Deserialized.Microsoft.IIs.PowerShell.Framework.ConfigurationElement#bindings#binding] does not contain a

| method named 'AddSslCertificate'.

Restart-WebItem: A parameter cannot be found that matches parameter name 'Name'.

this is my powershell script :

Import-Module -Name WebAdministration -Force -SkipEditionCheck
$CertificatePassword = mymdp
$CertificateName = mycertname
$ServerName = myservername
$SiteName = mysitename
# Import PFX certificate
$certPassword = ConvertTo-SecureString -String $CertificatePassword -Force -AsPlainText
$cert = Import-PfxCertificate -FilePath "C:\$CertificateName.pfx" -CertStoreLocation Cert:\LocalMachine\My -Password $certPassword
# Get the Thumbprint of the imported certificate
$thumbprint = $cert.Thumbprint
# Create a new HTTPS binding for the website
New-WebBinding -Name $SiteName -IP "*" -Port 443 -Protocol https
# Set the certificate for use in the HTTPS binding
$binding = Get-WebBinding -Name $SiteName -Protocol https
if ($binding) {
    $binding.AddSslCertificate($thumbprint, "My")
  
    # Restart the website to apply the changes
    Restart-WebItem -Name $SiteName
} else {
    Write-Host "The website '$SiteName' does not exist in IIS or there was an issue with creating the HTTPS binding."
}
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,151 questions
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,372 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,065 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,811 Reputation points Microsoft Vendor
    2024-04-18T02:51:34.81+00:00

    Hi EL MEDIOUNI Sohaib,

    It seems the module WebAdministration cannot be imported normally in PowerShell 7. Try import it using the Windows PowerShell Compatibility feature.

    Import-Module -Name WebAdministration -UseWindowsPowerShell
    

    If it still doesn't work then you'll have to run the script in Windows PowerShell 5.

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments