脚本的哪一部分正在打印目录信息?

时间:2018-09-12 05:24:24

标签: sql-server powershell-v5.0

我在PowerShell脚本(Process.ps1)中具有以下内容,该脚本从SQL表中读取并将结果附加到列出的变量中:

function Query($Query) {
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
    $SqlConnection.ConnectionString = "Server=$Server;Initial Catalog=$Database;Integrated Security=SSPI" 
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand 
    $SqlCmd.Connection = $SqlConnection 
    $SqlCmd.CommandText = $Query 
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter 
    $SqlAdapter.SelectCommand = $SqlCmd 
    $DataSet = New-Object System.Data.DataSet 
    $a=$SqlAdapter.Fill($DataSet)
    $SqlConnection.Close() 
    $DataSet.Tables[0]
}

$Result = Query "SELECT * from [$cubeTable]" | Out-GridView -Wait;

$CUBE = Query "SELECT [cube_name] FROM [$cubeTable] WHERE [cube_name] = '$CUBE_input'" | Select -ExpandProperty cube_name;

$Destination_Server = Query "SELECT [destination_server] FROM [$cubeTable] WHERE [cube_name] = '$CUBE'" | Select -ExpandProperty destination_server;

$BasePath = Query "SELECT [variable_value] FROM [$pathTable] WHERE [variable_name] = 'base_path'" | Select -ExpandProperty variable_value;

$jsonPath = Join-Path -Path $BasePath -ChildPath $jsonDirectory
New-Item -ItemType Directory -Force -Path $jsonPath

$JSON_file = Join-Path $jsonPath $CUBE |
             %{ ($_ + ".json") }

$processPATH = Join-Path -Path $BasePath -ChildPath $process_output_Directory
New-Item -ItemType Directory -Force -Path $processPATH

$process_output = Join-Path $processPATH $CUBE |
                  %{ ($_ + ".txt") }

$autosysPATH = Join-Path -Path $BasePath -ChildPath $AUTOSYS_output_Directory
New-Item -ItemType Directory -Force -Path $autosysPATH

$process_AUTOSYS_output = Join-Path $autosysPATH $CUBE |
                          %{ ($_ + "_process.txt") }

当我通过批处理文件在CMD中运行脚本时,它可以正常运行,但是,它从以下变量的某个位置输出这些目录信息:

  

json_file,process_output和autosys_output

我在这里有一个输出图像:output

编写控制台的确切位置在哪里?我没有回声或Write-Host!更不用说输出目录的功能了...

,它绝对不是这部分:$Result = Query "SELECT * from [$cubeTable]" | Out-GridView -Wait;,因为我将其注释掉了,并且仍然如截图所示输出目录信息。

1 个答案:

答案 0 :(得分:0)

New-Item返回创建的FileInfoDirectoryInfo对象。那就是您在输出中看到的。 PowerShell默认输出格式仅合并相似的连续对象以提供更紧凑的输出,因此您将获得一个表,而不是三个单独的表,每个表只有一个对象。

您可以通过在| Out-Null语句中添加New-Item来抑制输出:

New-Item -ItemType Directory -Force -Path $jsonPath | Out-Null

其他选项可能是捕获变量中的输出或使用重定向(> $null)。