我有一个.bat文件,它可以毫无问题地执行一系列内容。它进入powershell执行行:PowerShell.exe"Y:\Data\FS01\NoTouch_Deploy.ps1"
我得到以下错误:警告:未能加载"SQLAS"扩展:尝试管理服务时,SMO中出现异常。-->无法检索此请求的数据。-->无效的类
当我通过GUI运行相同的脚本时,它运行时没有任何问题。我也尝试过在同一个地方运行一个简单的Powershell脚本(测试脚本只是弹出一个Windows消息框,让我知道它正在打开和执行),这也很有效。但一旦我添加了常规.ps1并从.bat文件中运行它,我就会收到这个错误。脚本包含在下面。脚本的快速摘要:它设置了几个变量,创建了2个函数(BCP和一个用于错误捕获的logwrite),重命名了一些表并启动了SQL存储过程。然后执行BCP功能将数据从服务器移动到服务器,然后再执行一个过程。
cls
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
## TEST MSGBOX## [System.Windows.Forms.MessageBox]::Show("We are proceeding with next step." , "Status")
$ErrorActionPreference = "stop"
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
[Void][System.Reflection.Assembly]::LoadWithPartialName("System.Data")
Add-PsSnapin sqlserverprovidersnapin100 -ErrorAction SilentlyContinue
Add-PsSnapin sqlservercmdletsnapin100 -ErrorAction SilentlyContinue
Import-Module SQLPs -DisableNameChecking -ErrorAction SilentlyContinue
#Import-Module SQLAS -DisableNameChecking -ErrorAction SilentlyContinue
Set-Location 'C:'
# Set Variables (Make table driven for final tool creation)
$SourceServer = "DevServ"
$SourceDB = "DevDB"
$SourceObject = "DevTable"
$TargServer = "ProdServ"
$TargDB = "ProdDB"
$TargObject = "ProdTable"
$ContainerDB = 'MainDB'
################################################################################################################
## Begin Function LogWrite ##
##################################################################################################### ###########
Function LogWrite ()
{
Param
(
[string]$LogStr,
[string]$Table,
[string]$Server,
[string]$DataBase
)
Trap{continue}
#$DateTime = Get-Date -UFormat "%Y:%MM:%D:%H:%M:%S"
$LogInsQuery = "INSERT INTO dbo.Deploy_LOG ([TableName], [LOGTEXT],[DateStamp]) VALUES ('" + $Table + "', '" + $LogStr + "',getdate())"
Invoke-Sqlcmd -ServerInstance $SServer -Database $SDB -Query $LogInsQuery
}
################################################################################################################
## Begin Function BCP
################################################################################################################
Function BCP ()
{
Param
(
[string]$SourceServer,
[string]$SourceDB,
[string]$SourceObject,
[string]$TargServer,
[string]$TargDB,
[string]$TargObject
)
$NewServer
Try
{
$SourceCon = New-Object Data.SqlClient.SqlConnection
$SourceCon.ConnectionString = "Data Source=$SourceServer;DataBase=$SourceDB;Integrated Security=True"
Logwrite "$TargObject -- Beginning BCP transfer process" $SourceObject
$TargCon = New-Object Data.SqlClient.SqlConnection
$TargCon.ConnectionString = "Data Source=$TargServer;DataBase=$TargDB;Integrated Security=True"
$TargCon.open()
#[long]$StartCount = (Invoke-Sqlcmd -ServerInstance $SourceServer -Query "SELECT COUNT(*) as 'Ct' FROM $TargDB.dbo.$TargObject")[0]
$bcp = New-Object Data.SqlClient.SqlBulkCopy($TargCon.ConnectionString, [System.Data.SqlClient.SqlBulkCopyOptions]::KeepIdentity)
$FieldsServer = New-Object "Microsoft.SqlServer.Management.SMO.Server" $TargServer
$DevServer = New-Object "Microsoft.SqlServer.Management.SMO.Server" $SourceServer
$SelectString = 'SELECT '
$DevServer.Databases[$TargDB].Tables | ?{$_.name -eq $TargObject} | %{$_.Columns | %{$SelectString += "[$($_.name)],"}}
$SelectString = $SelectString.Substring(0, ($SelectString.Length - 1)) # Remove the trailing comma
$SelectString += " FROM dbo.$SourceObject"
$SourceCon.open()
$SqlCmd = New-Object "Data.SqlClient.SqlCommand" -ArgumentList $SelectString, $SourceCon
[Data.IDataReader]$DataReader = $SqlCmd.ExecuteReader()
$bcp.BulkCopyTimeout = 0
$bcp.DestinationTableName = "dbo.$TargObject"
$bcp.WriteToServer($DataReader)
$SqlCmd = $null
$DataReader = $null
$SourceCon.Close()
$bcp.Close()
$TargCon.Close()
LogWrite "$TargObject -- Transfer complete" $SourceObject;
}
Catch
{
LogWrite "ERROR in BCP Subroutine -- $_" $SourceObject;
}
}
################################################################################################################
## Begin main code section end of functions
################################################################################################################
Try
{
$SqlQuery = "SP_Rename 'TableName','TableName_Deploy'"
Invoke-Sqlcmd -ServerInstance $SServer -Database $SDB -Query $SqlQuery
$SqlQuery =""
# runs the Sproc on the FLD server to make sure that the _Deploy table exists, is empty and is indexed.
$DeployTableSproc = "EXECUTE [dbo].[SP_NoTouch_Staging_Build]"
Invoke-Sqlcmd -ServerInstance $TargServer -Database $ContainerDB -Query $DeployTableSproc
# Runs the BCP process to copy the records to the Feild into the new prepared table.
BCP $SServer $SDB $SourceObject $TargServer $TargDB $TargObject
$SqlQuery = "EXECUTE [MainDB].[dbo].[Notify_NoTouch] @EmailType = 6"
Invoke-Sqlcmd -ServerInstance $SServer -Database $SDB -Query $SqlQuery
$SqlQuery =""
LogWrite "No touch table transfer complete Table renamed and indexed" $SourceObject;
}
Catch
{
LogWrite "ERROR in Transfer process" $SourceObject;
}
我试图添加#导入模块SQLAS-DisableNameChecking-ErrorAction SilentlyContinue这也于事无补。有什么想法吗?感谢
由于您最终希望通过任务调度程序运行此脚本,我会继续在您想要的服务器上创建任务。在Actions选项卡中,只需设置以下条件:
程序/脚本字段
C:WINDOWSsystem32WindowsPowerShellv1.0powershell.exe
添加参数字段:
-NoProfile -ExecutionPolicy unrestricted -nologo -command "&{\pathtoyourfilesFileName.ps1 -Silent:$true}"
如果你想用一个批处理文件来测试这一点,只需要有一些类似的代码:
schtasks /RUN /s "ServerName" /TN "Scheduled Tasks Named" | Out-String