为什么 PowerShell 脚本末尾的返回会有所不同?

发布于 2024-11-03 14:24:12 字数 8396 浏览 5 评论 0原文

第三次编辑:

以下两个脚本之间的唯一区别(除了函数名称之外)是第二个脚本不通过显式返回返回结果。但他们的行为不同。第一个显示网格中的两行

abc
efg

,而第二个显示网格中的行。

脚本 1:

ipmo WPK

$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True"
$conn = new-object System.Data.SQLClient.SQLConnection
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

function Invoke-sql1
{
    param( [string]$sql,
           [System.Data.SQLClient.SQLConnection]$connection
           )
    $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
    $ds = New-Object system.Data.DataSet
    $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
    $da.fill($ds) | Out-Null
    return $ds.tables[0]
}

function Show-Bockmarks ($conn) {
        New-ListView -Name ListView -View {
           New-GridView -AllowsColumnReorder -Columns {
               New-GridViewColumn "title" 
           }
    } -show -On_Loaded {
            $ff_sql = @"
SELECT 'abc' title
UNION
SELECT 'efg' title
"@
            $TableView = $window | Get-ChildControl ListView
            $TableView.ItemsSource = @(Invoke-sql1 -sql $ff_sql -connection $conn)
             } 
}

Show-Bockmarks $conn

脚本 2:

ipmo WPK

$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True"
$conn = new-object System.Data.SQLClient.SQLConnection
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

function Invoke-sql2
{
    param( [string]$sql,
           [System.Data.SQLClient.SQLConnection]$connection
           )
    $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
    $ds = New-Object system.Data.DataSet
    $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
    $da.fill($ds) | Out-Null
    $ds.tables[0]
}

function Show-Bockmarks ($conn) {
        New-ListView -Name ListView -View {
           New-GridView -AllowsColumnReorder -Columns {
               New-GridViewColumn "title" 
           }
    } -show -On_Loaded {
            $ff_sql = @"
SELECT 'abc' title
UNION
SELECT 'efg' title
"@
            $TableView = $window | Get-ChildControl ListView
            $TableView.ItemsSource = @(Invoke-sql2 -sql $ff_sql -connection $conn)
             } 
}

Show-Bockmarks $conn

第一个脚本在网格中显示 2 行,而第二个脚本在网格中显示空行。 第二个似乎与

不使用函数的第三个脚本类似:

ipmo WPK

$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True"
$conn = new-object System.Data.SQLClient.SQLConnection
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

            $ff_sql = @"
SELECT 'abc' title
UNION
SELECT 'efg' title
"@

function Show-Bockmarks ($conn) {
        New-ListView -Name ListView -View {
           New-GridView -AllowsColumnReorder -Columns {
               New-GridViewColumn "title" 
           }
    } -show -On_Loaded {
        $TableView = $window | Get-ChildControl ListView
        $cmd = new-object System.Data.SQLClient.SQLCommand($ff_sql,$conn)
        $ds = New-Object system.Data.DataSet
        $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
        $da.fill($ds) | Out-Null
        $TableView.ItemsSource = @($ds.tables[0].rows)
    } 
}

Show-Bockmarks $conn

请注意,此处不使用函数,我必须显式使用 $ds.tables[0].rows 。否则,我会收到错误

Exception setting "ItemsSource": "Cannot convert the "Table" 
value of type "System.Data.DataTable" 
to type "System.Collections.IEnumerable"."

PowerShell function won't return object 可能会解释为什么会出现这种情况类似于没有返回的函数。但是返回如何使 2 行显示在网格中呢?

原帖:

Invoke-sqlite 和 Invoke-sqlite1 的功能几乎相同。

唯一的区别是 Invoke-sqlite 使用显式返回。 差异非常微妙,当我执行时

$o1   =  Invoke-sqlite $sql $conn
$o2   =  Invoke-sqlite2 $sql $conn

我看不到任何差异。但在下面脚本的完整上下文中,使用 Invoke-sqlite 时,网格中填充有数据,而使用 Invoke-sqlite 时,网格中填充有空行。

