Find Jobs
Hire Freelancers

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 -------------------------
Project ID: 11656683

About the project

7 proposals
Remote project
Active 8 yrs ago

Looking to make some money?

Benefits of bidding on Freelancer

Set your budget and timeframe
Get paid for your work
Outline your proposal
It's free to sign up and bid on jobs
7 freelancers are bidding on average €51 EUR for this job
User Avatar
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
€28 EUR in 1 day
5.0 (6 reviews)
2.9
2.9
User Avatar
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.
€33 EUR in 1 day
0.0 (0 reviews)
0.0
0.0
User Avatar
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.
€55 EUR in 1 day
0.0 (0 reviews)
0.0
0.0

About the client

Flag of NORWAY
Bjerkreim, Norway
4.8
5
Payment method verified
Member since Aug 20, 2016

Client Verification

Thanks! We’ve emailed you a link to claim your free credit.
Something went wrong while sending your email. Please try again.
Registered Users Total Jobs Posted
Freelancer ® is a registered Trademark of Freelancer Technology Pty Limited (ACN 142 189 759)
Copyright © 2024 Freelancer Technology Pty Limited (ACN 142 189 759)
Loading preview
Permission granted for Geolocation.
Your login session has expired and you have been logged out. Please log in again.