0

I have created a web api, through which I execute powerShell script Which create excel file with required data(audit log) into local System... But what I want is to get excel file as a response of that api. How can I achieve this?

My Powershell Script Code:-

# Create the session to Exchange Online
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -Uri https://outlook.office365.com/powershell-liveid/  -Credential $UserCredential -Authentication Basic -AllowRedirection

# Import the Exchange Online commands
Import-PSSession $Session
$csvFile = "C:\Reports\auditlog2.csv"
# Setup our start and end dates to pull back events
#$start = Get-Date
$end = Get-Date
$start = $end.AddDays(-1)
$i = 1;
$startTime = Get-Date
do
{
$AuditData = Search-UnifiedAuditLog -StartDate $start -EndDate $end -RecordType SharePointFileOperation -ResultSize 5000 -SessionCommand ReturnLargeSet -SessionId "ExtractLogs" -SiteIds siteid
$ConvertedOutput = $AuditData | Select-Object -ExpandProperty AuditData | ConvertFrom-Json
$ConvertedOutput | SELECT CreationTime,UserId,Operation,Workload,ObjectID,SiteUrl,SourceFileName,ClientIP,UserAgent | Export-csv $csvFile -NoTypeInformation -Append -Force 
Write-Host $i++ . $AuditData.Count
$i = $i + 1;
}
Until($AuditData.Count -eq 0)

Remove-PSSession $Session

My Web Api Code:-

Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();
        Pipeline pipeline = runspace.CreatePipeline();
        // Add your path or relative one
        pipeline.Commands.AddScript(@"C:\Reports\spauditapi-Modified\spauditapi-master\SPAuditApi\Shell_Script\ReportGenerationforPermission.ps1");
        //pipeline.Commands.AddScript(@"../Shell_Script/ReportGenerationforPermission.ps1");
        pipeline.Commands.Add("Out-String");

        //Collection <psobject/> results = pipeline.Invoke();
        var results = pipeline.Invoke();
        StringBuilder stringBuilder = new StringBuilder();
        foreach (PSObject obj in results)
        {
            stringBuilder.AppendLine(obj.ToString());
        }
        return stringBuilder.ToString();
3
  • General Idea - Either script returns the Path to the file it created. Or you should know it before hand (i.e. Pass a path to script where it should create the file). Then after the File is created - read bytes & use as ResponseStream body. Commented Jan 2, 2019 at 2:52
  • Yeah i am specifying the path where the file is being created, in my Powershell script. Commented Jan 2, 2019 at 4:42
  • 1
    @AmardeepGupta You would need possibly this module to convert your output to a excel file instead of a CSV : powershellgallery.com/packages/ImportExcel/5.4.2 then your API should return a FileContentResult instead of a string (see first code sample in that answer stackoverflow.com/a/44112330/934946 ) Commented Jan 2, 2019 at 10:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.