Cannot execute powershell file with function call in Transact SQL script Cannot execute powershell file with function call in Transact SQL script powershell powershell

Cannot execute powershell file with function call in Transact SQL script


After encountering this same problem, I've found out that I needed to check the ExecutionPolicy on both machines. I knew my ExecutionPolicy was fine on my machine, but I checked the SQL server machine via:

EXEC master..xp_cmdshell 'powershell -command "& { Get-ExecutionPolicy }" '

which returned Restricted (which means that no Powershell script can run, and hence I ran into the same error message that you did).

Additionally, I also made sure my script was stored in a location that SQL Server had permission to access. You could check this in your case with a command like:

EXEC master..xp_cmdshell 'powershell -command "& { dir C:\Users\mslaats\Desktop\Databasescripts\Powershell }" '

And that command will let you know if SQL can see your script or not. In my case, I put my script in a shared network location I knew SQL could already access.