我正在编写一个PowerShell脚本来读取日志并生成报告,日志的前缀格式如下例所示:
Job 001, Allan send successfully
Job 002, Kath receive successfully
Job 003, Allan receive failed
示例:
$Logs = Get-content logs.txt
Foreach ($line In $Logs) {
if ($line[0] -eq "1") {
$JobType, $Username, $UserID = $line -split ','
Write-host $JobType
Write-host $Username
Write-host $UserID
} elseif ($line[0] -eq "2") {
$JobType, $JobID, $UserID, $Action, $Result = $line -split ','
Write-host $JobType
Write-host $JobID
Write-host $UserID
Write-host $Action
Write-host $Result
}
}
我的最终结果应该是:
#define _WIN32_WINNT 0x600
#include <stdio.h>
#include <wininet.h>
#define BUFLEN 1024
static LPCTSTR acceptTypes[] = {"*/*", NULL};
static const char *postData = "submit=submit&cool=hawk";
static char headers[] = TEXT("Content-Type: multipart/form-data; boundary=abcde\r\n"
"--abcde\r\n"
"Content-Disposition: form-data; name=\"cool\"\r\n"
"testval\r\n"
"--abcde\r\n");
int main()
{
HINTERNET hSession, hConnect, hFile;
if( ( hSession = InternetOpen(
"myapp",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0
) ) == NULL )
{
printf("Couldn't start session. Error %ld\n", GetLastError());
exit(1);
}
printf("Session started\n");
if( ( hConnect = InternetConnect(
hSession,
"localhost",
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
0
) ) == NULL )
{
printf("Unable to connect to server. Error %ld\n", GetLastError());
exit(1);
}
printf("Connected to server\n");
if( ( hFile = HttpOpenRequest(
hConnect,
"POST",
"/test/index.php",
NULL,
NULL,
acceptTypes,
INTERNET_FLAG_RELOAD,
0
) ) == NULL )
{
printf("Unable to open request. Error %ld\n", GetLastError());
exit(1);
}
printf("Opening request..Opened\n");
unsigned long dataLen = strlen(postData)+1;
bool res = HttpSendRequest(
hFile,
headers,
-1,
(char*)postData,
dataLen
);
if( !res )
{
printf("Unable to send request. Error %ld\n", GetLastError());
exit(1);
}
printf("Request sent\n");
// char *szBuffer = (char*)malloc(sizeof(char)*BUFLEN);
char szBuffer[BUFLEN];
DWORD dwRead = 0;
memset(&szBuffer, '\0', sizeof(szBuffer));
while( InternetReadFile(hFile, szBuffer, sizeof(szBuffer), &dwRead) && dwRead )
{
printf("%s", szBuffer);
}
return 0;
}
我能够将数组拆分为变量,但我不确定如何将UserID组合在不同的行中并生成结果?
示例脚本:
{{1}}
答案 0 :(得分:1)
使用format operator(-f
)构建类似的输出字符串,并使用switch
语句来区分这些情况:
$JobType, $data = $line -split ',', 2
switch ($JobType) {
1 {
$Username, $UserID = $data -split ','
'{0} {1} {2}' -f $JobType, $Username, $UserID
}
2 {
$JobID, $UserID, $Action, $Result = $data -split ','
'{0} {1} {2} {3} {4}' -f $JobType, $JobID, $UserID, $Action, $Result
}
default { "Unknown job type: $JobType" }
}
答案 1 :(得分:0)
你可以改用它......
$Logs = Get-content logs.txt
$Type1 = $Logs | ? {$_ -match "^1"}
$Type2 = $Logs | ? {$_ -match "^2"}
$T1Header = "JobType","UserName","UserID"
$T2Header = "JobType","JobID","UserID","Action","Result"
$CSVT1 = ConvertFrom-Csv $Type1 -Delimiter "," -Header $T1Header
$CSVT2 = ConvertFrom-Csv $Type2 -Delimiter "," -Header $T2Header
它将为您创建2个CSV文件,每个文件都带有必需的标题
答案 2 :(得分:0)
如果我的问题是正确的,那么您就会问,如何加入logs.txt
与相应UserID
相对应的不同行。您可以通过构建将用户ID映射到用户名的哈希表来完成此操作:
$Logs = Get-Content .\logs.txt
$userMap = @{}
$jobLog = @()
foreach ($line In $Logs)
{
if ($line[0] -eq '1')
{
$null, $Username, $UserID = $line -split ','
$userMap[$UserID] = $Username
}
elseif ($line[0] -eq '2')
{
$jobLog += $line
}
# Do whatever is appropriate for logs with different IDs
}
现在,您可以通过替换从剩余的行生成所需的结果。您可能还想使用额外的hastable来替换像'Recv'这样的关键字:
$keywordMap = @{
'Send' = 'send'
'Recv' = 'receive'
'Success' = 'successfully'
'Fail' = 'failed'
}
foreach ($line in $jobLog)
{
$null, $JobID, $UserID, $Action, $Result = $line -split ','
Write-Output ('Job {0}, {1} {2} {3}' -f $JobID,
$userMap[$UserID], $keywordMap[$Action], $keywordMap[$Result])
}