简体   繁体   English

Powershell Script using Invoke-SQL command,needed for SQL job, the SQL Server version of Powershell is somewhat crippled, is there a workaround?

[英]Powershell Script using Invoke-SQL command,needed for SQL job, the SQL Server version of Powershell is somewhat crippled, is there a workaround?

Full Question: Have Powershell Script using Invoke SQL command, using snappins, I need them to be included in a SQL job, the SQL Server version of Powershell is somewhat crippled, does anyone know a workaround? Full Question: Have Powershell Script using Invoke SQL command, using snappins, I need them to be included in a SQL job, the SQL Server version of Powershell is somewhat crippled, does anyone know a workaround?

From what I have gathered, SQL Management Studio's version of powershell is underpowered, not allowing for the use of snappins, as such it does not recognize the cmdlets that I used in the script.根据我收集到的信息,SQL Management Studio 版本的 powershell 功能不足,不允许使用 snappin,因此它无法识别我在脚本中使用的 cmdlet。 I have tried running it in the job as a command line prompt rather than a Powershell script, which causes the code to work somewhat, however I check the history on the job and it says that invoke-sql is still not a recognized cmdlet.我尝试在作业中将其作为命令行提示符而不是 Powershell 脚本运行,这会导致代码在一定程度上起作用,但是我检查了作业的历史记录,它说 invoke-sql 仍然不是可识别的 cmdlet。 I speculate that because I am running the code on a remote server, with different credentials than my standard my profile with the snappins preloaded isn't being loaded, though this is somewhat doubtful.我推测,因为我在远程服务器上运行代码,使用的凭据与我的标准不同,我的预加载 snappin 的配置文件没有被加载,尽管这有点令人怀疑。

Also, as I am a powershell rookie, any advice on better coding practices/streamlining my code would be much appreciated!另外,由于我是 powershell 菜鸟,任何关于更好的编码实践/简化我的代码的建议将不胜感激!

Code is as follows:代码如下:

# define parameters
param
(
$file = "\\server\folder\file.ps1"
)

"invoke-sqlcmd -query """ | out-file "\\server\folder\file.ps1"

# retrieve set of table objects
$path = invoke-sqlcmd -query "select TableName from table WITH (NoLock)" -database db -server server

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$so = New-Object Microsoft.SqlServer.Management.Smo.ScriptingOptions

 $so.DriPrimaryKey = $false
 $so.Nocollation = $true
 $so.IncludeIfNotExists = $true
 $so.NoIdentities = $true
 $so.AnsiPadding = $false

# script each table
foreach ($table in $path)
{
#$holder = $table
$table =  get-item sqlserver:\sql\server\default\databases\database\tables\dbo.$($table.TableName)
$table.script($so) | out-file -append $file 
}


(get-content "\\server\folder\file.ps1") -notmatch "ANSI_NULLS"  | out-file "\\server\folder\file.ps1" 
(get-content "\\server\folder\file.ps1") -notmatch " AS "| out-file "\\server\folder\file.ps1" 
(get-content "\\server\folder\file.ps1") -notmatch "Quoted_" | out-file "\\server\folder\file.ps1"
(get-content "\\server\folder\file.ps1") -replace "\) ON \[PRIMARY\].*", ")" | out-file "\\server\folder\file.ps1"
(get-content "\\server\folder\file.ps1") -replace "\[text\]", "[nvarchar](max)" | out-file "\\server\folder\file.ps1"
(get-content "\\server\folder\file.ps1") -replace " SPARSE ", "" | out-file "\\server\folder\file.ps1"
(get-content "\\server\folder\file.ps1") -replace "COLUMN_SET FOR ALL_SPARSE_COLUMNS", "" | out-file "\\server\folder\file.ps1"


""" -database database -server server" | out-file "\\server\folder\file.ps1" -append

So I figured out the answer to my own question.所以我想出了我自己问题的答案。 Using this site: http://www.mssqltips.com/tip.asp?tip=1684 and http://www.mssqltips.com/tip.asp?tip=1199使用本网站: http://www.mssqltips.com/tip.asp?tip=1684http://www.mssqltips.com/tip.asp?tip=1199

I figured out that he was able to do so using a SQL Server Agent Proxy, so I followed the yellow brick road, and basically I set up a proxy to my account and was able to use the external powershell through a feature.我发现他可以使用 SQL 服务器代理代理来做到这一点,所以我沿着黄砖路走,基本上我为我的帐户设置了一个代理,并且能够通过一个功能使用外部 powershell。 A note, you need to create a credential under the securities tab in object explorer prior to being able to select one when creating the proxy.请注意,您需要在 object 资源管理器的“证券”选项卡下创建一个凭证,然后才能在创建代理时使用 select 一个。 Basically I ended up creating a proxy named powershell, using the powershell subsystem, and use my login info to create a credential.基本上,我最终使用 powershell 子系统创建了一个名为 powershell 的代理,并使用我的登录信息来创建凭据。 VOILA!瞧!

You have to add the snapins each time.您必须每次都添加管理单元。 In your editor you likely already have them loaded from another script/tab/session.在您的编辑器中,您可能已经从另一个脚本/选项卡/会话中加载了它们。 In SQL Server you will need to add something like this to the beginning of the script:在 SQL 服务器中,您需要在脚本的开头添加如下内容:

IF ( (Get-PSSnapin -Name sqlserverprovidersnapin100 -ErrorAction SilentlyContinue) -eq $null )
    {
        Add-PsSnapin sqlserverprovidersnapin100
    }
IF ( (Get-PSSnapin -Name sqlservercmdletsnapin100 -ErrorAction SilentlyContinue) -eq $null )
    {
        Add-PsSnapin sqlservercmdletsnapin100
    }

I'm not sure the error you are trying to workaround - can you post that?我不确定您要解决的错误-您可以发布吗?

Have you tried this from a PowerShell prompt?您是否从 PowerShell 提示符尝试过这个?

Add-PSSnapin SqlServerCmdletSnapin100添加-PSSnapin SqlServerCmdletSnapin100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM