我正在从未安装SQLPS(Ms SQL Server数据库引擎)的客户端计算机(Win7)运行PS脚本。
该脚本正在从具有相同版本的两个不同SQL服务器(我们称之为源和目标)执行大量SQL查询。
由于客户端计算机没有安装SQLPS,我决定使用import-pssession功能。
不幸的是,当我第三次/第四次导入SQLPS模块时,它会抛出“算术运算导致溢出”错误。
为了简化我想要的内容,以下是我的代码片段:
#SQL Query to find out the default folder for sql backup on the SQL SERVER
$tempSQLQuery = "EXECUTE [master].dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\MSSQLServer', N'BackupDirectory'"
#Source
#Get SQL App Server Credential
$MyCredential = Get-Credential -UserName $($sourceSQLServer + "\" + $sourceSQLServerUsername) -Message "Enter SQL application server Username and Password"
Write-Output "Import SQLPS from source SQL app server"
$Global:P_sourceSQLRemoteSession = New-PSSession -ComputerName $sourceSQLServer -Authentication negotiate -Credential $MyCredential
$sessionID = Import-PSSession -Session $Global:P_sourceSQLRemoteSession -Module SQLPS -DisableNameChecking -AllowClobber
Write-Output "Run query against source SQL Server"
#Run query against source SQL
Invoke-Sqlcmd -ServerInstance $sourceSQLServerInstance -username $sourceSQLServerSA -password $sourceSQLServerSAPassword -query $tempSQLQuery
#Destination
#Get SQL App Server Credential
$MyCredential = Get-Credential -UserName $($destinationSQLServer + "\" + $destinationSQLServerUsername) -Message "Enter SQL application server Username and Password"
Write-Output "Import SQLPS from destination SQL app server"
$Global:P_destinationSQLRemoteSession = New-PSSession -ComputerName $destinationSQLServer -Authentication negotiate -Credential $MyCredential
$sessionID = Import-PSSession -Session $Global:P_destinationSQLRemoteSession -Module SQLPS -DisableNameChecking -AllowClobber
Write-Output "Run query against destination SQL Server"
#Run the same query, against destination SQL server
Invoke-Sqlcmd -ServerInstance $destinationSQLServerInstance -username $destinationSQLServerSA -password $destinationSQLServerSAPassword -query $tempSQLQuery
#Remove session / cleanup
#Remove-PSSession -Session $Global:P_destinationSQLRemoteSession
现在,假设我没有从目标SQL服务器导入SQLPS:
#SQL Query to find out the default folder for sql backup on the SQL SERVER
$tempSQLQuery = "EXECUTE [master].dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\MSSQLServer', N'BackupDirectory'"
#Source
#Get SQL App Server Credential
$MyCredential = Get-Credential -UserName $($sourceSQLServer + "\" + $sourceSQLServerUsername) -Message "Enter SQL application server Username and Password"
Write-Output "Import SQLPS from source SQL app server"
$Global:P_sourceSQLRemoteSession = New-PSSession -ComputerName $sourceSQLServer -Authentication negotiate -Credential $MyCredential
$sessionID = Import-PSSession -Session $Global:P_sourceSQLRemoteSession -Module SQLPS -DisableNameChecking -AllowClobber
Write-Output "Run query against source SQL Server"
#Run query against source SQL
Invoke-Sqlcmd -ServerInstance $sourceSQLServerInstance -username $sourceSQLServerSA -password $sourceSQLServerSAPassword -query $tempSQLQuery
#Remove session / cleanup
#Remove-PSSession -Session $Global:P_sourceSQLRemoteSession
#Destination - with same SQLPS module from source SQL app server
#Get SQL App Server Credential
$MyCredential = Get-Credential -UserName $($destinationSQLServer + "\" + $destinationSQLServerUsername) -Message "Enter SQL application server Username and Password"
Write-Output "Run query against destination SQL Server using source SQL imported module"
#Run the same query, against destination SQL server
Invoke-Sqlcmd -ServerInstance $destinationSQLServerInstance -username $destinationSQLServerSA -password $destinationSQLServerSAPassword -query $tempSQLQuery**
导致以下错误: 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供程序:SQL网络接口,错误:26 - 查找指定的服务器/实例时出错)
您认为可能导致此问题的原因是什么? 你能想到一种替代方法来实现我想要的吗?我应该只有运行PS1脚本的客户端机器来安装SQL模块/附加组件吗?
谢谢堆!