Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用+;=foreach循环中switch语句的脚本块中_Arrays_Powershell_Foreach_Switch Statement - Fatal编程技术网

Arrays 使用+;=foreach循环中switch语句的脚本块中

Arrays 使用+;=foreach循环中switch语句的脚本块中,arrays,powershell,foreach,switch-statement,Arrays,Powershell,Foreach,Switch Statement,首先,感谢您花时间阅读本文,提前感谢您的帮助 这是我的密码: <# SCCM Request Alert Script #> Import-Module ActiveDirectory $WMIObjects = Get-WmiObject -Namespace 'ROOT\SMS\Site_EUR' -Class SMS_UserApplicationRequest -ComputerName "EUR-SCCM" $FileStore = "c:\export\

首先,感谢您花时间阅读本文,提前感谢您的帮助

这是我的密码:

<#
        SCCM Request Alert Script
#>
Import-Module ActiveDirectory

$WMIObjects = Get-WmiObject -Namespace 'ROOT\SMS\Site_EUR' -Class SMS_UserApplicationRequest -ComputerName "EUR-SCCM"
$FileStore = "c:\export\SCCMRequestfile.txt"
foreach ($Obj in $WMIObjects)
{
        [String]$RequestValue = $Obj.CurrentState
        $Application = $Obj.Application
        $User = $Obj.User -replace 'MYDOMAIN\\',""
        $ADUser = Get-ADUser -Identity $User
        $PendingRequest = @()
        $CancelledRequest = @()
        $DeniedRequest = @()
        $ApprovedRequest = @()
        $Unknown = @()

        $Args = @{ 'User' = $ADUser.Name; 'Application' = $Obj.Application }

        $PR = New-Object -TypeName PSObject -Property $Args
        $CR = New-Object -TypeName PSObject -Property $Args
        $DR = New-Object -TypeName PSObject -Property $Args
        $AR = New-Object -TypeName PSObject -Property $Args
        $UR = New-Object -TypeName PSObject -Property $Args

        switch ($RequestValue) {
                1 { $PendingRequest += $PR }
                2 { $CancelledRequest += $CR }
                3 { $DeniedRequest += $DR }
                4 { $ApprovedRequest += $AR }
        default { $Unknown += $UK }
        }


}

Write-Host -ForegroundColor 'yellow' "Pending Requests "
$PendingRequest
Write-Host -ForegroundColor 'DarkYellow'  "Cancelled Requests "
$CancelledRequest
Write-Host -ForegroundColor 'DarkRed' "Denied Requests "
$DeniedRequest
Write-Host -ForegroundColor 'Green' "Approved Requests "
$ApprovedRequest
Write-Host -ForegroundColor 'White' "Unknown Approval Type "
$Unknown
这会将每个对象放置在数组中

所以我想知道这是switch语句的问题还是我没有做过的事情,比如将它转换为数组

任何帮助都将不胜感激,谢谢


奈杰尔·塔施纳(Nigel Tatschner)

在循环的每次迭代中,您都在初始化数组。移动以下行,使其位于
foreach
循环之前

$PendingRequest = @();
$CancelledRequest = @();
$DeniedRequest = @();
$ApprovedRequest = @();
$Unknown = @();
$PendingRequest = @();
$CancelledRequest = @();
$DeniedRequest = @();
$ApprovedRequest = @();
$Unknown = @();