Uninstall Forticlient any version through Intune

This is the first script, so I then wanted to modify this so that I can write any errors in a text file to log it so can look into the log and see what is causing the issue, but when I run the below script it give me error shown below.
The uninstall worked fine with my script but it is showing as Error in Intune Overview

$forticlient = Get-WmiObject Win32_Product | Where-Object {$_.Name -like "*FortiClient*"}
msiexec /uninstall $forticlient.IdentifyingNumber /quiet /norestart
# Check for administrator rights
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Host "Please run this script as an administrator."
    exit
}

# Define the name of the FortiClient executable
$fortiClientExecutable = "FortiSSLVPNClient.exe"

# Get the list of installed FortiClient versions
$fortiClientVersions = Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* -ErrorAction SilentlyContinue |
    Where-Object { $_.DisplayName -like "FortiClient VPN*" } |
    ForEach-Object { $_.DisplayName }

# Uninstall FortiClient versions
foreach ($version in $fortiClientVersions) {
    $uninstallArgs = "/x $($_.PSChildName) /quiet /norestart"
    Write-Host "Uninstalling $version with arguments: $uninstallArgs"
    
    try {
        # Attempt to uninstall FortiClient
        Start-Process -FilePath "msiexec.exe" -ArgumentList $uninstallArgs -Wait -ErrorAction Stop
    }
    catch {
        # Capture error details and write to a text file
        $errorMessage = "Error uninstalling $version:`n$($_.Exception.Message)"
        $errorFilePath = "C:\temp\ForticlientError.txt"
        Add-Content -Path $errorFilePath -Value $errorMessage

        Write-Host "Error details written to $errorFilePath"
    }
}

Write-Host "FortiClient VPN Client uninstallation completed."

Get the following error - How can I fix it?

At line:26 char:45

  •     $errorMessage = "Error uninstalling $version:`n$($_.Exception ...
    
  •                                         ~~~~~~~~~
    

Variable reference is not valid. ‘:’ was not followed by a valid variable name character. Consider using ${} to delimit the name.
+ CategoryInfo : ParserError: (:slight_smile: , ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvalidVariableReferenceWithDrive

$errorMessage = "Error uninstalling $($version):`n$($_.Exception.Message)"

And again …

  1. You should not use Get-WmiObject anymore as it is depricated. Use Get-CimInstance instead.
  2. Using the Win32_Product class is usually a bad idea and might not do what you think it does or sometimes it does even more than you want.
    https://xkln.net/blog/please-stop-using-win32product-to-find-installed-software-alternatives-inside/

You’re using the pipeline variable $_ but you actually don’t have a pipeline.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.