我有一個.bat文件,它可以執行一系列沒有問題的事情。得到它的PowerShell的執行行: PowerShell.exe 「Y:\ DATA \ FS01 \ NoTouch_Deploy.ps1」從.bat文件執行.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 它並沒有幫助。有任何想法嗎?謝謝
添加'#Import-Module'什麼都不做,因爲'#'表示後面的所有內容都是註釋 - 它永遠不會被執行。但是,在v3和更新版本中,這並不重要,因爲如果需要,模塊會自動導入。 – alroc 2014-09-23 15:46:01
我跑了之後評論出來,它什麼也沒做。對不起,我把它粘貼在這裏時留下了評論。我認爲我仍然在V2 – 2014-09-23 15:58:22
批處理文件是如何運行的?你是手動運行還是計劃在不同帳戶下運行的任務?你如何在批處理文件之外通過ISE或控制檯手動運行腳本?你是否在你的ISE配置文件中自動加載沒有加載到控制檯的東西? – TheMadTechnician 2014-09-23 16:03:07