Include details into log file

samiam41
samiam41 used Ask the Experts™
on
Hey Experts!  I have a powerful script that I would like to add a little more details to the log file but am not sure how to put the lines into the existing code.  Any help would be appreciated.

Get-ADComputer -Filter 'Name -like "TRN3*"' -SearchBase "OU=COMPUTERS,OU=TRAINING,DC=LOCAL" -Property * | Select-Object -ExpandProperty name | out-file c:\tools\logs\testlog.csv

$computerList = Get-Content c:\tools\logs\testlog.csv
$logFile = 'C:\Tools\logs\profiles2.csv'

$computerList | ForEach-Object {
	$computerName = $_
	Write-Host "Processing $($computerName) ..."
	If (Test-Connection -ComputerName $computerName -Count 2 -Quiet) {
		Try {
			$diskC = Get-WmiObject  Win32_LogicalDisk -Filter "DeviceID='C:'" -ComputerName $computerName -ErrorAction SilentlyContinue
			$diskSize = [math]::Round($diskC.Size / 1GB, 2)
			$freeSpaceBefore = [math]::Round($diskC.FreeSpace / 1GB, 2)
			$profiles = Get-WmiObject -Class Win32_UserProfile -Filter "(Special='False') And (Loaded='False') And (Not (SID Like '%500'))" -ComputerName $computerName -ErrorAction Stop
			If ($profiles) {
				$profiles | ForEach-Object {
					$out = $_ | Select-Object -Property @{n='ComputerName'; e={$computerName}}, LocalPath, Result
					Try {
#						[void]$_.Delete()
						$out.Result = "Profile deleted"
					} Catch {
						$out.Result = "ERROR: $($_.Exception.Message)"
					}
					$out
				}
			} Else {
				[PSCustomObject]@{ComputerName=$computerName; Result='Found no profiles to delete'}
			}
			$diskC = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" -ComputerName $computerName -ErrorAction SilentlyContinue
			$freeSpaceAfter = [math]::Round($diskC.FreeSpace / 1GB, 2)
			[PSCustomObject]@{ComputerName=$computerName; Result="SUMMARY: disk size $($diskSize)GB, free space before: $($freeSpaceBefore)GB, free space after: $($freeSpaceAfter)GB"}
		} Catch {
			[PSCustomObject]@{ComputerName=$computerName; Result="ERROR: $($_.Exception.Message)"}
		}
	} Else {
		[PSCustomObject]@{ComputerName=$computerName; Result='No ping response'}
	}
} | Select-Object -Property ComputerName, LocalPath, Result | Export-Csv -NoTypeInformation -Path $logFile

Open in new window


I was trying to add these lines but it doesn't seem I'm using them correctly.

$Timestamp = (Get-Date).ToString('MMddyyyy')
$LogFileTimeStamped = $LogFile -f $TimeStamp

Add-Content -value "PROFILE CLEANUP OF TRAINING ACCOUNTS" -path $LogFileTimeStamped
Add-Content -value "" -path $LogFileTimeStamped
Add-Content -value "Started processing at $([DateTime]::Now). " -path $LogFileTimeStamped

Open in new window


Any help would be appreciated with this as I need to add the details in the log file.  Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Took some liberties with the version below:
* Changed the time stamp to format yyyyMMdd, because then the log files will be sorted by date automatically when sorted by name, and prevents ambiguities.
* Kept the log file format in strict csv, so the text you wanted is now in the Results column.
The new format will have two new columns, Date and Type (the type of the row)
Get-ADComputer -Filter 'Name -like "TRN3*"' -SearchBase "OU=COMPUTERS,OU=TRAINING,DC=LOCAL" -Property * | Select-Object -ExpandProperty name | out-file c:\tools\logs\testlog.csv
$computerList = Get-Content c:\tools\logs\testlog.csv
$logFile = "C:\Temp\profiles2_$([DateTime]::Now.ToString('yyyyMMdd')).csv"

