SQL CRUD example error

New to PSU so please be gentle…
I’ve reproduced Adam’s SQL CRUD blog in my environment however I’m getting a Invoke-DbaQuery error

I can run sections of the code in the powershell editor and retrieve data so it seems to be an environment problem. I just don’t know where to start. Not sure how to change the run as credentials as my SQL DB is using trusted connection.
If anyone can point me in the right direction I’d be very grateful.

Hello and welcome,
if dbatools is installed correctly it is automatically available in every UD environment. Try restart the UD service and try again.

Thanks for the comment. I have restarted the service (and my desktop) but no change.
Here’s a screenshot of my command line.

local

Maybe you installed the dbatools module only to the user profile and not system wide?

@dalef
your issue is the dbatools module is not loaded in PSU thats why you see the message command not recognized
what you need to do is to add the dbatools module in the admin screen under the environment you are using to be loaded when you run psu as below

1 Like

Thanks, that makes sense. I didn’t realise I had to add them manually. OK, so I type in dbatools and hit save but when I check it it’s not there - it doesn’t save. Am I missing something?

Not there…

@dalef
The problem with integrated env before the latest release it was not loading the windows powershell modules so adam fixed that issue in the latest release.
but in your case if you are using integrated env try to add the full path of dbatools just by itself first or the module name only to check if your issue resolved before adding anything else.

another solution is to look for psu folder and locate the modules folder there and add the dbatools folder to the rest of the moules there and test.

you can also in you script before running the query line add the following line to the path of the dbatools

import-module <path to dbatools module> -force 
invoke-dbaquery ....
1 Like

Many thanks. I’ll give those options a try. By the way, should I prefer one environment over another? All my scripts work in 5.1 and 7.2 so should I target those? What’s the “integrated” aspect of the Integrated environment - does it use both?

The integrated environment runs directly within the PSU process rather than starting an external process. If you are using PSU v2, it’s using PowerShell version 7.1.

Great, thanks Adam.