顺便说一句:该脚本的目的是在 firefoxplaces.sqlite 历史数据库的副本中搜索包含 1 到 3 个关键字组合的书签。您必须修改 dll 第 5 行的路径和 sqlite 数据库第 8 行的路径。

如果 System.Data.SQLite.dll 出现问题,请参阅 这个

ipmo WPK

if (! $sqlitedll)
{
    $sqlitedll = [System.Reflection.Assembly]::LoadFrom("C:\Program Files\System.Data.SQLite\bin\System.Data.SQLite.dll") 
}

$ConnectionString = "Data Source=C:\Var\sqlite_ff4\places.sqlite"

$conn = new-object System.Data.SQLite.SQLiteConnection 
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

# $sql = "SELECT * from moz_bookmarks t1 where parent = 4 and t1.title = 'sqlite' or t1.title = 'sql'"

function Invoke-sqlite
{
    param( [string]$sql,
           [System.Data.SQLite.SQLiteConnection]$connection
           )
    $cmd = new-object System.Data.SQLite.SQLiteCommand($sql,$connection)
    $ds = New-Object system.Data.DataSet
    $da = New-Object System.Data.SQLite.SQLiteDataAdapter($cmd)
    $da.fill($ds) | Out-Null
    return $ds.tables[0]
}

function Invoke-sqlite2
{
    param( [string]$sql,
           [System.Data.SQLite.SQLiteConnection]$connection
           )
    $cmd = new-object System.Data.SQLite.SQLiteCommand($sql,$connection)
    $ds = New-Object system.Data.DataSet
    $da = New-Object System.Data.SQLite.SQLiteDataAdapter($cmd)
    $da.fill($ds) | Out-Null
    $ds.tables[0]
}

# $o1   =  Invoke-sqlite $sql $conn
# $o2   =  Invoke-sqlite2 $sql $conn


function Show-Bockmarks ($resource) {
    #New-StackPanel -Orientation vertical {
    New-Grid -Rows 2 -Columns 1 -width 1400 -hight 1000 {

        New-StackPanel -Orientation horizontal -column 0 -row 0 -Children {
             New-Label    '1. Keyword'
             New-TextBox  -Name tag1 -width 200
             New-Label    '2. Keyword'
             New-TextBox  -Name tag2 -width 200
             New-Label    '3. Keyword'
             New-TextBox  -Name tag3 -width 200
             New-Button -Name Search "search" -On_Click {
            $text1 = $window | Get-ChildControl Tag1
            $tag1 = $text1.Text
            $text2 = $window | Get-ChildControl Tag2
            $tag2 = $text2.Text
            $text3 = $window | Get-ChildControl Tag3
            $tag3 = $text3.Text
            if ( $tag2 -ne '') {
$clause2 = @"            
    join moz_bookmarks l2 on b.fk = l2.fk and b.id <> l2.id
    join moz_bookmarks t2 on l2.parent = t2.id and  t2.parent = 4 and upper(t2.title) = upper('$tag2')
"@                        
            } else { $clause2 = '' }        

            if ( $tag3 -ne '') {
$clause3 = @"            
    join moz_bookmarks l3 on b.fk = l3.fk and b.id <> l3.id
    join moz_bookmarks t3 on l3.parent = t3.id and  t3.parent = 4 and upper(t3.title) = upper('$tag3')
"@                        
            } else { $clause3 = '' }        

$ff_sql = @"
SELECT b.title, datetime (b.dateAdded / 1000000, 'unixepoch', 'localtime') dateAdded , p.url
    from moz_bookmarks b
    join moz_bookmarks l1 on b.fk = l1.fk and b.id <> l1.id
    join moz_bookmarks t1 on l1.parent = t1.id and  t1.parent = 4 and upper(t1.title) = upper('$tag1')
    join moz_places p  on b.fk = p.id $clause2 $clause3
where b.title is not null and b.type = 1
"@
#             $query = $window | Get-ChildControl query
#             $query.text = $ff_sql
            $conn = $resource.conn
            $window.Title = "$($conn.database) Database Browser"
            $TableView = $window | Get-ChildControl TableView
            $TableView.ItemsSource = Invoke-sqlite -sql $ff_sql -connection $conn
             } 
#             New-textbox -Name query 
             New-Button -Name Cancel "Close" -On_Click {$window.Close()} 
        }
        # -VerticalScrollBar $True 
        # New-ScrollViewer {
        New-ListView -Column 0 -Row 1 -Name TableView -View {
           New-GridView -AllowsColumnReorder -Columns {
               New-GridViewColumn "title" 
               New-GridViewColumn "dateAdded" 
               New-GridViewColumn "url" 
           }
        }   -On_SelectionChanged {
             start $this.selecteditem.url
        }
        #}

    } -asjob -Resource $resource
}

