Can you help me review & improve the below code?
I don’t have much scripting experience, and am very much still learning.
My end goal is to transfer User Profile data files from one PC to a virtual desktop, excluding some directories/files, logging errors, to account for stale user data in the profile in the from the virtual desktop being created for them from the physical PC.
Ideally it would identify & modify the Virtual Desktop user profile only if files after a certain date on the Physical PC are not more recently modified on the Virtual Desktop. (but at this point I haven’t worked on that at all, but was looking at compare-object.)
the below results in permssion errors
Function LogWrite { Param ([string]$logstring) Add-content $Logfile -value $logstring } #import csv of users and computers to be moved $SourceFile = import-csv C:\Scripts\VDI\source.csv #Operate line by line through CSV SourceFile ForEach ($Line in $SourceFile) { #Assign Variables for each User $UserID = $Line.UserID $PhysicalPCName = $Line.OldPC $VDPCName = $Line.NewPC $IncludePath = ("\\$PhysicalPCName" + "\c$\Users\" + $UserID + "\*") $ExcludePath = ("\\$PhysicalPCName" + "\c$\Users\" + $UserID + "\AppData\ntuser.*", "\\$PhysicalPCName" + "\c$\Users\" + $UserID + "\AppData\Local\*", "\\$PhysicalPCName" + "\c$\Users\" + $UserID + "\AppData\LocalLow\*") #Create Logfile $Logfile= "\\hqemcnas2\vdi_staging$\Log\$userid.txt" LogWrite -Message "Starting ProfileCopy for $UserID" LogWrite -Message "Source is $PhysicalPCName, Destination is $VDPCName" LogWrite -Message "Logfile for $UserID is $Logfile" #Define User Profile Path locations per User $OldProfilePath = "\\$PhysicalPCName" + "\c$\Users\" + $UserID + "\*" $NewProfilePath = "\\$VDPCName" + "\c$\Users\" + $UserID + "\*" LogWrite -Message "Old Profile Path is $OldProfilePath" LogWrite -Message "VW7 Profile Destination is $NewProfilePath" LogWrite -Message "Old Profile Path is $IncludePath" LogWrite -Message "Excluded Paths are $ExcludePath" #Collect Folder Hierarchy from OldPC $oldProfileData = Get-ChildItem -path $OldProfilePath -Recurse -Force -exclude $ExcludePath $NewProfileData = Get-ChildItem -path $NewProfilePath -Recurse -Force } #Copy Folder Structure ForEach ($Object in $oldProfileData) { #reset copy count $i=0 Copy-Item $oldProfileData -Destination $NewProfilePath -Recurse #Test copy existence and Log if (test-Path -Path "$NewProfilePath\$($Object.name)" -eq true) {LogWrite -Message "The File $NewProfilePath\$($Object.name) was copied to $NewProfilePath"} else {LogWrite -Message "The File $NewProfilePath\$($Object.name) was not copied to $NewProfilePath"} $i++ LogWrite -Message "Files Copied is $i" if ($i -eq 0) { LogWrite -Message "All Files failed to copy" } else { LogWrite -Message "$i files were copied for $userid" } }