Invoke-FixSkypeProfile.ps1


<#PSScriptInfo
 
.VERSION 1.0.0.0
 
.GUID 916e1133-bdb3-4fcf-b98e-bee27b5dcae0
 
.AUTHOR jassha@microsoft.com
 
.COMPANYNAME Microsoft Corporation
 
.COPYRIGHT 2017 Microsoft Corporation
 
.TAGS SkypeForBusiness
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
.DESCRIPTION
 Script used to remove all sip_ profiles from the currently logged on user's profile.
 
#>



function GetSkypeDirectory{
    [cmdletbinding()]
    Param()
    #need to check against Skype for Business 2015 and 2016
    begin{
        $office2013Path = $env:LOCALAPPDATA + '\Microsoft\Office\15.0\Lync'
        $office2016Path = $env:LOCALAPPDATA + '\Microsoft\Office\16.0\Lync'
    }
    process{
        try{
            if (Test-Path -Path $office2013Path){
                #found Office 2015 path
                Write-Verbose -Message "Found path for Skype for Business profile in $($office2013Path)"
                return $office2013Path
            }elseif (Test-Path -Path $office2016Path){
                #found Office 2016 path
                Write-Verbose -Message "Found path for Skype for Business profile in $($office2013Path)"
                return $office2016Path
            }else{
                #couldn't find either path
                Write-Warning -Message "Could not find a suitable path for Office 2013 or 2016 containing a Skype for Business profile."
            }
        }catch{
            throw;
        }
    }
    end{}
}
function ControlSkypeForBusiness{
    [cmdletbinding()]
    Param(
        [Parameter(mandatory=$true, valuefrompipeline=$false)]
        [ValidateSet("Shutdown","Startup")]
        [string]$Action
    )
    begin{}
    process{
        if ($Action -eq "Shutdown"){
            try{
                if ($result = Get-Process lync -ErrorAction SilentlyContinue){
                    #found it running, shut it down!
                    Write-Verbose -Message "Skype for Business is running."
                    do{
                        Write-Verbose -Message "Attempting to shut down Skype for Business."
                        Get-Process lync -ErrorAction SilentlyContinue | Stop-Process
                        Start-Sleep -Seconds 1
                        $counter ++
                    }until($result.HasExited -eq $true -or $counter -eq "30")
                }else{
                    Write-Verbose -Message "Skype for Business is not running."
                }
            }catch{
                throw;
            }
        }

        if ($Action -eq "Startup"){
            try{
                Write-Verbose -Message "Attempting to start Skype for Business."
                Start-Process lync
            }catch{
                throw;
            }
        }        
    }
    end{}

}
function RemoveSkypeProfile{
    [cmdletbinding()]
    Param(
        [Parameter(mandatory=$true, valuefrompipeline=$false)]
        [string]$Path
    )
    begin{
        $Path = $Path + "\sip_*"
    }
    process{
        try{
            Remove-Item -Path $Path -Recurse -Force -ErrorAction SilentlyContinue -ErrorVariable $errorMessage
            Write-Verbose -Message "Successfully removed Skype for Business profile."
        }catch{
            $_.Exception.Message
            throw;
        }
        if ($errorMessage){
            $errorMessage
            throw;
        }
    }
    end{}
}

$getResult = GetSkypeDirectory -Verbose
ControlSkypeForBusiness -Action Shutdown -Verbose
RemoveSkypeProfile -Path $getResult -Verbose
ControlSkypeForBusiness -Action Startup -Verbose