livemanager
8/21/2018 - 7:05 AM

Get Csv File

Get Csv File

Param (
    [ValidateSet('HMB','HMD','HMM')] [string] $CountryCode = 'HMM',
    [string] $StartDate = '2018-08-01',
    [string] $EndDate = '2018-08-20',
    [string] $CSVFileName = 'HMMLog.csv',
    [string] $SaveFolder = 'C:\Temp\Logs'
)

$UserID = "administrator"
$Pwd = ConvertTo-SecureString "pass@word!01" -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential($UserID, $Pwd)
$Session = New-PSSession -ComputerName 192.168.100.50 -Credential $Cred -Authentication Default -Port 80

Invoke-Command -Session $Session -ScriptBlock {
    $CountryCode = $args[0]
    $StartDate = $args[1]
    $EndDate = $args[2]
    $CSVFileName = $args[3]

    $_StartDate = Get-Date($StartDate)
    $_EndDate = Get-Date($EndDate)
    $CurrentDate = $_StartDate

    $LogString = ""
    While($CurrentDate -lt $_EndDate.AddDays(1)) {
        $LogFileName = "u_ex$($CurrentDate.ToString('yyMMdd')).log"
        if($LogString -eq "") {
            $LogString = "D:\WA_Log\IIS\${CountryCode}\*${LogFileName}"
        }else{
            $LogString += ",D:\WA_Log\IIS\${CountryCode}\*${LogFileName}"
        }
        $CurrentDate = $CurrentDate.AddDays(1)
    }
    
    if(Test-Path -Path "D:\WA_Log\IIS\_Work\${CSVFileName}") {
        Remove-Item "D:\WA_Log\IIS\_Work\${CSVFileName}" -Force
    }
    $Query = "
        SELECT * INTO D:\WA_Log\IIS\_Work\${CSVFileName} FROM ${LogString}
    "
	#IIS Log Into CSV
    & "C:\Program Files (x86)\Log Parser 2.2\logparser" $Query -i:W3C -o:csv 

} -ArgumentList @($CountryCode, $StartDate, $EndDate, $CSVFileName)

Copy-Item -FromSession $Session -Path "D:\WA_Log\IIS\_Work\${CSVFileName}" -Destination $SaveFolder -Recurse