Search code examples
powershellaccess-denied

Powershell Invoke-Sqlcmd: xp_create_subdir() returned error 5, 'Access is denied.'


I was trying to use run a powershell script, which using invoke-sqlcmd to execute my script file to backup my database into specific folder.

Invoke-Sqlcmd -InputFile $scriptfile -ServerInstance $ServerName -Database "Master" -Username $sqlUser -Password $sqlPassword

It works fine in D drive (or other drives), but whenever on/at C drive and place the backup database at C drive, it will hit:

Invoke-Sqlcmd: xp_create_subdir() returned error 5, 'Access is denied.'

I wonder if I have missed out any permission grant?

The user permission (domain\ServiceAccount) was granted to FullControl to the output folder before invoke-sqlcmd.

Please help.


Solution

  • Solved. I was able to perform the action after I grant the access to the folder for the account NT Service\MSSQL$Instance.