SQL job cannot delete snapshot - sql-server

I have an issue with deleting snapshot job. The SQL Server job history shows:
Message Executed as user: NT Service\SQLSERVERAGENT. The step did not generate any output. Process Exit Code 255. The step failed.
NT Service\SQLSERVERAGENT account has "full control" permissions to whole snapshot path.
My job has one step:
Get-ChildItem -Path G:\Data\snapshot\unc\SNAPSHOT_PUBLICATION -Include *.* -Recurse | foreach { $_.Delete()}
get-Date >> g:\data\snapshot\unc\snapshot-cleaner.log
Any ideas why this action fails every time?

Related

GitLab on windows using sqlserver job: unable to persist credentials persist credentials with the 'wincredman'

I got an error during the call to a file called by powershell.exe on WINDOWS.
I try to push on GitLab repo.
The code it's inside a script (see below).
I call it from SQL SERVER JOB
----------- the code -------------
$DTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$Msg = ("Auto from SQL job on '" + $DTime + "'")
cd "B:\Instances"
git pull
git add --all
git commit -m $Msg
git push origin master
This is the error:
fatal: Unable to persist credentials with the 'wincredman' credential store.
See https://aka.ms/gcm/credstores for more information.
bash: /dev/tty: No such device or address
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'https://xxx.xxx.it': No such file or directory
I understand is something related to WINDOWS and authentication... but I can't find a solution.
Using "Git bash" calling with the same user of the SQL SERVER JOB, all run fine, also because I run it with the users I need. For the SQL SERVER JOB I got a proxy user (same of the call via "Git Bash").
I tried aldo with "Windows Credential Manager" to re-insert user and password but I'm always front to this wall. How is it possible to call from sql server JOB not powershell but directly my "Git Bash"? I'm not an expert of powershell and this kind of call.
This is the path to my "C:\Program Files\Git\git-bash.exe"....
I would like to sostitute the step below... but Git Bash can receive en external file like arguments... like the one that I use with PowerShell?
powershell.exe -File "C:\Dbatools\GitLab_Push.ps1"
Thak to anyone can give me the right advice.
Alen Italy.

How to run the PowerShell batch file or a command.ps1 file through SQL server if sql is in one sever and file is in another server

I want to connect two VM/server one has data file and batch file another have sql install wanted to trigger command of server A which will hit server b files
Sql command--- in server A
EXEC master..xp_cmdshell 'cd.. && "C:\Program Files\Powershell\6\pwsh.exe" -File "C:\Users\sprasad\Desktop\script\command1.ps1"'
error are
1- import-module: The specified module 'C:\Program
Files\Derivation_19_01_rev0\Core.PowershellModule.TradeLoader.dll' was
not loaded because no valid module file was found in any module
directory.
because files are in server B
I am running sql scripts stored in files (sqlscript.sql) on ServerA against a SQL installation on ServerB using remote powershell. The Powershel module SQLPS is needed on serverA.
see link
steps:
enable remotepowershell on serverB
Enable-PSRemoting -force
in your SQL server, add the user that executes the script on serverA in the list of users with rights on the database.
use a script on severA like:
Import-Module -Name SQLPS -NoClobber -DisableNameChecking -Scope Local Invoke-Sqlcmd -ServerInstance serverA -InputFile sqlscript.sql
-Verbose
Invoke-Sqlcmd -ServerInstance 'serverB' -InputFile sqlscript.sql -Verbose

TFS Run batch script on a remote server with admin permission

I currently have Server A which is where my TFS and Build Agent is located. I then have Server B which is when my source code site. I am trying to set up a build definition and copies file from on location in server B to another and then build the solution.
However when I run this batch file as part of a build definition it is not creating folders where it need to be. I believe due to the agent not having correct permissions.
Is there a way to run the following batch script to run with Admin permission from a build definition.
You can try below workarounds:
Convert the batch script to PowerShell script, then copy the
PowerShell script to target machine and use the PowerShell on Target
Machines task to run the script. You can enter your admin user
and password using the task. Reference below screenshot.
Add a PowerShell task and run below script to call the cmd.exe to
run the batch script with an admin user account on target machine
(Copy the batch script to target machine first, in below sample I
copied the batch script to C:\Scripts\Test.bat):
Param(
[string]$computerName = "v-tinmo-12r2",
)
$Username = "Domain\user"
$Password = ConvertTo-SecureString "password-here" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($Username,$password)
Invoke-Command -ComputerName $computerName -Credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c 'C:\Scripts\Test.bat'"}

