powershell - Powershell 函数返回值已更改
问题描述
我对 Powershell 中的函数有疑问。我想归还一个 int32-Value,但 Value 和 Data-Type 总是在变化……我不知道为什么。
那是主要部分,函数被调用的地方..
$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{
#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”称为“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”,每次插入 Checkbox 时我总是递增 1。要填写所有数据,我们有不同的功能:general、cpu、processModel ...要正确处理 Checkboxes,我需要将递增的 $CheckBoxNumber-Variable 传递给下一个函数。
从第一个函数“general”开始,我将函数称为“GenerateTable”,在“GenerateTable”的末尾,变量的值为 2,并且是“int32”。那都是正确的。
现在问题的第一部分:我把 Value 还给了 general-function,$CheckBoxNumber-Variable 突然变成了 Type "Object[]",Value 仍然是 2。
但是当我们回到主要部分时,从第二个功能 CPU 开始的地方,值也变回了 1....
我不知道为什么!我是否以错误的方式将参数返回给函数?我也尝试了返回,但它也没有用......我也尝试用 [int32] 转换值,但后来我得到“无法转换......”失败......有人可以帮我解决这个奇怪的问题吗? ?
解决方案
推荐阅读
- ruby-on-rails - Activeadmin 未初始化常量 Superadmin::DashboardController
- typescript - 基于记录或类似的从 Typescript 类型定义中省略属性
- php - 如何使用 mysql/laravel 处理不同部分的信息
- php - Wordpress如何在相关帖子的循环中间插入一个div标签
- android - 使用通过CMAKE构建的Android NDK时如何获取BUILD TYPE?
- javascript - zustand 什么时候选择器取决于范围?
- python - Spotify 刷新令牌 Python
- swiftui - UINavigationBar 透明
- unicode - 如何在 USB HID 报告中发送重音字符和 unicode
- pine-script - 标签定位和/或对齐