如何根据数据来自哪个月份将powershell中的数据附加到其他文件?

问题描述 投票:0回答:2

我每天都在收集有关Office 365产品采用的数据。我不知道如何将当前的逻辑转换为基于文件大小的新文件写入基于报告日期的文件。

我最初的想法是使用if语句按月划分数据,并准备添加12个文件(取决于数据的月份),但这似乎效率很低。

$name = "O365SPSiteActivity.csv"
$auth=Get-AuthCode
$accesstoken=$auth[1]

### data pulling process has been omitted ###

if ($report -ne $null)
{
    ###New section for making the new files
    #Get current file
    $source = "D:\O365Data\"+ $name 
    $File = Get-Item $source

    If (((Get-Item $file).Length/1MB) -ge 700) 
    {
        $date = (get-date -Format dd-MM-yyyy)
        $RenamedFileName = "O365SPSiteActivity-$date.csv"
        Rename-Item $file.FullName -NewName $RenamedFileName
        $FileName = "D:\temp\" + $name
        Send-MailMessage –From [email protected] –To [email protected] –Subject “New File Has been Created" –Body “New File Name: $RenamedFileName "  -SmtpServer kbslp-com.mail.protection.outlook.com -Port 25


    }
    Else
    {
        $FileName = "D:\temp\" + $name
        Copy $File $FileName
    }


    #########################################################################
    $Data=@()
    $c=1
    foreach ($row in $report)
    {   Write-Progress -Activity $row.'User Principal Name' -PercentComplete (($c/$report.count)*100) -ID 4
        $string = "" | Select "???Report Refresh Date","User Principal Name","Is Deleted","Deleted Date","Last Activity Date","Viewed or Edited File Count",
                "Synced File Count","Shared Internally File Count","Shared Externally File Count","Visited Page Count","Report Period"
        $string.'???Report Refresh Date' = Get-Date($row.'Report Refresh Date') -Format "yyyy-MM-dd"
        $string.'User Principal Name' = $row.'User Principal Name'
        $string.'Is Deleted' = $row.'Is Deleted'
        $string.'Deleted Date' = $row.'Deleted Date'
        $string.'Last Activity Date' = $row.'Last Activity Date'
        $string.'Viewed or Edited File Count' = $row.'Viewed or Edited File Count'
        $string.'Synced File Count' = $row.'Synced File Count'
        $string.'Shared Internally File Count' = $row.'Shared Internally File Count'
        $string.'Shared Externally File Count' = $row.'Shared Externally File Count'
        $string.'Visited Page Count' = $row.'Visited Page Count'
        $string.'Report Period' = $row.'Report Period'
        $Data += $string
        $c++
    }
    $Data | Export-Csv -Append -Path $FileName -NoTypeInformation -Force
    #$FolderUrl = $teamSitePath + "/" + $ListName
    #$UploadFileInfo = New-Object System.IO.FileInfo($FileName)
    #Upload-SPOFile -WebUrl $teamSiteUrl -spCredentials $SPOCreds -FolderUrl $FolderUrl -FileInfo $UploadFileInfo
    $newFile = Get-Item $FileName
    Copy $newFile $File.FullName
}
$report = $null
$Data = $null

理想情况下,我想更改此脚本以将其写入文件,例如:

[O365SPSiteActivity-2019-Oct.csv,然后在十月,然后是O365SPSiteActivity-2019-Nov.csv,等等,这取决于数据来自何时。

powershell date append office365api
2个回答
0
投票

您要查找当前日期还是电子表格中的日期?

$date = (get-date -Format dd-MM-yyyy)

如果今天运行,将输出到O365SPSiteActivity-2019-Oct.csv。

[如果您要根据数据中的日期对CSV进行分块,我可以将export-CSV移到foreach循环内,并对其进行修改以使用类似于上面的名称,而不是将其附加到$ Data数组中。

$Data = "2019-May-31"
Get-Date -Format yyyy-MMM -Date ([datetime]::parseexact($Data, 'yyyy-MMM-dd', $null))
$FileName = "O365SPSiteActivity-$date.csv"
$Data | Export-Csv -Append -Path $FileName -NoTypeInformation -Force

这会将每一行写入相应的CSV文件-对于每个月的数据,您最终都会得到一个不同的CSV文件。


0
投票

为什么您要先写一个临时文件并在完成后复制(如果存在)文件?如果我理解这个问题,您希望每月创建一个新的csv报告。然后,为什么不简单地做这样的事情:

# create a filename for this month
$currentReport = 'O365SPSiteActivity-{0:yyyy-MMM}.csv' -f (Get-Date)

并将您的数据导出到-Append中?如果该文件尚不存在,则将创建该文件,否则将向其追加新数据。

© www.soinside.com 2019 - 2024. All rights reserved.