It turns out that the call has been working but the output was not visible. This is because the script has code like this:
DECLARE UserCursor CURSOR FOR
SELECT
'CREATE USER ' + SPACE(1) + QUOTENAME([name]) + ' FOR LOGIN ' + QUOTENAME([name]) + ' WITH DEFAULT_SCHEMA = ' + QUOTENAME([default_schema_name]) + ';'
AS [-- SQL STATEMENTS --]
FROM sys.database_principals AS rm
WHERE [type] IN ('U', 'S', 'G') -- windows users, sql users, windows groups
AND [name] <> 'dbo'
AND [name] <> 'guest'
AND default_schema_name is not null
OPEN UserCursor
FETCH NEXT FROM UserCursor INTO @addUserScript
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @addUserScript
FETCH NEXT FROM UserCursor INTO @addUserScript
END
CLOSE UserCursor
DEALLOCATE UserCursor;
The solution then was to call the script like this:
Invoke-DbaQuery `
-SQLInstance $HCM_SQL_host `
-Database $Refresh_database `
-Verbose 4>&1 `
-File "\\file-srv0\scriptdir\Userlist.sql" | `
out-string -Width 250 | Out-File -FilePath $Create_Users_File_Fullname
Note the line "-Verbose 4?&1 `" that redirects the verbose output (i.e. the PRINT output) back to the standard output and, ultimately, the file.