Upload-EndpointManagerLogsToAzureBlob.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID ec909599-b3ae-48fa-a331-72c40493d267
 
.AUTHOR PiotrG.
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 A sample script to collect Intune, ConfigMgr and GPO logs and upload them to Azure blob
 
#>

Param(
    [Parameter(Mandatory=$true,
    HelpMessage="Enter Shared Access signature link (permissions Read, Add, Create, Write, List) to container in Azure Blob",
    ParameterSetName="URL")]
    [String]   
$URL 
)

$FileName = "$($env:COMPUTERNAME)_$((Get-date).ToString("yyyy-MM-dd_HH-mm")).zip"
$URL = $URL.Split("?")[0]+"/$($FileName)?"+$URL.Split("?")[1];


$DestDir = "c:\ProgramData\MyLogs\LogsCollector"
$LogDir = "$($DestDir)\logs"
$OutDir = "$($DestDir)\OutDir"
$TempDir = "$($DestDir)\TempDir"

#cleaning directories
New-Item -Path $DestDir -ItemType Directory -force | out-null

if (Test-Path $LogDir) {
    Remove-item -Path $LogDir -Recurse -Force
}
New-Item -Path $LogDir -ItemType Directory -force | out-null

if (Test-Path $OutDir) {
    Remove-item -Path $OutDir -Recurse -Force
}
New-Item -Path $OutDir -ItemType Directory -force | out-null

if (Test-Path $TempDir) {
    Remove-item -Path $TempDir -Recurse -Force
}
New-Item -Path $TempDir -ItemType Directory -force | out-null

#CM logs
if (Test-Path c:\windows\ccm) {
    Copy-Item -Path c:\windows\ccm\logs\*.* "$($TempDir)" -Recurse -Force
    Compress-Archive -Path "$($TempDir)\*.*" -DestinationPath "$($LogDir)\ccm.zip" 
    Remove-item -force "$($TempDir)\*.*"
}
#Intune Management Extension logs
if (Test-Path "C:\ProgramData\Microsoft\IntuneManagementExtension\Logs") {
    Copy-Item -Path C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\*.* "$($TempDir)" -Recurse -Force
    Compress-Archive -Path "$($TempDir)\*.*" -DestinationPath "$($LogDir)\IME.zip"
    Remove-item -force "$($TempDir)\*.*"
}
#MDMDiagnostics logs
Start-Process -Wait MdmDiagnosticsTool.exe -NoNewWindow  -ArgumentList "-area Autopilot -cab $($LogDir)\mdmdiag.cab" | Out-Null

#gpresult
Start-Process -Wait gpresult -NoNewWindow  -ArgumentList "/scope computer /f /h $($LogDir)\gpresult_computer.html" | Out-Null
Start-Process -Wait gpresult -NoNewWindow  -ArgumentList "/scope user /f /h $($LogDir)\gpresult_user.html" | Out-Null


#Compressing

Compress-Archive -Path "$($LogDir)\*.*" -DestinationPath "$($OutDir)\$($FileName)"

#Uploading
$headers = @{
    'x-ms-blob-type' = 'BlockBlob'
}
Invoke-RestMethod -Uri $URL -Method Put -Headers $headers -InFile "$($OutDir)\$($FileName)"