2

In Sccm, normally I generate a report for a pc for "installed software on a specific computer" using gui in the following manner. enter image description here

then enter pc name and by clicking view report.

enter image description here

My question is,

I am trying to create it using powershell by login to scccm server using https://blogs.technet.microsoft.com/enterprisemobility/2013/03/27/powershell-connecting-to-configuration-manager/

is it possible to generate above report using powershell ? could not find any suitable document or tutorial to do that. Any help would be greatly appreciated !

Ps: my intention is to generate /grab software installed on remote pcs using a script. I use sccm reports because sccm database has already stored it and even if the remote pc is not online, we can just generate the report without accessing the remote pc.

user879
  • 269
  • 2
  • 6
  • 21

2 Answers2

0

I know this is a few years old now, but there is in my opinion a better solution. You can use PowerShell to run any SSRS report, including the SCCM reports.

There is a good general discussion on this topic here: https://sqlbelle.wordpress.com/2015/08/17/automate-ssrs-report-generation-using-powershell/

Below is a working example. Note that the Microsoft report viewer is a prerequisite, and the below code presumes that you have the needed packages in c:\temp\SQLReportViewer, and of course insert your SSRS servername and the correct path to the report you want to run:

                $username = 'someuser'
                Write-Progress -Activity "Progress searching for $username" -status "Getting 'logged on user' report from SCCM"
 
                Try
                {
                    $version, $key = (Get-ChildItem C:\Windows\assembly\GAC_MSIL\Microsoft.ReportViewer.WinForms -ErrorAction Stop | Sort-Object name)[-1].Name -split '__'
                    Add-Type -AssemblyName "Microsoft.ReportViewer.WinForms, Version=$version, Culture=neutral, PublicKeyToken=$key" -ErrorAction Stop
                }
                Catch
                {
                    Write-Warning "`nFailed to load the required Microsoft ReportViewer Assembly"
                    Write-Warning 'Installing Microsoft Report Viewer 2015...'
                    #$install = Read-Host 'Install Microsoft Report Viewer 2015 (y/n)'
                    #if ($install -like "n*") { return }

                    msiexec.exe /passive /norestart /i c:\temp\SQLReportViewer\2015\SQLSysClrTypes.msi | Out-Null
                    msiexec.exe /passive /norestart /i c:\temp\SQLReportViewer\2015\ReportViewer.msi | Out-Null

                    Try
                    {
                        $version, $key = (Get-ChildItem C:\Windows\assembly\GAC_MSIL\Microsoft.ReportViewer.WinForms -ErrorAction Stop | Sort-Object name)[-1].Name -split '__'
                        Add-Type -AssemblyName "Microsoft.ReportViewer.WinForms, Version=$version, Culture=neutral, PublicKeyToken=$key" -ErrorAction Stop
                    }
                    Catch { Write-Error 'Still failed to load the required Microsoft ReportViewer Assembly'; Return }
                }

                $reportViewer = New-Object Microsoft.Reporting.WinForms.ReportViewer
                $reportViewer.ProcessingMode = "Remote"
                $reportViewer.ServerReport.ReportServerUrl = "http://<ssrs server>/reportserver"
                $reportViewer.ServerReport.ReportPath = "/Useful Reports/Master User List/Logon User report"

                $inputParams = @{
                    username = "%\$username"
                }

                #create an array based on how many incoming parameters
                $params = New-Object 'Microsoft.Reporting.WinForms.ReportParameter[]' $inputParams.Count
                $i = 0
                ForEach ($p In $inputParams.GetEnumerator())
                {
                    $params[$i] = New-Object Microsoft.Reporting.WinForms.ReportParameter($p.Name, $p.Value, $false)
                    $i++
                }

                $reportViewer.ServerReport.SetParameters($params)

                #required variables for rendering
                $mimeType = $null
                $encoding = $null
                $extension = $null
                $streamids = $null
                $warnings = $null

                #export to Excel
                Try
                {
                    $bytes = $reportViewer.ServerReport.Render("CSV", $null,
                        [ref]$mimeType,
                        [ref]$encoding,
                        [ref]$extension,
                        [ref]$streamids,
                        [ref]$warnings)
                }
                Catch
                {
                    'Failed to run the required SCCM report.'
                    Return
                }

                $csv = [System.Text.Encoding]::UTF8.GetString($bytes) -split '\n'


                #region start after first blank line which is where the column headers are defined
                $i = 0
                ForEach ($line In $csv)
                {
                    If ($line.trim() -eq "") { Break }
                    Else { $i++ }
                }

                $csv = $csv[($i + 1) .. $csv.count]
                #endregion start after first blank line

                $sessionsFromSCCM = ConvertFrom-Csv $csv

                $sessionsFromSCCM
0

Are you trying to distribute the reports via email by running a powershell script? SCCM has SQL Server Reporting Services. SSRS allows you to subscribe to reports and send it via email. If you looking at some custom data in the report, then you will need to create a custom report in SCCM.

If you still want to use powershell, then you would need to query the SCCM database. For that you would need to use SQLPS module and run the query which is embedded in the built in report or run your own custom query.

Install SQL PS Module

Hope this helps!

Rajiv Iyer
  • 157
  • 8