Skip to content

Instantly share code, notes, and snippets.

@markhallen
Created March 4, 2020 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markhallen/03aa10cb387f432af48c225698be2a4e to your computer and use it in GitHub Desktop.
Save markhallen/03aa10cb387f432af48c225698be2a4e to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
This script was created for use in ConfigMger Run Scripts
.DESCRIPTION
The client log files will be copied to a network share
.PARAMETER Logshare
Specifies the target share upon which to save the log files
.EXAMPLE
Get-CMClientLogFiles.ps1 -Logshare '\\server\share'
#>
Param(
[string]$Logshare = $null
) #end param
if(!($Logshare))
{
<#
- Check which site server the Client should connect to
#>
$CMSites=@{
'cmsitesserver.fqdn.com'='\\server\share'
}
# check the current domain
$ThisDomain = (Get-WmiObject win32_computersystem).Domain
# find the correct site for this domain
$DefaultServer = $CMSites.GetEnumerator() | Where-Object { $_.Name -like "*$ThisDomain" }
if($DefaultServer -eq $null) {Write-Host "Unknown domain"; Exit 3}
if($DefaultServer -ne $null){
$Logshare = $DefaultServer.Value
}
if($DefaultServer -eq $null) {Write-Host "Unknown log sahre location"; Exit 3}
}
if(!(Test-Path $Logshare)) {Write-Host "Can't connect to $Logshare"; Exit 3}
#Get path for SCCM client Log files
$Logpath = Get-ItemProperty -path HKLM:\Software\Microsoft\CCM\Logging\@Global
$Log = $logpath.LogDirectory
$Temp = "$env:temp\SCCM"
$InstLogs = "C:\Logs"
$TmpCMLogs = "$Temp\CMLogs"
$TmpInstLogs = "$Temp\InstLogs"
$ZipFile = "$Temp.zip"
#Create folders
New-Item -Path $TmpCMLogs -ItemType Directory -Force | Out-Null
try {
Copy-item -path $log\* -destination $TmpCMLogs -Force | Out-Null
}
catch {
Write-Host "Failed to copy files to $TmpCMLogs"
Exit 3
}
New-Item -Path $TmpInstLogs -ItemType Directory -Force | Out-Null
try {
Copy-item -path $InstLogs\* -destination $TmpInstLogs -Force | Out-Null
}
catch {
Write-Host "Failed to copy files to $TmpInstLogs"
Exit 3
}
#Create a .zip archive with sccm logs
try {
Compress-Archive -Path $Temp\* -CompressionLevel Optimal -DestinationPath $ZipFile -Force | Out-Null
}
catch {
Write-Host "Failed to compress files to $ZipFile"
Exit 3
}
#Copy zipped logfile to servershare
$Computerlogshare=$Logshare + "\" + $env:Computername
New-Item -Path $Computerlogshare -ItemType Directory -Force | Out-Null
try {
Copy-Item $ZipFile -Destination $Computerlogshare -force | Out-Null
}
catch {
Write-Host "Failed to copy $ZipFile to $Computerlogshare"
Exit 3
}
#Cleanup temporary files/folders
Remove-Item $Temp -Recurse | Out-Null
Remove-item $ZipFile | Out-Null
Write-Host "The client logs have been copied to $Computerlogshare"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment