Invoke-Sqlcmd : The WriteObject and WriteError methods cannot be called from outside the overrides of the BeginProcessi
ng, ProcessRecord, and EndProcessing methods, and only from that same thread. Validate that the cmdlet makes these call
s correctly, or please contact Microsoft Support Services.