Show-Bockmarks -resource @{conn = $conn}

3rd Edit:

The only difference between the following two scripts (besides the name of the function) is the fact, that the second doesn't return the result via explicit return. But they behave different. The first shows the two rows

abc
efg

in the grid, while the second shows to empty rows in the grid.

Skript 1:

ipmo WPK

$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True"
$conn = new-object System.Data.SQLClient.SQLConnection
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

function Invoke-sql1
{
    param( [string]$sql,
           [System.Data.SQLClient.SQLConnection]$connection
           )
    $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
    $ds = New-Object system.Data.DataSet
    $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
    $da.fill($ds) | Out-Null
    return $ds.tables[0]
}

function Show-Bockmarks ($conn) {
        New-ListView -Name ListView -View {
           New-GridView -AllowsColumnReorder -Columns {
               New-GridViewColumn "title" 
           }
    } -show -On_Loaded {
            $ff_sql = @"
SELECT 'abc' title
UNION
SELECT 'efg' title
"@
            $TableView = $window | Get-ChildControl ListView
            $TableView.ItemsSource = @(Invoke-sql1 -sql $ff_sql -connection $conn)
             } 
}

Show-Bockmarks $conn

Script 2:

ipmo WPK

$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True"
$conn = new-object System.Data.SQLClient.SQLConnection
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

function Invoke-sql2
{
    param( [string]$sql,
           [System.Data.SQLClient.SQLConnection]$connection
           )
    $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
    $ds = New-Object system.Data.DataSet
    $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
    $da.fill($ds) | Out-Null
    $ds.tables[0]
}

function Show-Bockmarks ($conn) {
        New-ListView -Name ListView -View {
           New-GridView -AllowsColumnReorder -Columns {
               New-GridViewColumn "title" 
           }
    } -show -On_Loaded {
            $ff_sql = @"
SELECT 'abc' title
UNION
SELECT 'efg' title
"@
            $TableView = $window | Get-ChildControl ListView
            $TableView.ItemsSource = @(Invoke-sql2 -sql $ff_sql -connection $conn)
             } 
}

Show-Bockmarks $conn

The first script shows 2 rows in a grid, while the 2nd show to empty rows in the grid.
The second seems to behave similar to this

3rd script not using function:

ipmo WPK

$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True"
$conn = new-object System.Data.SQLClient.SQLConnection
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

            $ff_sql = @"
SELECT 'abc' title
UNION
SELECT 'efg' title
"@

function Show-Bockmarks ($conn) {
        New-ListView -Name ListView -View {
           New-GridView -AllowsColumnReorder -Columns {
               New-GridViewColumn "title" 
           }
    } -show -On_Loaded {
        $TableView = $window | Get-ChildControl ListView
        $cmd = new-object System.Data.SQLClient.SQLCommand($ff_sql,$conn)
        $ds = New-Object system.Data.DataSet
        $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
        $da.fill($ds) | Out-Null
        $TableView.ItemsSource = @($ds.tables[0].rows)
    } 
}

Show-Bockmarks $conn

Note here without use of a function, I have to explicitly use $ds.tables[0].rows. Otherwise I get the error

Exception setting "ItemsSource": "Cannot convert the "Table" 
value of type "System.Data.DataTable" 
to type "System.Collections.IEnumerable"."

