我如何暂停PowerShell以查看错误



我尝试了下面链接中的所有建议,但没有任何作用。

您如何使用PowerShell 2.0进行"暂停"?

这是我的代码:

Read-Host -Prompt "Press Enter to continue"
Invoke-Sqlcmd -Query "Select * from TBL_AGG_BOC" -ServerInstance "server_nameSQL2008" -Database "db_name" > "C:UsersTBL_AGG_BOC.txt"
Read-Host -Prompt "Press Enter to continue"

脚本不断失败,它闭嘴如此之快,以至于我看不到实际发生的事情。我该如何强迫它保持几秒钟,以便我可以看到这里发生了什么?

文件保存并命名为 'test_export.ps1',然后右键单击文件,然后单击'Run with PowerShell'将其发射。我在这台计算机上没有管理员权利。我希望这不是问题。

最后我得到了工作。这是几件事的结合。

  1. 打开powerShell作为管理员并运行Set-ExecutionPolicy -Scope CurrentUser
  2. 提供RemoteSigned并按 Enter
  3. 运行Set-ExecutionPolicy -Scope CurrentUser
  4. 提供Unrestricted并按 Enter

此链接有很大帮助!

powershell说"执行脚本在此系统上被禁用。"

然后,运行此:

$server = "SERVERNAMEINSTANCE"
$database = "DATABASE_NAME"
$tablequery = "SELECT name from sys.tables"
# Declare 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:mssqlexport$($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 = "SERVERNAMEINSTANCE"

我整合了这些更改后,一切都很好。