Fix bug in powershell script executed from Sql server
€8-30 EUR
Cancelled
Posted over 7 years ago
€8-30 EUR
Paid on delivery
I have a sql trigger that executes a ps1 script.
The script works but I would like to have it work in background. Takes very long to finish.
When I try to set it up as a script block it only works when executed locally from server.
Not when executed from Sql Server:
This is the sql trigger:
-------------------
declare @cmd nvarchar(2000)
declare @folder nvarchar(200)
SET @folder = ''''+'"'+(SELECT CAST(Project_ID as nvarchar)+' '+Name FROM INSERTED)+'"'+''''
set @cmd = 'powershell "D:\PS\[login to view URL]"'+' ' +@folder
exec master.dbo.xp_cmdshell @cmd
-------------------------
This is script that works when called from sql but takes very long to finish:
-------------------------
param(
[Parameter(Position=0, Mandatory=$true)] [string]$FolderName
)
$username = "user"
$PasswordFile = "D:\PS\Passwords\[login to view URL]"
$KeyFile = "D:\PS\Passwords\[login to view URL]"
$key = Get-Content $KeyFile
$UserCred = New-Object [login to view URL] ($username, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key))
$NetWorkCred = New-Object [login to view URL]($username , (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key))
$Session = New-PSSession -ConfigurationName [login to view URL] -ConnectionUri [login to view URL] -Credential $UserCred -Authentication Basic -AllowRedirection
Import-Module D:\PS\[login to view URL]
Create-Folder -MailboxName mailbox -Credentials $NetWorkCred -NewFolderName $FolderName -ParentFolder '\Innboks'
Import-PSSession $Session
$ProjectNo = $[login to view URL](0,4)
New-InboxRule -Name $ProjectNo -Mailbox mailbox -SubjectOrBodyContainsWords "PREFAB$ProjectNo" -MoveToFolder ":\Innboks\$FolderName" -StopProcessingRules $true -confirm:$false -Force
Remove-PSSession $Session
-------------------------
This is script that works when executed locally on server and works in background, but it does not work when called from sql trigger:
-------------------------
param(
[Parameter(Position=0, Mandatory=$true)] [string]$FolderName
)
$createFolderAndRule = {
param([string]$FolderName)
$username = "user"
$PasswordFile = "D:\PS\Passwords\[login to view URL]"
$KeyFile = "D:\PS\Passwords\[login to view URL]"
$key = Get-Content $KeyFile
$UserCred = New-Object [login to view URL] ($username, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key))
$NetWorkCred = New-Object [login to view URL]($username , (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key))
$Session = New-PSSession -ConfigurationName [login to view URL] -ConnectionUri [login to view URL] -Credential $UserCred -Authentication Basic -AllowRedirection
Import-Module D:\PS\[login to view URL]
Create-Folder -MailboxName mailbox -Credentials $NetWorkCred -NewFolderName $FolderName -ParentFolder '\Innboks'
Import-PSSession $Session
$ProjectNo = $[login to view URL](0,4)
New-InboxRule -Name $ProjectNo -Mailbox mailbox -SubjectOrBodyContainsWords "PREFAB$ProjectNo" -MoveToFolder ":\Innboks\$FolderName" -StopProcessingRules $true -confirm:$false -Force
Remove-PSSession $Session
}
Start-job -ScriptBlock $createFolderAndRule -Argumentlist $FolderName
-------------------------
Hi,
This can't be done using MySQL, because xp_cmdshell command will block execution and wait for the program to finish.
The proper way to do this is detect the condition of trigger,enque the data to be processed in a table, and have a program run in such a way that it polls the table for the data and then use it for the processing.
for further information,please contact me.
regards,
Tarandeep
Dear Project Poster,
I have relevant hands on 5 years of experience .We have a team of all the skills. I assure you about the work quality and time delivery. Please give me chance to complete this project.
We have 100% client satisfaction ratio.
I had already done the same work in past. Looking forward to hear from you.
Let me give an opportunity to work. We can talk further when you message me.
Hello.
first of all check which account uses SQL when triggering. Usually this is issue source.
In case of any questions feel free to chat.
Thanks and have a nice day.