在powershell中实现使用时出错

时间:2013-03-26 17:18:43

标签: powershell using

当我尝试在Powershell中实现使用功能时发布this

Function cUsing {
    param (
        [System.IDisposable] $inputObject = $(throw "The parameter -inputObject is required."),
        [ScriptBlock] $scriptBlock = $(throw "The parameter -scriptBlock is required.")
    )
    Try { &$scriptBlock }
    Finally {
        if ($inputObject -ne $null) {
            if ($inputObject.psbase -eq $null) {
                $inputObject.Dispose()
            } else {
                $inputObject.psbase.Dispose()
            }
        }
    }
}


    cUsing($sqlConnection = New-Object System.Data.SqlClient.SqlConnection)
{
$sqlConnection.ConnectionString = "Server=myserver; Database=master; Integrated Security= True"
$sqlConnection.Open()
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandText = "SELECT STATEMENT"
$sqlCmd.Connection = $sqlConnection
$dsValues = New-Object System.Data.DataSet
$daValues = New-Object System.Data.SqlClient.SqlDataAdapter($sqlCmd)
$daValues.Fill($dsValues)
Write-Host $dsValues.Tables[0]
}

错误:

[ScriptBlock] $scriptBlock = $(throw  <<<< "The parameter -scriptBlock is required.")

修改[Graimer]

当我移动下面的花括号

cUsing($sqlConnection = New-Object System.Data.SqlClient.SqlConnection){
....
....
 Write-Host $dsValues.Tables[0]}

我没有收到任何错误,但我得到的输出是

    $sqlConnection.ConnectionString = "Server=myserver; Database=master; Integrated Security= True"
$sqlConnection.Open()
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandText = "Select query"
$sqlCmd.Connection = $sqlConnection
$dsValues = New-Object System.Data.DataSet
$daValues = New-Object System.Data.SqlClient.SqlDataAdapter($sqlCmd)
$daValues.Fill($dsValues)
Write-Host $dsValues.Tables[0]

编辑[Ansgar Wiechers]

当我将scriptblock行更改为

Function cUsing {
param (
    [System.IDisposable] $inputObject = $(throw "The parameter -inputObject is required."),
    [ScriptBlock] $scriptBlock = ${throw "The parameter -scriptBlock is required."}
)

我没有收到任何错误,但我得到的结果与上面编辑中显示的相同。

3 个答案:

答案 0 :(得分:0)

脚本块是通过花括号定义的,而不是通过$()定义的。像这样:

[ScriptBlock]$scriptBlock = { throw "The parameter ..." }

$()是评估子表达式的运算符。

答案 1 :(得分:0)

    Add-Type @"
        using System;

        public class Code : System.IDisposable
        {
            public bool IsDisposed { get; set; }
            public void Dispose() 
            {
                Dispose(true);
                GC.SuppressFinalize(this);      
            }

            protected virtual void Dispose(bool disposing)
            {
                if (!IsDisposed)
                {
                    IsDisposed = true;   
                }
            }
        }
    "@

    function using-block {
        param (
            [System.IDisposable]$InputObject = $(throw "The parameter -inputObject is required."),
            [ScriptBlock]$ScriptBlock = $(throw "The parameter -scriptBlock is required.")
        )
        try { &$ScriptBlock }
        finally {
            if ($InputObject) {
                if ($InputObject.PSBase) {
                    $InputObject.PSBase.Dispose()
                } else {
                    $InputObject.Dispose()
                }
            }
        }
    }

    using-block($c = New-Object Code) {
        $c.IsDisposed
    }
    $c.IsDisposed

输出:

答案 2 :(得分:0)

您的代码存在的问题是您不将scriptblock作为参数提供,而是将其作为命令提供给它自己。 Powershell中的命令在一行中。您的代码仅使用using参数运行命令,并且失败。然后你宣布一个scriptblock。

这里有两个选择;逃避换行(BAD练习),或将开口花括号向上移动一行,使其被视为cusing命令的一部分。像这样:

cUsing($sqlConnection = New-Object System.Data.SqlClient.SqlConnection) {
    $sqlConnection.ConnectionString = "Server=myserver; Database=master; Integrated Security= True"
...
..
.
}