首页 > 解决方案 > 通过 web api 运行 powershell 脚本,希望将该 api 的响应作为由 ps1 脚本生成的 excel 文件

问题描述

我创建了一个 web api,通过它我执行 powerShell 脚本,该脚本将带有所需数据(审计日志)的 excel 文件创建到本地系统中......但我想要的是获取 excel 文件作为该 api 的响应。我怎样才能做到这一点?

我的 Powershell 脚本代码:-

# 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

我的网络 API 代码:-

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();

标签: c#powershellasp.net-web-api

解决方案


推荐阅读