Fabricio Martinez
Fabricio Martinez

Reputation: 27

Unable to Get Deployment Status of Devices in SCCM via PowerShell

I have created a powershell script that checks a Deployment ID in sccm and then pulls all the members in the deployment and checks the deployment status for the members. The script is able to pull the members, however, it cannot pull the Deployment Status for any of the members that it needs to check through. If possible, I would like to avoid using SQL. I just need to pull a huge report once a day that I can use to compare to another. However, I cannot figure out how to pull the Deployment Status.

    # 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"

Upvotes: 0

Views: 191

Answers (1)

Syberdoor
Syberdoor

Reputation: 2619

Get-CMDeploymentStatus does not have a Property ResourceName that represents a device. In fact it has no info about devices at all. What you can do is pipe its returned object into Get-CMDeploymentStatusDetails to get that information. This will already be an array of device statusinfos so the whole matching against the devices you got in a loop is not necessary. So it would look a bit like this:

$status = Get-CMDeploymentStatus -DeploymentID $deploymentID | Get-CMDeploymentStatusDetails
foreach($s in $status) {
    Write-Host "Status for device $($s.DeviceName): $($s.StatusType) ($($s.StatusDescription))"
}

You don't even have to really check for unknown. It would just be StatusType 4 (not a missing status). Also note the StatusDescription can contain more details than the StatusType ("executed successfully", and "no need to rerun" both would be 1 e.g.)

Upvotes: 0

Related Questions