我已尝试过以下链接中的所有推荐,但没有任何效果。
How do you do a ‘Pause’ with PowerShell 2.0?
这是我的代码。
Read-Host -Prompt "Press Enter to continue"
Invoke-Sqlcmd -Query "Select * from TBL_AGG_BOC" -ServerInstance "server_name\SQL2008" -Database "db_name" > "C:\Users\TBL_AGG_BOC.txt"
Read-Host -Prompt "Press Enter to continue"
脚本保持失败,但是它关闭得如此之快,以至于我无法看到实际发生的情况。我怎么能强迫它保持开放几秒钟,这样我才能看到这里发生了什么?该文件已保存并命名为'test_export.ps1'
,我右键单击该文件,然后单击'Run with PowerShell'
将其关闭。我没有此计算机的管理员权限。我希望这不是问题。
答案 0 :(得分:0)
最终得到了这个工作。这是一些事情的组合。
1) Open PowerShell as Administrator and run Set-ExecutionPolicy -Scope CurrentUser
2) Provide RemoteSigned and press Enter
3) Run Set-ExecutionPolicy -Scope CurrentUser
4) Provide Unrestricted and press Enter
此链接帮了很多忙!
PowerShell says "execution of scripts is disabled on this system."
然后,运行:
$server = "SERVERNAME\INSTANCE"
$database = "DATABASE_NAME"
$tablequery = "SELECT name from sys.tables"
#Delcare Connection Variables
$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $tablequery
$command.Connection = $connection
#Load up the Tables in a dataset
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()
# Loop through all tables and export a CSV of the Table Data
foreach ($Row in $DataSet.Tables[0].Rows)
{
$queryData = "SELECT * FROM [$($Row[0])]"
#Specify the output location of your dump file
$extractFile = "C:\mssql\export\$($Row[0]).csv"
$command.CommandText = $queryData
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()
$DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation
}
事实证明是这样的:$server = "SERVERNAME"
不是这个:$server = "SERVERNAME\INSTANCE"
在我将这些变化合并后,一切正常。