无法通过PowerShell获取SCCM中设备的部署状态求助
问题:SCCM部署状态无法获取,仅能拉取成员设备
我编写了一段PowerShell脚本,用于查询SCCM中指定Deployment ID的部署,拉取该部署内的所有成员设备并检查其部署状态。脚本能够成功拉取成员设备,但所有设备的部署状态都返回“Unknown”。希望尽量避免使用SQL,仅需每日生成一份可用于对比的报表,求解决状态获取失败的问题。
原脚本
# Import SCCM module Import-Module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + '\ConfigurationManager.psd1') # Change directory to the SCCM site code cd "ABC:\" # Define the deployment ID $deploymentID = "{B4901AE9-8501-4C32-B5BB-7AC098765B63}" # Define the script directory and date-based folder $ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path $date = Get-Date -Format "MM.dd.yyyy" $childFolder = "$ScriptDir\$date" New-Item -ItemType Directory -Path $childFolder -Force # Define the output CSV path $csvPath = "$childFolder\DeploymentStatus-$date.csv" # Create CSV with custom column names $headers = [PSCustomObject]@{ "Member Name" = $null "Deployment Status" = $null } $headers | Export-Csv -Path $csvPath -NoTypeInformation Write-Host "CSV file created with custom column names at $csvPath" # Retrieve deployment status $results = @() $deployments = Get-CMDeployment | Where-Object { $_.DeploymentID -eq $deploymentID } # Debugging step to print out the retrieved deployments Write-Host "Retrieved deployments:" $deployments | Format-Table -AutoSize foreach ($deployment in $deployments) { $devices = Get-CMDevice -CollectionID $deployment.CollectionID foreach ($device in $devices) { # Debugging step to print out the device name Write-Host "Processing device: $($device.Name)" # Retrieve the status for each device using case-insensitive comparison $status = Get-CMDeploymentStatus -DeploymentID $deploymentID | Where-Object { $_.ResourceName -ieq $device.Name # Case-insensitive comparison } if ($status) { # If status is found, output it Write-Host "Status for device $($device.Name): $($status.StatusType)" $result = [PSCustomObject]@{ "Member Name" = $device.Name "Deployment Status" = $status.StatusType } } else { # If no status is found, mark it as "Unknown" Write-Host "No status found for device $($device.Name). Marking as Unknown." $result = [PSCustomObject]@{ "Member Name" = $device.Name "Deployment Status" = "Unknown" } } # Add the result to the results array $results += $result } } # Debugging step to print out the results before exporting Write-Host "Results to be exported:" $results | Format-Table -AutoSize # Export deployment status to CSV $results | Export-Csv -Path $csvPath -NoTypeInformation -Append Write-Host "Deployment status exported to $csvPath" # Open the CSV file Invoke-Item -Path $csvPath # Pause to keep the window open Read-Host -Prompt "Press Enter to exit"
解决方案
问题核心在于Get-CMDeploymentStatus默认返回的是部署的汇总统计数据(比如成功数、失败数),而非单台设备的具体状态。要获取单设备级别的部署状态,需改用Get-CMDeploymentStatusDetails,并通过设备的ResourceID进行精准匹配,同时优化脚本逻辑提升执行效率。
修正后的脚本
# Import SCCM module Import-Module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + '\ConfigurationManager.psd1') # Change directory to the SCCM site code cd "ABC:\" # Define the deployment ID $deploymentID = "{B4901AE9-8501-4C32-B5BB-7AC098765B63}" # Define output directory and CSV path $ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path $date = Get-Date -Format "MM.dd.yyyy" $childFolder = "$ScriptDir\$date" New-Item -ItemType Directory -Path $childFolder -Force $csvPath = "$childFolder\DeploymentStatus-$date.csv" # Get target deployment $deployments = Get-CMDeployment | Where-Object { $_.DeploymentID -eq $deploymentID } if (-not $deployments) { Write-Host "No deployment found with ID: $deploymentID" Read-Host -Prompt "Press Enter to exit" exit } # Get all devices in the deployment's collection $devices = Get-CMDevice -CollectionID $deployments.CollectionID if (-not $devices) { Write-Host "No devices found in the target collection" Read-Host -Prompt "Press Enter to exit" exit } # Get ALL device-level status details for the deployment (one-time call for efficiency) $allStatusDetails = Get-CMDeploymentStatusDetails -DeploymentID $deploymentID # Create a hash table for quick lookups (ResourceID as key) $statusLookup = @{} foreach ($status in $allStatusDetails) { $statusLookup[$status.ResourceID] = $status.StatusType } # Process each device to get status $results = foreach ($device in $devices) { $statusType = $statusLookup[$device.ResourceID] ?? "Unknown" Write-Host "Processing device: $($device.Name) - Status: $statusType" [PSCustomObject]@{ "Member Name" = $device.Name "Deployment Status" = $statusType } } # Export results to CSV (no need to pre-write headers) $results | Export-Csv -Path $csvPath -NoTypeInformation Write-Host "Deployment status exported to $csvPath" # Open the CSV file Invoke-Item -Path $csvPath Read-Host -Prompt "Press Enter to exit"
关键修改说明
- 替换状态查询Cmdlet:用
Get-CMDeploymentStatusDetails替代Get-CMDeploymentStatus,该Cmdlet专门返回单台设备的部署执行状态 - 优化查询效率:一次性获取所有设备的状态详情并存入哈希表,避免在循环中重复调用Cmdlet,大幅提升脚本执行速度
- 精准匹配设备:使用设备的
ResourceID(唯一标识)进行状态匹配,避免设备名称重复、大小写差异导致的匹配失败 - 简化CSV初始化:直接通过结果对象导出CSV,无需单独写入表头,逻辑更简洁
- 增加异常判断:添加部署不存在、设备为空的判断,提升脚本健壮性
内容的提问来源于stack exchange,提问作者Fabricio Martinez
相关产品推荐
相关产品推荐

