从表中依次获取结果foreach?

时间:2018-09-29 10:47:57

标签: powershell

我有以下Powershell脚本,应该在对表中的顺序进行排序后在多维数据集上运行一个处理命令

param($App_input, $Script) #%2 and %3 arguments from cmd line input;

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] }

#$cube = Query "SELECT DISTINCT cube_name, sequence FROM [dbo].[$cubeTable] WHERE [application] = '$App_input' ORDER BY [sequence]" | Select -ExpandProperty cube_name;

$Table = Query "SELECT * from [dbo].[$cubeTable]"

                  # -ExpandProperty trims the column name, so it only outputs the value, not with header!

$App = Query "SELECT DISTINCT[application] FROM [dbo].[$cubeTable] WHERE [application] = '$App_input'" | Select -ExpandProperty application;

    $i=0
    $CUBE = @()

    foreach($row in $Table | where { $_.application -match $App })
    {   
        $i++
        $CUBE += $row.Item("cube_name")
        write-host " > CUBE $($i):" $CUBE[$CUBE -1]
        cmd /c "runPowerShell.bat $Script $CUBE"    #where $Script is Process.ps1 script that processes on each CUBE selected from table in order
    }

这是假设的表格:

Table

例如:如果我要像这样传递app参数和script参数:

someBatchFile.bat SSP Process.ps1

脚本首先应检查循环是否仅在[application]列= SSP的情况下返回结果,因此在这种情况下避免使用第三行。但是,出于某种原因我拥有$_.application -match $App的where子句没有任何效果,并且无论如何,powershell都会返回所有行...

完成检查后,脚本应对行进行排序(从2、2、1到1、2、2),并返回CUBE2,CUBE1(距其不同仅一次),并在每次迭代中传递给{{ 1}}。

本质上,在此示例中,迭代应如下所示:

  

cmd / c“ runPowerShell.bat $ Process.ps1 CUBE2”

     

cmd / c“ runPowerShell.bat $ Process.ps1 CUBE1”

在这种情况下,我知道order by会有所帮助,但是在foreach和$CUBE

的情况下,我不知道如何使用它

3 个答案:

答案 0 :(得分:0)

您的Query函数返回DataTable类型的对象,这意味着$Table变量也是DataTable

对于DataTable类型的变量,您想使用Select()方法来获取过滤结果。像这样:

$Table.Select("application = '$App'")

也。查看您的整个代码,这对我来说真的很奇怪。

param($App_input, $Script)

$App = Query "SELECT DISTINCT[application] FROM [dbo].[$cubeTable] WHERE [application] = '$App_input'" | Select -ExpandProperty application;

您正在将 $ application 传递给脚本,以便下次将其传递给查询,该查询应从数据库表中返回 application 值,而该查询的过滤条件等于应用值。

您可以只使用应用值。

答案 1 :(得分:0)

我知道了:

我本来可以在$Table

中完成检查
$Table = Query "SELECT * from [dbo].[$cubeTable] WHERE [application] = '$App_input' AND [active] = 'TRUE' ORDER BY [sequence]"

    $i=0
    $CUBE = @()

    foreach($row in $Table)
    {   
        $i++
        $CUBE += $row.Item("cube_name")
        write-host " > CUBE $($i): $($CUBE[-1])`n"
        cmd /c "runPowerShell.bat $Script $($CUBE[-1])" #where $Script is Process.ps1 script that processes on each CUBE selected from table in order
    }

答案 2 :(得分:0)

尝试

$App = Query "SELECT DISTINCT cube_name FROM [dbo].[$cubeTable] WHERE [application] = '$App_input'"
$i=0

$App | %{

    $i++
    $CUBE=$_.Item("cube_name")
    " > CUBE $i : $CUBE"
    cmd /c "runPowerShell.bat $Script $CUBE"
}