You are here:Home » XP_CMDSHELL » Sharing/Unsharing remote server's folder with remote stored procedure execution in SQL Server

Sharing/Unsharing remote server's folder with remote stored procedure execution in SQL Server

Recently, when I was working with database backup plans and scheduling it on another server as this shared folder of the server should not be available to anyone after the backup process completion. I have created below script with stored procedure on remote server and run the stored procedure remotely from local server to share and unshare the remote server's folder. Please note xp_cmdshell and Ad Hoc Distributed Queries should be enabled on the server.
USE MASTER
GO

-- Creating shared 
EXEC XP_CMDSHELL 'NET SHARE SharedFolder=E:\DatabaseBackups
     /GRANT:Everyone,Full
     /REMARK:"Database backups perform on another server"'    
GO

-- Removing shared
EXEC XP_CMDSHELL 'NET SHARE SharedFolder  /delete"'
GO