Mapping a drive and backing up a SQL Server database

We back up our SQL Server databases to NAS, and was curious if it would be possible to back up to a temporary mapped drive, as opposed to the UNC path. I'm hitting a wall.
Using New-PSDrive:
New-PSDrive -Name "Y" -PSProvider FileSystem -Root "\\NAS\Backups" -Scope Global -Persist
Test-Path "Y:\SQLDBA" <# returns True #>
<# Method 1 #>
sqlcmd -E -d master -Q "BACKUP DATABASE [SQLDBA] TO DISK = N'Y:\SQLDBA\SQLDBA.bak' WITH INIT" -b
<# Method 2 #>
Invoke-Sqlcmd -Database master -Query "BACKUP DATABASE [SQLDBA] TO DISK = N'Y:\SQLDBA\SQLDBA.bak' WITH INIT" -QueryTimeout 999999999 -verbose -ErrorAction Stop
Remove-PSDrive "Y"
Using net use:
net use Y: \\NAS\Backups /persistent:yes
<# Method 1 #>
sqlcmd -E -d master -Q "BACKUP DATABASE [SQLDBA] TO DISK = N'Y:\SQLDBA\SQLDBA.bak' WITH INIT" -b
<# Method 2 #>
Invoke-Sqlcmd -Database master -Query "BACKUP DATABASE [SQLDBA] TO DISK = N'Y:\SQLDBA\SQLDBA.bak' WITH INIT" -QueryTimeout 999999999 -verbose -ErrorAction Stop
net use Y: /delete
Either method gets me the same error:
Cannot open backup device 'Y:\SQLDBA\SQLDBA.bak'. Operating system error 3(The system cannot find the path specified.).
Is this possible?
No, it is not. Mapped drives exist in a Session. The server process (of sql server) has it's own session, so it does not see your mapped drive.
To back up to a network drive when SQL Server is running in a domain user account, the shared drive must be mapped as a network drive in the session where SQL Server is running. If you start Sqlservr.exe from command line, SQL Server sees any network drives you have mapped in your login session.
Please check the following article for reference:
http://msdn.microsoft.com/en-us/library/ms179313.aspx#NetworkShare

Deploying database using TFS Deployer and SqlPackage

I'm trying to make PowerShell script that uses SqlPackage to deploy database through TFS Deployer service. Script is working if it is executed directly from command line but it fails when TFS Deployer tries to execute it, the same user account is used for both cases.
The service is running in test mode (TfsDeployer -d), as console application, but it also fails when runs as Windows Service.
This is log file (captured output of SqlPackage and exception caught in PowerShell script):
11/18/2012 17:51:49 | Publishing to database 'databaseName' on server 'machineName'.
11/18/2012 17:51:56 | An error occurred while the batch was being executed.
11/18/2012 17:51:56 | System.Management.Automation.RemoteException: *** Could not deploy package.
These are the only information I was able to collect. Error code (HRESULT) was not present in the caught exception.
PowerShell script goes like this:
try{
$cmd = Join-Path (Get-Item "Env:ProgramFiles(x86)").Value "Microsoft SQL Server\110\DAC\bin\SqlPackage.exe"
$src = Join-Path $source "db.dacpac"
$cfg = Join-Path $source "db.publish.xml"
&$cmd /Action:Publish /SourceFile:$src /Profile:$cfg 2>&1 | ForEach-Object -Process {
Write-Log $_
}
}
catch [Exception] {
Write-Log $_.Exception.ToString()
if($_.Exception.HResult) {
Write-Log "Code: $($_.Exception.HResult.ToString('X'))"
}
}

Resources