# Get the hostname and logged-in user $hostname = $env:COMPUTERNAME $loggedInUser = $env:USERNAME $folderName = "$loggedInUser`_$hostname" # Define the paths $basePath = "\\servername.domain\patchmypclogs\$folderName" $installLogsPath = "$basePath\PatchMyPCInstallLogs" $intuneLogsPath = "$basePath\PatchMyPCIntuneLogs" $sourceInstallLogs = "C:\ProgramData\PatchMyPCInstallLogs" $sourceIntuneLogs = "C:\ProgramData\PatchMyPCIntuneLogs" # Function to check connection function Test-ConnectionToServer { param ( [string]$serverPath ) try { $null = Get-Item -Path $serverPath return $true } catch { return $false } } # Check connection to \\servername.domain\patchmypclogs if (-not (Test-ConnectionToServer -serverPath "\\servername.domain\patchmypclogs")) { Write-Output "Cannot connect to \\servername.domain\patchmypclogs. Exiting script." exit } # Create the base folder and subfolders if they don't exist if (-not (Test-Path -Path $basePath)) { New-Item -Path $basePath -ItemType Directory } if (-not (Test-Path -Path $installLogsPath)) { New-Item -Path $installLogsPath -ItemType Directory } if (-not (Test-Path -Path $intuneLogsPath)) { New-Item -Path $intuneLogsPath -ItemType Directory } # Function to copy files if there's a new version and count them function Copy-NewFiles { param ( [string]$sourcePath, [string]$destinationPath ) $filesCopied = 0 Get-ChildItem -Path $sourcePath | ForEach-Object { $sourceFile = $_.FullName $destFile = Join-Path -Path $destinationPath -ChildPath $_.Name if (-not (Test-Path -Path $destFile) -or (Get-Item -Path $sourceFile).LastWriteTime -gt (Get-Item -Path $destFile).LastWriteTime) { Copy-Item -Path $sourceFile -Destination $destFile -Force $filesCopied++ } } return $filesCopied } # Copy the files if there's a new version and count them $installLogsCopied = Copy-NewFiles -sourcePath $sourceInstallLogs -destinationPath $installLogsPath $intuneLogsCopied = Copy-NewFiles -sourcePath $sourceIntuneLogs -destinationPath $intuneLogsPath # Display the number of files copied $totalFilesCopied = $installLogsCopied + $intuneLogsCopied Write-Output "Total files copied: $totalFilesCopied"