Sql-Server

如何使用powershell編寫文件夾中的所有權限?

  • October 1, 2018

使用下面的腳本,我找出數據和日誌文件夾在哪裡:

   declare @rc int, 
   @dir nvarchar(4000) 

   declare @data    nvarchar(4000)
   declare @log     nvarchar(4000)
   declare @SQLPath nvarchar(4000)
   declare @Backup  nvarchar(4000)

   exec @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\Setup',N'SQLPath', @dir output, 'no_output' 
   select @SQLPath = @dir

   ----------------------------------------------------------------
   --For the default data location: 
   ----------------------------------------------------------------

   exec @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\MSSQLServer',N'DefaultData', @dir output, 'no_output' 


   if (@dir is null) 
   begin 
   exec @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\Setup',N'SQLDataRoot', @dir output, 'no_output' 
   select @dir = @dir + N'\Data' 
   end 

   select @data = @dir

   ----------------------------------------------------------------
   --For the default log location: 
   ----------------------------------------------------------------
   exec @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\MSSQLServer',N'DefaultLog', @dir output, 'no_output' 


   select @log = @dir


   ----------------------------------------------------------------
   --For the default Backup location: 
   ----------------------------------------------------------------
   exec @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\MSSQLServer',N'BackupDirectory', @dir output, 'no_output' 


   select @Backup = @dir

   SELECT  
   [data path] =     @data, 
   [log path] =      @log, 
   [sql path] =      @SQLPath, 
   [Backup Folder] = @Backup

然後我手動轉到該文件夾並查看 sql server ( the sql server services account) 對該文件夾的權限。

在此處輸入圖像描述

我想編寫這些權限的腳本,以便我可以將它們應用於新文件夾。

使用powershell,我如何從這個文件夾編寫所有這些權限let's say folder a:\data並將這些權限應用到folder b:\data同一台機器上?

Get-Acl 幫助我將權限從我的開發機器 DATA 文件夾複製到測試文件夾。

$Acl = Get-Acl "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER01\MSSQL\DATA"
Set-Acl "C:\Program Files\Microsoft SQL Server\test" $Acl

這會覆蓋新文件夾的權限,請小心處理。

( https://blogs.msdn.microsoft.com/johan/2008/10/01/powershell-editing-permissions-on-a-file-or-folder/ )

引用自:https://dba.stackexchange.com/questions/218980