PowerShell from SQL

Konstantin 1 Reputation point
2020-11-09T14:06:10.467+00:00

Hello,

I have two SQL boxes one SQL 2014 Developer edition and a second SQL 21019 standard on two different Windows boxes.

My PowerShell script working in SQL 2014 Developer but not in SQL 21019. It generates error

The term 'C:\Path_to_File\ExportToExcel.ps1'
is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name,
or if a path was included, verify that the path is correct and try again.

I tested this script from the command line and it working perfectly.

The sql code simple exec xp_cmdshell 'PowerShell.exe "C:\Path_to_File\ExportToExcel.ps1"'

Run command xp_cmdshell 'PowerShell.exe' doesn't generate any errors any errors

Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.
NULL
PS C:\Windows\system32>

Any ideas?

Thank you

Konstantin

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,521 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. SChalakov 10,386 Reputation points MVP
    2020-11-09T14:50:40.617+00:00

    Hi Konstantin,

    can you post the content of the script so that we can check it out and comment?

    Thanks and Regards,
    Stoyan

    0 comments No comments

  2. Konstantin 1 Reputation point
    2020-11-09T15:29:00.68+00:00

    Hi Stoyan this is a script example

    Remove-Item "*.csv"

    $sqlData = Invoke-Sqlcmd -Query "Select * From User" -Database "USER" -Server "SERVER1"
    $sqlData | Export-Csv -NoTypeInformation -Path "C:\Path_to_File\User.csv" –Delimiter “|”

    This code working if I run it from the command line.

    Thank you

    0 comments No comments

  3. Rich Matheisen 46,711 Reputation points
    2020-11-09T19:12:38.787+00:00

    An example isn't the code that fails! I expect if you take a look at the script you'll find a missing (or extra) quote somewhere.

    I'd start by removing the quotes that surround *.csv, USER, SERVER and C:\Path_to_File\User.csv

    0 comments No comments

  4. Konstantin 1 Reputation point
    2020-11-09T20:33:56.387+00:00

    this code I run

    exec xp_cmdshell 'PowerShell.exe "C:\Path_to_File\ExportToExcel.ps1"'

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.