Powershell函数的返回值已更改

时间:2018-10-22 10:01:43

标签: powershell

我对Powershell中的功能有疑问。我想退还一个int32值,但是值和数据类型总是在变化……我不知道为什么。

那是调用函数的主要部分。

$DocPath = "\\CHVMES02\Registry\EXCEL_ALLE\Version_AllFromExcel_Aktuell"
$server = "chvmes01"
$Building = "E44"
$AID = "N/A"
$Anlage = "Engineering"

$applicationPoolsPath = "/system.applicationHost/applicationPools"
$applicationPools = Get-WebConfiguration $applicationPoolsPath

foreach ($appPool in $applicationPools.Collection)
{
    #[int32]$CheckBoxNumber = 1

    $appPoolPath = "$applicationPoolsPath/add[@name='$($appPool.Name)']"


    $Word = New-Object -ComObject Word.Application
    $Word.Visible = $True
    $Document = $Word.Documents.Add($DocPath + "\IIS Application Pool.dotm")
    $Selection = $Word.Selection

    $CheckBoxNumber = general

    $CheckBoxNumber = cpu

    processModel

    ProcessOrphaning
}

现在首先将Function General称为:

Function general{
        #General Informations

        $General = Get-ItemProperty IIS:\AppPools\DefaultAppPool | Select *  
        $AppPool = $appPool.Name
        $Category = "General"

        $hash = @{
                    0x0 = $General.managedRuntimeVersion
                    0x1 = $General.enable32BitAppOnWin64
                    0x2 = $General.managedPipelineMode
                    0x3 = $General.queueLength
                    0x4 = $General.startMode
                    0x5 = $appPool

        }

        $columns = @{
                        0x0 = "CLR-Version"
                        0x1 = "Enable 32bit"
                        0x2 = "Managed Pipeline Mode"
                        0x3 = "Start Mode"
                        0x4 = "Queue Length"
                        0x5 = "Name"
        }

        $CheckBoxNumber = GenerateTable $hash $columns $AppPool $Server $Category

        $Type = $CheckBoxNumber.GetType()

        $CheckBoxNumber
}

从函数常规内部调用函数“ GenerateTable”,正如名称中所说,该函数生成表并填写数据...

Function GenerateTable{
        Param(
            [Parameter(Mandatory=$True, Position=1)]
            [HashTable]$hash,

            [Parameter(Mandatory=$True, Position=2)]
            [HashTable]$Columns,

            [Parameter(Mandatory=$True, Position=3)]
            [string]$AppPool,

            [Parameter(Mandatory=$True, Position=4)]
            [string]$Server,

            [Parameter(Mandatory=$True, Position=5)]
            [string]$Category
        )

        $Range = @($Selection.Paragraphs)[-1].Range
        $a = $Selection.MoveDown(5, 1000)

        $RowsCount = $hash.Count +1

        $Table = $Selection.Tables.add(
        $Selection.Range,($RowsCount),2,
        [Microsoft.Office.Interop.Word.WdDefaultTableBehavior]::wdWord9TableBehavior,
        [Microsoft.Office.Interop.Word.WdAutoFitBehavior]::wdAutoFitFixed)

        $tblTitle = $AppPool + ": " + $Category + ", Server: " + $Server + " - " + "Datasource: IIS Manager"

        $Table.Cell(1,1).Merge($Table.Cell(1,2))
        $Table.cell(1,1).range.Bold=1
        $Table.cell(1,1).range.text = $tblTitle

        $ColNr = 2
        foreach ($h in $Columns.GetEnumerator() | Sort Key) {
            $Value = $h.value

            $Table.cell($ColNr,1).range.text = $Value
            $ColNr = $ColNr+1
        }

        $ColNr = 2

        foreach ($x in $Hash.GetEnumerator() | Sort Key) {
            $Value = [string]$x.value

            if(([string]::IsNullOrEmpty($Value))){
                $Value = "N/A"
            }

            if($Value -eq $false -or $Value -eq $true){

                $Range = $Table.cell($ColNr,2).range
                $Document.FormFields.Add($Range, 71) 
                $CheckBox = "Check" + $CheckBoxNumber 
                $Type = $CheckBoxNumber.GetType()
                $CheckBoxNumber = $CheckBoxNumber + 1 
                $Type = $CheckBoxNumber.GetType()

                if($Value -eq $true){
                    $Document.FormFields($Checkbox).Checkbox.Value = True
                }
                Elseif($Value -eq $false){
                    $Document.FormFields($Checkbox).Checkbox.Value = False
                }
            }
            Else {
                $Table.cell($ColNr,2).range.text = [string]$Value
            }

            $ColNr = $ColNr+1
        }

        $a = $Selection.MoveDown(5, 1000)
        $Selection.Text = "`n`n"      

        $CheckBoxNumber

}

我有一个变量“ $ CheckBoxNumber”,每次插入一个复选框时,它总是加1。 要填充所有数据,我们具有不同的功能:常规,cpu,processModel ... 为了正确处理Checkbox,我需要将递增的$ CheckBoxNumber-Variable传递给下一个函数。

在第一个函数“ general”中,我将函数称为“ GenerateTable”,在“ GenerateTable”的末尾,变量的值为2,为“ int32”。没错。

现在问题的第一部分:我将值返回给通用函数,$ CheckBoxNumber-Variable突然更改为类型“ Object []”,值仍为2。

但是当我们回到主要部分时,从第二个功能CPU开始启动的地方,“值”也变回了1 ....

我不知道为什么!我是否以错误的方式将参数返回给函数?我也尝试过使用return,但是它也没有起作用...我也尝试过使用[int32]转换值,但是随后我遇到了“无法转换...”失败的消息。有人可以帮助我解决这个奇怪的问题吗? ?

0 个答案:

没有答案