How to enable DisallowInfectedFileDownload in SharePoint Online without getting an InvalidCastException error

Question:

How can I resolve the InvalidCastException error when using the Set-SPOTenant cmdlet to enable the DisallowInfectedFileDownload parameter in SharePoint Online?

I am following the Microsoft guide to configure safe attachments for SharePoint Online, OneDrive for Business, and Microsoft Teams. I want to prevent users from downloading malicious files from SharePoint Online by using the PowerShell command:

“`powershell

Set-SPOTenant -DisallowInfectedFileDownload $true

“`

However, when I run this command, I get the following error:

“`powershell

Set-SPOTenant : Unable to cast object of type ‘System.String’ to type

‘Microsoft.Online.SharePoint.TenantAdministration.EsignatureThirdPartyProvidersInfo’.

At line:1 char:1

+

Set-SPOTenant -DisallowInfectedFileDownload $true

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Set-SPOTenant], InvalidCastException + FullyQualifiedErrorId : System.InvalidCastException,Microsoft.Online.SharePoint.PowerShell.SetTenant “`

I have searched online for a solution, but I could not find any. One user suggested that I need to download an older version of the Microsoft.Online.SharePoint.PowerShell module, but I am not sure if that is the best practice. Is there a way to fix this error without downgrading the module? If not, which version of the module should I use and how can I install it?

Answer:

How to fix the InvalidCastException error when using Set-SPOTenant to enable DisallowInfectedFileDownload in SharePoint Online

If you are following the [Microsoft guide] to configure safe attachments for SharePoint Online, OneDrive for Business, and Microsoft Teams, you may encounter an error when you try to enable the DisallowInfectedFileDownload parameter using the Set-SPOTenant cmdlet. The error message looks like this:

“`powershell

Set-SPOTenant : Unable to cast object of type ‘System.String’ to type

‘Microsoft.Online.SharePoint.TenantAdministration.EsignatureThirdPartyProvidersInfo’.

At line:1 char:1

+ Set-SPOTenant -DisallowInfectedFileDownload $true + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Set-SPOTenant], InvalidCastException + FullyQualifiedErrorId : System.InvalidCastException,Microsoft.Online.SharePoint.PowerShell.SetTenant “`

This error occurs because the Microsoft.Online.SharePoint.PowerShell module has a bug that prevents it from correctly parsing the DisallowInfectedFileDownload parameter. This bug was introduced in version 16.0.21513.12000 of the module, which was released on February 9, 2024. The previous version of the module, 16.0.21411.12000, does not have this bug and can successfully run the Set-SPOTenant cmdlet with the DisallowInfectedFileDownload parameter.

To fix this error, you have two options:

  • Option 1: Wait for Microsoft to release a new version of the module that fixes the bug. You can check the [release notes] of the module to see if a new version is available and what changes it includes. You can also contact Microsoft support to report the bug and request a resolution.
  • Option 2: Downgrade the module to the previous version that works. To do this, you need to uninstall the current version of the module and install the older version. You can use the following PowerShell commands to perform this task:
  • “`powershell

    Uninstall the current version of the module

    Uninstall-Module -Name Microsoft.Online.SharePoint.PowerShell -RequiredVersion 16.0.21513.12000 -Force

    Install the previous version of the module

    Install-Module -Name Microsoft.Online.SharePoint.PowerShell -RequiredVersion 16.0.21411.12000 -Force

    “`

    After you downgrade the module, you can run the Set-SPOTenant cmdlet with the DisallowInfectedFileDownload parameter without any errors. However, you should be aware that downgrading the module may also remove some features or fixes that were introduced in the newer version. You should review the [release notes] of the module to see what changes you may be missing.

    We

hope this article helps you resolve the InvalidCastException error when using Set-SPOTenant to enable DisallowInfectedFileDownload in SharePoint Online. If you have any questions or feedback, please leave a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *

Privacy Terms Contacts About Us