我有一个.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;
}
我试图添加 #Import-Module SQLAS -DisableNameChecking -ErrorAction SilentlyContinue 它没有帮助。有任何想法吗?感谢
答案 0 :(得分:1)
由于您最终希望通过任务计划程序运行此脚本,因此我将继续在您希望它所在的服务器上创建任务。在“操作”选项卡中,只需设置以下条件:
计划/脚本字段
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe
添加参数字段:
-NoProfile -ExecutionPolicy unrestricted -nologo -command "&{\\path\to\your\files\FileName.ps1 -Silent:$true}"
如果要使用批处理文件对其进行测试,只需使用以下代码:
schtasks /RUN /s "ServerName" /TN "Scheduled Tasks Named" | Out-String