PowerShell function won't return object might explain, why this behaves similar to the function without return. But how does the return make the 2 rows show in the grid?

Original post:

The functions Invoke-sqlite and Invoke-sqlite1 are nearly the same.

The only difference is that Invoke-sqlite uses explicit return.
The difference is very subtile, when I execute

$o1   =  Invoke-sqlite $sql $conn
$o2   =  Invoke-sqlite2 $sql $conn

I can't see any difference. But in the full context of the script below, with Invoke-sqlite the grid is filled with data and with Invoke-sqlite the grid is filled with empty rows.

BTW: The purpose of the script is to search a copy of a firefox places.sqlite history database for bookmarks with combinations of 1 to 3 keywords. You have to modify the path for the dll line 5 and the path for the sqlite database line 8.

If you have problems with System.Data.SQLite.dll see this

ipmo WPK

if (! $sqlitedll)
{
    $sqlitedll = [System.Reflection.Assembly]::LoadFrom("C:\Program Files\System.Data.SQLite\bin\System.Data.SQLite.dll") 
}

$ConnectionString = "Data Source=C:\Var\sqlite_ff4\places.sqlite"

$conn = new-object System.Data.SQLite.SQLiteConnection 
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

# $sql = "SELECT * from moz_bookmarks t1 where parent = 4 and t1.title = 'sqlite' or t1.title = 'sql'"

function Invoke-sqlite
{
    param( [string]$sql,
           [System.Data.SQLite.SQLiteConnection]$connection
           )
    $cmd = new-object System.Data.SQLite.SQLiteCommand($sql,$connection)
    $ds = New-Object system.Data.DataSet
    $da = New-Object System.Data.SQLite.SQLiteDataAdapter($cmd)
    $da.fill($ds) | Out-Null
    return $ds.tables[0]
}

function Invoke-sqlite2
{
    param( [string]$sql,
           [System.Data.SQLite.SQLiteConnection]$connection
           )
    $cmd = new-object System.Data.SQLite.SQLiteCommand($sql,$connection)
    $ds = New-Object system.Data.DataSet
    $da = New-Object System.Data.SQLite.SQLiteDataAdapter($cmd)
    $da.fill($ds) | Out-Null
    $ds.tables[0]
}

# $o1   =  Invoke-sqlite $sql $conn
# $o2   =  Invoke-sqlite2 $sql $conn


function Show-Bockmarks ($resource) {
    #New-StackPanel -Orientation vertical {
    New-Grid -Rows 2 -Columns 1 -width 1400 -hight 1000 {

        New-StackPanel -Orientation horizontal -column 0 -row 0 -Children {
             New-Label    '1. Keyword'
             New-TextBox  -Name tag1 -width 200
             New-Label    '2. Keyword'
             New-TextBox  -Name tag2 -width 200
             New-Label    '3. Keyword'
             New-TextBox  -Name tag3 -width 200
             New-Button -Name Search "search" -On_Click {
            $text1 = $window | Get-ChildControl Tag1
            $tag1 = $text1.Text
            $text2 = $window | Get-ChildControl Tag2
            $tag2 = $text2.Text
            $text3 = $window | Get-ChildControl Tag3
            $tag3 = $text3.Text
            if ( $tag2 -ne '') {
$clause2 = @"            
    join moz_bookmarks l2 on b.fk = l2.fk and b.id <> l2.id
    join moz_bookmarks t2 on l2.parent = t2.id and  t2.parent = 4 and upper(t2.title) = upper('$tag2')
"@                        
            } else { $clause2 = '' }        

            if ( $tag3 -ne '') {
$clause3 = @"            
    join moz_bookmarks l3 on b.fk = l3.fk and b.id <> l3.id
    join moz_bookmarks t3 on l3.parent = t3.id and  t3.parent = 4 and upper(t3.title) = upper('$tag3')
"@                        
            } else { $clause3 = '' }        

$ff_sql = @"
SELECT b.title, datetime (b.dateAdded / 1000000, 'unixepoch', 'localtime') dateAdded , p.url
    from moz_bookmarks b
    join moz_bookmarks l1 on b.fk = l1.fk and b.id <> l1.id
    join moz_bookmarks t1 on l1.parent = t1.id and  t1.parent = 4 and upper(t1.title) = upper('$tag1')
    join moz_places p  on b.fk = p.id $clause2 $clause3
where b.title is not null and b.type = 1
"@
#             $query = $window | Get-ChildControl query
#             $query.text = $ff_sql
            $conn = $resource.conn
            $window.Title = "$($conn.database) Database Browser"
            $TableView = $window | Get-ChildControl TableView
            $TableView.ItemsSource = Invoke-sqlite -sql $ff_sql -connection $conn
             } 
#             New-textbox -Name query 
             New-Button -Name Cancel "Close" -On_Click {$window.Close()} 
        }
        # -VerticalScrollBar $True 
        # New-ScrollViewer {
        New-ListView -Column 0 -Row 1 -Name TableView -View {
           New-GridView -AllowsColumnReorder -Columns {
               New-GridViewColumn "title" 
               New-GridViewColumn "dateAdded" 
               New-GridViewColumn "url" 
           }
        }   -On_SelectionChanged {
             start $this.selecteditem.url
        }
        #}

    } -asjob -Resource $resource
}

