c# - 通过 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();
解决方案
推荐阅读
- python - 如何在列表中减去奇数索引值并添加偶数索引值?(a-b+c-d+e)
- javascript - 在 Laravel 应用程序上使用 npm 安装的 JS/CSS 库
- android - 如何摆脱离子页面之间的白色背景
- powershell - 如何从命令行运行 powershell 命令以使用当前目录作为参数?
- python - 即使使用 Selenium 找到元素也没有被点击
- python - pyserial 8位干净?
- python - 我想将我的目标文件保存为 .CSV 而不是 xlsx
- typescript - SyntaxError: Unexpected token {, ES6 Imports
- c++ - 有人可以解释这个链式模板实例化吗?
- javascript - 使用 fetch 向谷歌云函数发送一个帖子请求,其中一个对象作为主体不起作用