Category Archives: Microsoft

SQL How to empty all tables in a database

You might need to purge all the data in all the tables in a particular database. For this we can use the following script.

USE <database name>
DECLARE @TableName AS VARCHAR(MAX)
DECLARE table_cursor CURSOR
FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND TABLE_NAME LIKE '%_Partition%'
OPEN table_cursor
FETCH NEXT FROM table_cursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @SQLText AS NVARCHAR(4000)
SET @SQLText = 'TRUNCATE TABLE ' + @TableName
EXEC sp_executeSQL @SQLText
FETCH NEXT FROM table_cursor INTO @TableName
END
CLOSE table_cursor
DEALLOCATE table_cursor

(6)

Fix: Storage Replica warning Events 10448 Storage Replica has failed an application IO

When checking the logs of the Storage Replica on a Windows Server 2016, 2019 or 2022, using the below command, you get the error saying that Storage Replica has failed an application IO.

Get-WinEvent -ProviderName Microsoft-Windows-StorageReplica -max 20

In this case, the replication is not working and there are no other issues pointing to what could be the problem. In my case the solution was to restart the Storage Replica service from the services.msc. After this, I ran the Get-WinEvent command again and it was replicating again.

 

(3)

Server Manager refresh failed error

When opening Server Manager on the server, in the notifications you will see an error stating Refresh Failed.

At this stage, you will not be able to add any features to the server and you will not be able to add or remove windows features.

A pop-up about collecting inventory data and a refresh error saying “Configuration refresh failed with the following error: Invalid class” will show.

Before proceeding you can take a backup of this by using the command below
winmgmt /backup %computername%_MM_DD_YEAR.WMI_backup

To fix the problem you would need to do the following in a command prompt as Administrator.

winmgmt /verifyrepository
winmgmt /salvagerepository
winmgmt /resetrepository

This will resolve the problem and when you relaunch the Server Manager, you will be able to add the features with no issues.

(50)

Install Module with PowerShell: NuGet Unable to download, check your internet connection

While installing the NuGet PowerShell module, you will get the following error

WARNING: MSG:UnableToDownload «https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409» «»
WARNING: Unable to download the list of available providers. Check your internet connection.
WARNING: Unable to download from URI 'https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409' to ''.
Install-PackageProvider : No match was found for the specified search criteria for the provider 'NuGet'. The package provider requires 'PackageManagement' and 'Provider' tags. Please check if the specified package has the tags.
At line:1 char:1
+ Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Power...PackageProvider:InstallPackageProvider) [Install-PackageProvider], Exception
+ FullyQualifiedErrorId : NoMatchFoundForProvider,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackageProvider

To resolve the problem open a PowerShell window as Administrator and enter the following command.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

This will resolve the problem temporarily as if you would run the command again, it will still prompt the same problem. To resolve the issue permanently, you need to update the registry by running the below command to update the registry.

Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord

Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord

Close all PowerShell windows and try again with the following command to confirm that the protocol has been updated.

[Net.ServicePointManager]::SecurityProtocol

(21)

Search and Restore file from SharePoint Online using PowerShell

When having SharePoint Online, one can easily recover items from the Recycle Bin, but unfortunately, there is no search facility for the Recycle Bin from the web interface. We can use PowerShell to easily search for the item.

To be able to connect, we need to install the new module, Open a PowerShell window and use the below to install the module.

Install-Module -Name PnP.PowerShell

If you would have an older version already installed, you need to use the below command to remove the old module, and then install the module as above.

Uninstall-Module -Name SharePointPnPPowerShellOnline -AllVersions -Force

To connect with the SharePoint Online site, we need to use the following command which will ask us to allow the script to communicate along with the Multi-Function Authentication (MFA).

Connect-PnPOnline -Url "Enter the SharePoint URL here" -Interactive

To extract the specified SharePoint Online Recycle Bin, you need to use the following command to output to the screen. Change the RowLimit to set the maximum results.

Get-PnPRecycleBinItem -RowLimit 50 | Select Title, ItemType, Size, ItemState, DirName, DeletedByName, DeletedDate | Format-table -AutoSize

To export the results to a CSV, you need to add the Export-CSV at the end as below

Get-PnPRecycleBinItem -RowLimit 50 | Select Title, ItemType, Size, ItemState, DirName, DeletedByName, DeletedDate | Export-Csv "C:\Temp\RecycleBin.csv" -NoTypeInformation

To restore a specific file, you cannot just specify the name, as you need to get the item information. We need to use the following commands

$itemtorestore=Get-PnPRecycleBinItem | ? -Property Title -EQ "filename of the deleted file"
Restore-PnpRecycleBinItem -Identity $itemtorestore -Force

This will restore the file to its original location.

Swish

(8)

Fix: Rule Not clustered or the cluster service is up and online error during update of standalone SQL server

When updating your SQL server (Standalone) with a Cumulative Update (CU) you might check the error in the Rule Check Result.

Rule "Not clustered or the cluster service is up and online." failed.

