# Need to know where our script is so we can use full path to all our dependent files.
$InvocationPath = Split-Path -Path $MyInvocation.MyCommand.Path -Parent

function Get-ComputerName()
{
    return [system.environment]::MachineName
}

function Start-PowerLogs {
param (
    [parameter(Mandatory=$true)][string]$PowerLogsPath
)
    # change to $PowerLogsPath directory
    Set-Location -Path "$PowerLogsPath"
            
    # netsh trace start scenario=WLAN maxSize=400 tracefile=mytrace.etl report=yes perfmerge=yes
    $destDir = "$PowerLogsPath\tempFiles"
    $FilesToCab = $null
    $LatestPerftraceEtl = $null
    $LatestDripsWatchdogLiveDump = $null

    $MYHOST = Get-ComputerName
    "$MYHOST" | Set-Content "$PowerLogsPath\out.txt"

    if (-not (Test-Path "$destDir" -pathType container))
    {
        # Path for tempFiles
        "Creating tempFiles directory" | Add-Content "$PowerLogsPath\out.txt"
        New-Item $destDir -ItemType directory 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    }

    Write-Host "Copying Windows Update logs"
    "Copying Windows Update logs" | Add-Content "$PowerLogsPath\out.txt"
    $windowsUpdateLogs = "$env:systemroot\windowsupdate.log"
    if ((Test-Path "$windowsUpdateLogs" -pathType leaf))
    {
        Copy-Item $windowsUpdateLogs $destDir -Force 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    }

    Write-Host "Copying Windows Live Logs"
    "Copying Windows Live Logs" | Add-Content "$PowerLogsPath\out.txt"
    $windowsLiveLogs = "$env:localappdata\Packages\microsoft.windowscommunicationsapps_8wekyb3d8bbwe\LocalState\*.etl"
    Copy-Item $windowsLiveLogs $destDir -Force 2>&1 | Add-Content "$PowerLogsPath\out.txt"

    Write-Host "Copying Network Information"
    "Copying Network Information" | Add-Content "$PowerLogsPath\out.txt" 
    $netTraceCab = "$env:TEMP\NetTraces\NetTrace.cab"
    if ((Test-Path $netTraceCab -pathType leaf))
    {
        Copy-Item $netTraceCab $destDir -Force 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    }
    cmd.exe /c "route print > `"$destDir\route-nic-id.txt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "ipconfig /all > `"$destDir\ipconfig-out.txt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"

    Write-Host "Flushing always on network tracing"
    "Flushing always on network tracing" | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "LogMan Update LwtNetLog -fd -ets" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "netsh trace merge %SystemRoot%\System32\LogFiles\WMI\LwtNetLog.etl `"$destDir\LwtNetLog-merged.etl`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    $wifiEtl = "$env:SystemRoot\System32\LogFiles\WMI\Wifi.etl"
    Copy-Item $wifiEtl $destDir -Force 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    
    Write-Host "Capturing BCD information"
    "Capturing BCD information" | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "bcdedit > `"$destDir\bcdedit-out.txt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"

    Write-Host "Capturing Bitlocker status"
    "Capturing Bitlocker status" | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "manage-bde -status > `"$destDir\bde-status-out.txt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"

    # look for Drivers that are missing (code ">28<" or ">43<" or ">52<")
    Write-Host "Capturing PNP Information"
    "Capturing PNP Information" | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "ddodiag -o `"$destDir\ddodiag-driverInstall.xml`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    # get some dxdiag output for device tree
    cmd.exe /c "dxdiag /whql:off /t `"$destDir\dxdiag_out.txt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "driverquery.exe /FO TABLE /V > `"$destDir\$MYHOST.Drivers.log`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    
    Write-Host "Capturing System information"
    "Capturing System information" | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "systeminfo > `"$destDir\systeminfo.txt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "msinfo32.exe /nfo `"$destDir\$MYHOST.SystemInfo.nfo`" /categories +all" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "reg query `"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion`" /v BuildLab > `"$destDir\osver.txt`"" 2>&1 | `
        Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "reg query `"HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System`" /f SystemBios >> `"$destDir\osver.txt`"" 2>&1 | `
        Add-Content "$PowerLogsPath\out.txt"

    Write-Host "Capturing Power Configuration"
    "Capturing Power Configuration" | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "powercfg /qh > `"$destDir\powercfg.txt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "powercfg a > `"$destDir\powercfg-a.txt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "powercfg /requests > `"$destDir\powercfg-requests.txt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    # Registry power settings
    cmd.exe /c "reg save HKLM\System\CurrentControlSet\Control `"$destDir\powersettings_REG`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"

    Write-Host "Capturing PowerCfg Logs"
    "Capturing PowerCfg Logs" | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "wevtutil qe System /q:`"*[System[(EventID=506 or EventID=507 or EventID=109) and TimeCreated[timediff(@SystemTime) <= 259200000]]]`" /c:100 /f:renderedXml > `"$destDir\cs-exit-events.xml`"" 2>&1 | `
        Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "wevtutil qe System /q:`"*[System[EventID=520 and TimeCreated[timediff(@SystemTime) <= 259200000]]]`" /c:100 /f:TEXT > `"$destDir\%COMPUTERNAME%.BrightnessEngine.log`"" 2>&1 | `
        Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "wevtutil epl System `"$destDir\SystemEventLog.evt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "wevtutil epl Microsoft-Windows-TaskScheduler/Operational `"$destDir\tasksOperational.evt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "wevtutil epl Microsoft-Windows-GroupPolicy/Operational `"$destDir\gplog.evt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "wevtutil epl Microsoft-Windows-PushNotification-Platform/Operational `"$destDir\PushNotificationOp.evt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "powercfg /srumutil output `"$destDir\srumutil-$MYHOST.csv`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "powercfg /batteryreport output `"$destDir\battery-report-$MYHOST.html`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "powercfg /sleepstudy output `"$destDir\sleepstudy-report-$MYHOST.html`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "powercfg /sleepstudy /duration 20 /output `"$destDir\long20-sleepstudy-report-$MYHOST.html`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "powercfg /sleepstudy /duration 10 /output `"$destDir\long10-sleepstudy-report-$MYHOST.html`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "powercfg /sleepstudy /duration 5  /output `"$destDir\long5-sleepstudy-report-$MYHOST.html`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "powercfg /sleepstudy /duration 10 /output `"$destDir\sleepstudy-report-$MYHOST.xml`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "powercfg /systemsleepdiagnostics /output `"$destDir\system-sleep-diagnostics-$MYHOST`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    $sleepStudyEtls = "$env:windir\system32\SleepStudy\*.etl"
    Copy-Item $sleepStudyEtls $destDir -Force 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    
    "" | Add-Content "$PowerLogsPath\out.txt"
    "Starting Energy Analysis ..." | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "powercfg /energy output `"$destDir\energy-report-$MYHOST.html`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"

    "Capturing telemetry MachineId" | Add-Content "$PowerLogsPath\out.txt"
    cmd.exe /c "reg query `"HKLM\Software\Microsoft\SQMClient`" /v MachineId > `"$destDir\machine_id.txt`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"

    "" | Add-Content "$PowerLogsPath\out.txt"
    "Generating CAB file ..." | Add-Content "$PowerLogsPath\out.txt"

    ".OPTION EXPLICIT" | Set-Content "$destDir\input.ddf"
    "" | Add-Content "$destDir\input.ddf"
    ".Set CabinetName1=PowerLogs.CAB" | Add-Content "$destDir\input.ddf"
    ".Set DiskDirectory1=..\output" | Add-Content "$destDir\input.ddf"
    ".Set MaxDiskSize=0" | Add-Content "$destDir\input.ddf"
    ".Set Cabinet=on" | Add-Content "$destDir\input.ddf"
    ".Set Compress=on" | Add-Content "$destDir\input.ddf"
    cmd.exe /c "dir /b $destDir | findstr /v exclude | findstr /v setup  | findstr /v disk |findstr /v input.ddf >> `"$destDir\input.ddf`"" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    Set-Location -Path $destDir
    cmd.exe /c "makecab /f input.ddf" 2>&1 | Add-Content "$PowerLogsPath\out.txt"

    "" | Add-Content "$PowerLogsPath\out.txt"
    Set-Location -Path $PowerLogsPath
    cmd.exe /c "dir output" 2>&1 | Add-Content "$PowerLogsPath\out.txt"
    Remove-Item $destDir -Force -Recurse 2>&1 | Add-Content "$PowerLogsPath\out.txt"
}

Export-ModuleMember -Function Start-PowerLogs
