I see lots of examples on running SQLPS from SQL Server Agent, but how do you call a powershell script from TSQL on demand?
I'm replacing a C# CLR function with a PowerShell script. The script will simply get a path and date modified for files in a directory structure.
What's the command to call a PowerShell script from a T-SQL stored procedure?
Something like this:
set #sql = 'powershell.exe -file "YourScript.ps1" -nologo'
EXEC xp_cmdshell #sql
Related
I have an SSIS dtsx package that I want to run using PowerShell. Below is what I am running in powershell.
EXEC xp_cmdshell '"C:\Program Files\Microsoft SQL Server\130\DTS\Binn\DTExec.exe" /f "F:\SqlExport\New package.dtsx"'
Unfortunately I get the below error, and I dont know why
EXEC : The term 'EXEC' 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.
At line:1 char:1
+ EXEC xp_cmdshell '"C:\Program Files\Microsoft SQL Server\130\DTS\Binn ...
+ ~~~~
+ CategoryInfo : ObjectNotFound: (EXEC:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I have enabled the xp_cmdshell in SQL Server using below command as well
EXEC xp_cmdshell is a T-SQL statement for launching shell commands via cmd.exe.
I presume the intent is to have PowerShell execute the command that starts with executable path "C:\Program Files\Microsoft SQL Server\130\DTS\Binn\DTExec.exe", launched from your T-SQL script via EXEC xp_cmdshell[1].
Instead, your error message implies that PowerShell executed the entire line, which predictably failed: PowerShell has no EXEC command (and there is no external program by that name on your system).
That said, PowerShell is neither needed here, nor would it enter the picture, if your EXEC xp_cmdshell line had been executed by SQL Server (via a T-SQL script).
In fact, your T-SQL command should work as-is (invocation of an external program with arguments, via cmd.exe), if properly executed by SQL Server.
[1] Update: If the intent is simply to launch the command from PowerShell
& "C:\Program Files\Microsoft SQL Server\130\DTS\Binn\DTExec.exe" /f "F:\SqlExport\New package.dtsx"`
Note the need for &, the call operator, to tell PowerShell that the double-quoted string that follows is the name of an executable to invoke.
I am getting the exception as mentioned when I am trying to execute sql queries using PowerShell
[ERROR] Invoke-Sqlcmd cmdlet was not available.
SQL Server PowerShell components may not be installed.
The weird thing I figured out was with this path $env:PSMODULEPATH. I am having the paths set as follows
%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\;C:\Program Files\WindowsPowerShell\Modules\
Instead of the above if I just move the following path before SQL Server or to the first position it is working fine
C:\Program Files\WindowsPowerShell\Modules\
When I update this is how it looks
%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files\WindowsPowerShell\Modules\;C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\
So is there any way to move it to the first place or above to the SQL PS module path
NOw you just need to set the environmental variable to point to that path externally:
Set-Item -Path env:psmodulepath -Value "C:\Program Files\WindowsPowerShell\Modules\;%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\;"
This should do our job.
Hope it helps.
We have recently started using SQL Server 2012 SP3 and building the SQL server 2012 using a PowerShell script. There is a requirement in our automation process to run multiple database scripts on a db and I have found Invoke-Sqlcmd very reliable until I found this issue.
When I run Invoke-sqlcmd with a proper set of parameters in PowerShell's debug mode on the system on which the SQL server is installed recently, I don't have problem.
PowershellCommand : Invoke-Sqlcmd -InputFile $sStrJBSPExecRolePath -ServerInstance $sStrSQLName -ErrorAction Stop
But when I execute same query through a PowerShell automation script after rebuilding the same server, I end up getting below error
The term 'Invoke-Sqlcmd' 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 did research online many suggested to Import SQLPS, etc., so for testing I added the below command in my script
get-pssnapin -Registered
Import-Module “sqlps” -DisableNameChecking**
Even after adding the above into the script, I still end up with same error. But when I run the same script manually it runs perfectly fine. I don't understand what is wrong.
PowerShell automation script - This script installs the .Net Framework 3.5, SQL Server 2012, SQL Server 2012 SP3, and then loads the SMO assembly that I use to change SQL settings such as the Max Memory limit of SQL.
Open up PowerShell as an Administrator and install the sqlserver module by Install-Module sqlserver
After the module has installed, the module commands including the Invoke-sqlcmd should be readily available.
You can check the same using Get-Command -Module sqlserver.
If this module is not readily available, you can Import-Module sqlserver after installing it.
This is not a complete solution, but just a work around which is working for me.
When you execute the query from automation the user which is executing that is not having access to the sqlcmd. Execute you command for the directory where your sqlcmd.exe is present.
Just put
CD "C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn".
to get the location for sqlcmd search the location for SQLCMD.exe in the search box.
if not found, you need to install that where it is missing, but in your case I think it is present, you just need to get the location right.
Also you will need set the path variable for the user executing the automation script or else it will only recognize the sqlcmd, but wont execute that.
$env:Path += ";C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\"
you can get this path from you local user for which it is working by $Env:Path
I am installing SQL Server 2008R2 with Powershell.
Below is the command
function install{
$Command ="SQL_SERVER_CDs\SQL2008_R2\R2_ENTERPRISE\Setup.exe /q /ACTION=Install /IACCEPTSQLSERVERLICENSETERMS /INDICATEPROGRESS /FEATURES=SQLENGINE,REPLICATION,FULLTEXT /INSTANCENAME=DEMO5 /SECURITYMODE=SQL /SAPWD=Me#inv2011 /SQLSYSADMINACCOUNTS=CORP\R-PITTUR CORP\AIMFUNDS-G-Admin-SQL /SQLSVCACCOUNT=RAMU-PC\RAMU /SQLSVCPASSWORD=***** /AGTSVCACCOUNT=RAMU-PC\RAMU /AGTSVCPASSWORD=***** /ISSVCACCOUNT=RAMU-PC\RAMU /ISSVCPASSWORD=***** /INSTANCEDIR=S:\\ /INSTALLSHAREDDIR=C:\Program Files\Microsoft SQL Server /SQLUSERDBDIR=I:\DB_DATA /SQLUSERDBLOGDIR=H:\DB_LOGS /SQLTEMPDBDIR=T:\DEMO5 /SQLBACKUPDIR=S:\DEMO5 /SQLCOLLATION=SQL_Latin1_General_CP1_CI_AS"
Invoke-Expression $Command
}
When i call this install function, SQL installation is happenening but nothing is displayed in the Powershell console though i put Indicateprogress in the above installation string. How can we display logging of sql server installation. Is it possible to redirect the output to any control like Richtextbox using Powershell.
Some of your command options contain spaces, for eg:
/INSTALLSHAREDDIR=C:\Program Files\Microsoft SQL Server
wrap them in quotes like this:
`"/INSTALLSHAREDDIR=C:\Program Files\Microsoft SQL Server`"
note the use of the backtick character (`) to escape the quote within the string.
/q means quite install, right?
Here's the command I use to run SQL 2008 R2 installs:
$command += 'setup.exe /CONFIGURATIONFILE=`"$configFile`" /SAPWD=`"$sysadminPassword`" /SQLSVCPASSWORD=`"$servicePassword`" /AGTSVCPASSWORD=`"$servicePassword`" /FTSVCPASSWORD=`"$servicePassword`" /ISSVCPASSWORD=`"$servicePassword`"'
Invoke-Expression $command
What would I need to do in order to run a PowerShell script in an SSIS package?
Does PowerShell need to be installed on the SQL server?
How do I actually call the script in SSIS?
Thanks
Yes, PowerShell needs to be installed. Then, you can use an Execute Process Task to point to the PowerShell executable and give the script as an argument.
Like so:
Executable: %windir%\system32\WindowsPowerShell\v1.0\powershell.exe
Arguments: C:\path\to\script.ps1