The machine is clustered, but the cluster is not online or cannot be access from one of its nodes. To continue determine why the cluster is not online and rerun setup instead of rerunning the rule since the rule can no longer detect a cluster environment correctly.

This is a very strange message, especially since the SQL server is a stand alone server and there is no cluster. After a restart of the server and re-trying the update, the problems still persists.

After a lot of investigation, this issue is nothing related to SQL, but WMI. This can be verified by opening the WMI Management using the command WMIMGMT.MSC, right-click on WMI Control (Local) and click on Properties.

If your entries show Invalid Class, as the above screenshot, we need to re-compile the MOF files using the following command from a command prompt as Administrator.

mofcomp C:\Windows\System32\WBEM\cimwin32.mof

The result should be as below

After this, if you rerun the WMIMGMT.MSC and the same process, the WMI should look like this.

You can now re-run the SQL update and it will update with no issues, thus solving the problem permanently.

(785)

Fix: Cannot install July updates KB5004298 and KB5004285

You may have an issue with installing the KB5004298 and KB5004285 on your system. At first, the updates would install successfully, but when you reboot you will get an error saying “Failure configuring Windows Updates. Reverting Changes”

There will not be any error on your Windows machine, only a generic message that the update failed with the error message Code 800F0922 in the Windows Update History.

The error is due to an Acrobat Flash removal issue and the update will fail since it couldn’t find the folders. To resolve the issue, you need to create the following folders with the same case.

C:\Windows\System32\Macromed\Flash
C:\Windows\SysWOW64\Macromed\Flash

Once the above empty folders are created, simply re-run the updates and after a restart, they will be successful. Voila!

 

(687)

Fix: Missing Sysvol and Netlogon after domain controller promotion

Many cases I found an issue with the newly promoted domain controller is missing the SYSVOL and NETLOGON shares. Most of the cases it would also be a new domain controller for a new forest. In most cases, you would need to update the flag as below.

Open Regedit
Browse to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Netlogon\Parameters
Set SysVolReady from 0 to 1
Close Regedit

This will create the SYSVOL share. If the NETLOGON share is not created you would need to create the folder scripts in C:\Windows\SYSVOL\domain\. When this is done, restart the NETLOGON service.

This is the easy part. In some cases, although the NETLOGON and SYSVOL shares are working, no group policies or scripts are being replicated using the DFS or DFRS.

We can verify the replication by running the following command.

For /f %i IN ('dsquery server -o rdn') do @echo %i && @wmic /node:"%i" /namespace:\\root\microsoftdfs path dfsrreplicatedfolderinfo WHERE replicatedfoldername='SYSVOL share' get replicationgroupname,replicatedfoldername,state

The states should translate as below

0 = Uninitialized
1 = Initialized
2 = Initial Sync
3 = Auto Recovery
4 = Normal
5 = In Error

In my case, I have noticed that the newly promoted server was showing 2 and the main domain controller was showing “No Instance(s) Available” which is quite strange.

Here you would need to look into the original Active Directory server for any problems and you would see a warning on the DFS Replication under Applications with Event ID 2213 as below.

It says that the DFS Replication service stopped replication on volume C:. This occurs when a DFSR JET database is not shut down cleanly and Auto Recovery is disabled.

What we need to do here is from the event viewer take note of the volumeGUID and run the below command and replacing GUID-NUMBER with your GUID.

wmic /namespace:\\root\microsoftdfs path dfsrVolumeConfig where volumeGuid="GUID-NUMBER" call ResumeReplication

This will restart the replication and recreate the database. This can be seen with an event with ID 2214 saying The DFS Replication service successfully recovered from an unexpected shutdown on volume C:.This can occur if the service terminated abnormally (due to a power loss, for example) or an error occurred on the volume. No user action is required.

If you run the command to see the state of the replication you will see that the servers are all showing state 4 as below and the both Sysvol and Netlogon will be replicated.

(42009)

Fix: SharePoint error Cannot generate SSPI context for SQL Server

When having a SharePoint setup you might get the following error in your event viewer.

Event ID 5586 – Unknown SQL Exception 0 occurred. Additional error information from SQL Server is included below. The target principal name is incorrect.  Cannot generate SSPI context.

The main culprit could be the lack of an SPN record. To do this, log into your SQL server as a domain administrator. Launch a command prompt as Administrator and type the following.

setspn -L Domain\UserName

Note: Replace Domain\UserName with the SharePoint user you are using for the services. If you are getting the ‘Cannot generate SSPI context’ you should not see an entry for the SQL server in the command we just executed i.e. you SharePoint farm service user.  Run the command below to add the SPN record for the SQL server

setspn -A MSSQLSvc/SQLServerName.Domain.com:1433 Domain\UserName

Note: Replace SQLServerName.Domain.com with the full FQDN name of your SQL Server. If you are using SQL AlwaysOn Availability or cluster, please enter the full FQDN of the SQL listener. Also change the Domain\Username with the SharePoint service user.

Once executed, run the first command we executed and make sure that you have the SQL SPN record for the username.

This should fix the Event ID 5586

(6433)