PowerUpSQL icon indicating copy to clipboard operation
PowerUpSQL copied to clipboard

Invoke-SQLUploadFileOle on SQL linked servers

Open MuhoX opened this issue 1 year ago • 0 comments

Hello,

The function Invoke-SQLUploadFileOle does not have an option to use it on linked servers. I've modified the code slightly so that it can be used on linked servers.

I'd like to request a new feature or function to be able to upload and download files with OLE on linked servers. Below is an example code which can be implemented into the upload function:

 [Parameter(Mandatory = $true,
        HelpMessage = 'Linked server name string')]
        [String]$LinkedServer = "",
...
		write-verbose "$instance : Uploading $($FileBytes.Length) bytes to: $OutputFile"           
		$QueryFileUpload = @"
EXEC ('
DECLARE @ob INT;
EXEC sp_OACreate ''ADODB.Stream'', @ob OUTPUT;
EXEC sp_OASetProperty @ob, ''Type'', 1;
EXEC sp_OAMethod @ob, ''Open'';
EXEC sp_OAMethod @ob, ''Write'', NULL, 0x$FileData;
EXEC sp_OAMethod @ob, ''SaveToFile'', NULL, ''$OutputFile'', 2;
EXEC sp_OAMethod @ob, ''Close'';
EXEC sp_OADestroy @ob;') AT [$LinkedServer];
"@
		# Execute query    
		$null = Get-SQLQuery -Instance $Instance -Path $LinkedServer -Query $QueryFileUpload -Username $Username -Password $Password -Credential $Credential -SuppressVerbose 	
...	

For me editing this part of the code it was possible to upload and download files through linked SQL servers, but it's not perfect. If needed I can further modify both the upload and download functions and make a Pull Request.

Regards,

MuhoX

MuhoX avatar Dec 25 '24 12:12 MuhoX