function SetExternalSharingForSites($csvrows, $adminUrl, $credentials, $logFile)
{
#Connect to Admin site and set sharing properties for all sites in CSV List
Connect-SPOService -Url $adminUrl -Credential $credentials;
foreach($row in $csvrows)
{
if ($row.RequestType -eq ‘Adhoc’ -or $row.RequestType -eq ‘Extranet’)
{

        try
        {
            if ($sitesWithErrors.Contains($row.Url) -eq $false)
            {
                WriteToLog -logFile $logFile -text ("Attempting to set External Sharing for " + $row.url) -ForegroundColor Yellow
                Set-SPOSite -Identity $row.url -SharingCapability ExistingExternalUserSharingOnly
                Set-SPOSite -Identity $row.Url -DisableSharingForNonOwners
                WriteToLog -logFile $logFile -text ("External Sharing enabled for " + $row.url) -ForegroundColor Green
            }
        }
        catch
        {
            WriteToLog -logFile $logFile -text ("Could not enable External Sharing for " + $row.url + '. ' + $Error[0].Exception.GetBaseException()) -ForegroundColor Red
            $sitesWithErrors.Add($row.Url,"Could not enable External Sharing");
        }
    }
}
Disconnect-SPOService

}