Show-Bockmarks -resource @{conn = $conn}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如何视而不见 2024-11-10 14:24:12

为了提供帮助,我在 SQLClient 之上写了同样的东西,它似乎工作得很好。

function func1
{
  param( [string]$sql,
         [System.Data.SQLClient.SQLConnection]$connection
     )


  $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$conn)
  $ds = New-Object system.Data.DataSet
  $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
  $da.fill($ds) | Out-Null

  $ds.Tables[0]
}

function func2
{
  param( [string]$sql,
       [System.Data.SQLClient.SQLConnection]$connection
     )


  $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$conn)
  $ds = New-Object system.Data.DataSet
  $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
  $da.fill($ds) | Out-Null

  return $ds.Tables[0]
}

Clear-Host

$sqldll = [reflection.assembly]::loadwithpartialname("System.Data.SqlClient")
$connectionString="Data Source=JPBHPP2\SQLEXPRESS;Initial Catalog=Ardeche-Earth;Integrated Security=True"

$conn = new-object System.Data.SQLClient.SQLConnection 
$sql = "SELECT * FROM communes WHERE COM_NCC LIKE 'AURI%'"
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

$a = func1 -sql $sql -connection $conn
$b = func2 -sql $sql -connection $conn

Write-Host "`$a : $($a.count)"
Write-Host "`$b : $($b.count)"

$conn.Close() 

$a 和 $b 在末尾保存相同的 System.Data.DataRow 数组

In order to help, I wrote the same thing on the top of SQLClient and it seems to work fine.

function func1
{
  param( [string]$sql,
         [System.Data.SQLClient.SQLConnection]$connection
     )


  $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$conn)
  $ds = New-Object system.Data.DataSet
  $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
  $da.fill($ds) | Out-Null

  $ds.Tables[0]
}

function func2
{
  param( [string]$sql,
       [System.Data.SQLClient.SQLConnection]$connection
     )


  $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$conn)
  $ds = New-Object system.Data.DataSet
  $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
  $da.fill($ds) | Out-Null

  return $ds.Tables[0]
}

Clear-Host

$sqldll = [reflection.assembly]::loadwithpartialname("System.Data.SqlClient")
$connectionString="Data Source=JPBHPP2\SQLEXPRESS;Initial Catalog=Ardeche-Earth;Integrated Security=True"

$conn = new-object System.Data.SQLClient.SQLConnection 
$sql = "SELECT * FROM communes WHERE COM_NCC LIKE 'AURI%'"
$conn.ConnectionString = $ConnectionString 
$conn.Open() 

$a = func1 -sql $sql -connection $conn
$b = func2 -sql $sql -connection $conn

Write-Host "`$a : $($a.count)"
Write-Host "`$b : $($b.count)"

$conn.Close() 

$a and $b holds the same array of System.Data.DataRow at the end

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文