$dateTime = {[DateTime]::Now.ToString('yyyy-MM-dd HH:mm:ss')}
$computerList | ForEach-Object {
	$computerName = $_
	Write-Host "Processing $($computerName) ..."
	[PSCustomObject]@{
		ComputerName = $computerName
		Date = & $dateTime
		Type = 'Cleanup Start'
		Result = "PROFILE CLEANUP OF TRAINING ACCOUNTS START: $([DateTime]::Now)"
	}
	If (Test-Connection -ComputerName $computerName -Count 2 -Quiet) {
		Try {
			$diskC = Get-WmiObject  Win32_LogicalDisk -Filter "DeviceID='C:'" -ComputerName $computerName -ErrorAction SilentlyContinue
			$diskSize = [math]::Round($diskC.Size / 1GB, 2)
			$freeSpaceBefore = [math]::Round($diskC.FreeSpace / 1GB, 2)
			$profiles = Get-WmiObject -Class Win32_UserProfile -Filter "(Special='False') And (Loaded='False') And (Not (SID Like '%500'))" -ComputerName $computerName -ErrorAction Stop
			If ($profiles) {
				$profiles | ForEach-Object {
					$out = $_ | Select-Object -Property @{n='ComputerName'; e={$computerName}}, @{n='Date'; e={& $dateTime}}, @{n='Type'; e={'Profile'}}, SID, LocalPath, RoamingConfigured, RoamingPath, Result
					Try {
#						[void]$_.Delete()
						$out.Result = "Profile deleted"
					} Catch {
						$out.Result = "ERROR: $($_.Exception.Message)"
					}
					$out
				}
			} Else {
				[PSCustomObject]@{
					ComputerName = $computerName
					Date = & $dateTime
					Type = 'Profile'
					Result = 'Found no profiles to delete'
				}
			}
			$diskC = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" -ComputerName $computerName -ErrorAction SilentlyContinue
			$freeSpaceAfter = [math]::Round($diskC.FreeSpace / 1GB, 2)
			[PSCustomObject]@{
				ComputerName = $computerName
				Date = & $dateTime
				Type = 'Disk Summary'
				Result = "SUMMARY: disk size $($diskSize)GB, free space before: $($freeSpaceBefore)GB, free space after: $($freeSpaceAfter)GB"
			}
		} Catch {
			[PSCustomObject]@{
				ComputerName = $computerName
				Date = & $dateTime
				Type = 'WMI Error'
				Result = "ERROR: $($_.Exception.Message)"
			}
		} Finally {
			[PSCustomObject]@{
				ComputerName = $computerName
				Date = & $dateTime
				Type = 'Cleanup End'
				Result = "PROFILE CLEANUP OF TRAINING ACCOUNTS END: $([DateTime]::Now)"
			}
		}
	} Else {
		[PSCustomObject]@{
			ComputerName = $computerName
			Date = & $dateTime
			Type = 'Remoting Error'
			Result = 'No ping response'
		}
		[PSCustomObject]@{
			ComputerName = $computerName
			Date = & $dateTime
			Type = 'Cleanup End'
			Result = "PROFILE CLEANUP OF TRAINING ACCOUNTS END: $([DateTime]::Now)"
		}
	}
} | Select-Object -Property ComputerName, Date, Type, SID, LocalPath, RoamingConfigured, RoamingPath, Result | Export-Csv -NoTypeInformation -Path $logFile

Open in new window

Author

Commented:
Thanks oBdA.  I think I mis-communicated.  Just need to have the start/stop time of the script running at the beginning of the log file, not on each entry.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Well, that's what you said :), but while I was at it, I thought it might be useful to be able to see if an operation takes longer than expected; overhead is minimal. Told you I took some liberties ...
Anyway, start/stop time of the script can obviously be taken from the first and last line.
If you want to keep the additional columns "just in case", but don't want them at the moment, then you can just remove "Date" and "Type" in the parameters for Select-Object in the last line.
Or is it total information overflow and you really want it gone?
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Author

Commented:
Brilliant work!!  Yes you did take some liberties and it made a major positive impact.

Seriously, if you ever offer online classes for Powershell, you would fill the class.  Thank you for your help with this as it's way beyond what I could have ever expected.  You're demeanor was also stellar.

Author

Commented:
Someone needs to test oBdA if he is partial or full quantum AI.

Author

Commented:
I finally ran the script with the safety off and a few of the profiles are being deleted but I saw a few errors like this:

ERROR: Exception calling "Delete" with "0" argument(s): ""

Thoughts?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Can you provide more details?
Target OS, only on certain machines, only with certain profiles, can the errors be reproduced by running the script again?
Have rebooted the machines with the errors and re-run the script?

Author

Commented:
Script is just running.  Windows 7 Ent., script is still on first pc.  I'll let it finish running, was just curious if that error sounded familiar.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Can't say it is, but sometimes Windows still has a hard time to really release a profile when a user logs off.
Might help to reboot the training machines before cleaning the profiles.

Author

Commented:
Good call.  Going to restart a couple of them and rerun the script to see if it wipes them out.  The script finished running after 12 hours and looking through the results and comparing them to the user directories left on the computer, it appears that the c$\Users\xxxx\Appdata dir still reside.  Most users have a few empty folders in the Appdata dir and the file footprint has been drastically reduced.  Will let you know what I see following restarts.  Thanks oBdA

Author

Commented:
Going to close this one up as I have a different approach.  Thanks for the follow-up!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial