Altiris Software Dev Kit (ASDK)

 View Only

HowTo Automatically Save Report to CSV File using Powershell and ASDK 

Oct 02, 2015 05:11 PM

As there is no possibility of saving a Report as csv file on schedule in ITMS 7.x, here's a powershell script to do so.

  1. Create a new 'Run Script on Server' Task, choose 'Powershell' and paste the code below.
  2. Replace the variables.
    1. $reportGUID: Guid of the Report you want to run
    2. $reportFile: Path and file name of the output file. Either use a local path on the NS, where the script runs, or use a network directory. Make sure that the application identity of your Notification Server has the required rights to delete and write files in this directory.
    3. $separator: Depending on your location, use comma or semicolon as separator in the csv file.
    4. Also replace the connection credentials of ASDK.
  3. Set a schedule for the script.
    You can also add a 'Send email' Task where you send the report file link (attachment is not possible) to some administrators or whatever. Then put both tasks in a Job to schedule.
# Save Report as CSV
#######################################
# HauckS
# 2015-10-02
#
# Change History:
#######################################

# Declare Variables
$reportGUID = "a36f2aef-4758-4b2b-a7a1-bafe2d28dca5";
$reportFile = "\\%NSSERVER%\share\Export\report.csv";
$separator = ";" # Comma or Semicolon

try
{
    # Load ASDK Object
    $oReportManagement = New-Object -Com Altiris.ASDK.NS.ReportManagement;
    
    # Connect to Server
    $oReportManagement.TargetServer = "%NSSERVER%";
    $oReportManagement.UserName = "userxy";
    $oReportManagement.Password = "passwordxy";
    $oReportManagement.Authenticate();
    
    # Get Report
    $reportResult = $oReportManagement.RunReport($reportGUID);
    # Create File
    if(Test-Path $reportFile)
    {
        Remove-Item $reportFile;
    }
    New-Item -Type file -path $reportFile;
    # Create StreamWriter
    $writer = [System.IO.StreamWriter] $reportFile;
    
    # Process Recordset
    if($reportResult.RecordCount -gt 0)
    {
        $reportResult.MoveFirst();
        # Write Field Names
        $line = $reportResult.Fields.Item(0).Name;
        for($i=1; $i -lt $reportResult.Fields.Count; $i++)
        {
            $line = $line + $separator + $reportResult.Fields.Item($i).Name;
        }
        $writer.WriteLine($line);
        # Process every line in Report
        do
        {
        $line = $reportResult.Fields.Item(0).Value;
        for($i=1; $i -lt $reportResult.Fields.Count; $i++)
        {
            $line = $line + $separator + $reportResult.Fields.Item($i).Value;
        }
        $writer.WriteLine($line);
        $reportResult.MoveNext();
        }
        until($reportResult.EOF);
    }
    # Close StreamWriter
    $writer.close();
}
catch{
echo $error;
exit;
}

About the script:

  • I developed and tested it on ITMS 7.5 SP1 HF1.
  • To process a custom Report with 8 columns and 50.000 rows, the script needs about 3 minutes to run.
  • No extras, no features, just plain report-to-file. If you need some more, you can ask me.

Statistics
0 Favorited
1 Views
0 Files
0 Shares
0 Downloads

Tags and Keywords

Related Entries and Links

No Related Resource entered.