How to log output to file - powershell

powershell newb here. I am having some difficulty trying log my output to a file. I have tried two tactics, both of which do not work for me. The first is using the Start/Stop-Transcript cmdlet. This works great in testing on my local machine, but doesn't seem to work at all in a script that I deploy to workstations.
$path1 = Test-Path ($env:ProgramFiles + "\Sophos\Sophos Anti-Virus\SavService.exe")
$path2 = Test-Path (${env:ProgramFiles(x86)} + "\Sophos\Sophos Anti-Virus\SavService.exe")
$shareloc = '\\SERVER1\NETLOGON\SophosPackages\SophosInstall_wFW_Silent.exe'
$logpath = '\\SERVER1\NETLOGON\si_sophos_log.txt'
if (($path1 -eq $true) -or ($path2 -eq $true)) {} ELSE {
& $shareloc
Start-Transcript -Append -Path $logpath | Out-Null
Write-Output ""
Get-Date
Write-Output "Sophos has been installed on `"$env:COMPUTERNAME`""
Write-Output ""
Stop-Transcript
}
The way I would prefer to do it, is using: | Out-File -Append -FilePath $logpath
I think this would be the preferred method because it would catch any error that might occur in the log, as apposed to Start-Transcript. When I try to use this method however, I get an error at the pipeline "An empty pipeline element is not allowed."
$path1 = Test-Path ($env:ProgramFiles + "\Sophos\Sophos Anti-Virus\SavService.exe")
$path2 = Test-Path (${env:ProgramFiles(x86)} + "\Sophos\Sophos Anti-Virus\SavService.exe")
$shareloc = '\\SERVER1\NETLOGON\SophosPackages\SophosInstall_wFW_Silent.exe'
$logpath = '\\SERVER1\NETLOGON\si_sophos_log.txt'
if (($path1 -eq $true) -or ($path2 -eq $true)) {} ELSE {
& $shareloc
Write-Output ""
Get-Date
Write-Output "Sophos has been installed on `"$env:COMPUTERNAME`""
Write-Output ""
} | Out-File -Append -FilePath $logpath
Thank you in advance for any assistance!

If you write the following :
if ($true) {Write-Output "titi"} else {Write-Output "toto"} | Out-File -Append c:\temp\titi
You will get the same error, because the if condition is not evaluated when you pipe.
You can try to force ti evaluate it
$(if ($true) {Write-Output "titi"} else {Write-Output "toto"}) | Out-File -Append c:\temp\titi

When the if condition evaluates as true, the empty scriptblock gets piped to Out-File which causes your error. i.e. the following throws the error you specified:
if($true) { } else { Write-Output "Something" } | Out-File -Append -FilePath C:\temp\myfile.txt

Related

Powershell delete files older than 30 days and log

I'm using this code to delete files older than 30 days
Function Remove_FilesCreatedBeforeDate {
$Path = "\\servername\path"
$Date = (Get-Date).AddDays(-30)
$ValidPath = Test-Path $Path -IsValid
If ($ValidPath -eq $True) {
"Path is OK and Cleanup is now running"
Get-ChildItem -Path $path -Recurse | Where-Object { $_.LastWriteTime -lt $Date } | Remove-Item -Recurse -force -Verbose
}
Else {
"Path is not a ValidPath"
}
}
Remove_FilesCreatedBeforeDate
Now I want to log which files were deleted, and also whether there was an error or the path isn't valid. Can anyone help me here?
//EDIT
Im Now using this Code (Thanks to Efie for helping)
[Cmdletbinding()]
param(
[Parameter()]$LogPath = 'C:\Admin\scripts\Clean_Folder\Log\log.txt',
[Parameter(ValueFromPipeline)]$Message
)
process {
$timeStampedMessage = "[$(Get-Date -Format 's')] $Message"
$timeStampedMessage | Out-File -FilePath $LogPath -Append
}
}
Function Remove-FilesCreatedBeforeDate {
[Cmdletbinding()]
param(
[Parameter()]$Path = '\\servername\path\',
[Parameter()]$Date = $(Get-Date).AddDays(-30)
)
process {
if(-not (Test-Path $Path -IsValid)) {
"Path $Path was invalid" | Write-MyLog
return
}
"Path $Path is OK and Cleanup is now running" | Write-MyLog
try {
Get-ChildItem -Path $Path -Recurse |
Where-Object {
$_.LastWriteTime -lt $Date
} | Remove-Item -recurse -force -verbose | Write-MyLog
}
catch {
"Remove-Item failed with message $($_.Exception.Message)" | Write-MyLog
}
}
}
Write-MyLog
Remove-FilesCreatedBeforeDate
Two files getting deleted but i just see this in my Log
[2021-07-22T16:27:53] Path \\servername\path\ is OK and Cleanup is now running
I dont see which files getting deleted sadly
A simple implementation for your example would be something like this:
Function Remove-FilesCreatedBeforeDate {
[Cmdletbinding()]
param(
[Parameter(Mandatory)]$Path = '\some\default\path',
[Parameter()]$Date = $(Get-Date).AddDays(-30)
)
process {
if(-not (Test-Path $Path -IsValid)) {
"Path $Path was invalid" | Write-MyLog
return
}
"Path $Path is OK and Cleanup is now running" | Write-MyLog
try {
Get-ChildItem -Path $Path -Recurse |
Where-Object {
$_.LastWriteTime -lt $Date
} | Remove-Item -Recurse -Force -Verbose
}
catch {
"Remove-Item failed with message $($_.Exception.Message)" | Write-MyLog
}
}
}
function Write-MyLog {
[Cmdletbinding()]
param(
[Parameter()]$LogPath = 'default\log\path\log.txt',
[Parameter(ValueFromPipeline)]$Message
)
process {
$timeStampedMessage = "[$(Get-Date -Format 's')] $Message"
$timeStampedMessage | Out-File -FilePath $LogPath -Append
}
}
Some notes:
Advanced Functions
process { }, [Cmdletbinding()], and [Parameter()] are what turn your function into an 'advanced' function. You get to use loads of built in features normally reserved for compiled cmdlets this way.
For example, you could now suppress errors with $ErrorActionPreference = 'SilentlyContinue' like you're used to doing with native Powershell cmdlets.
You can pipe your messages to your logging function by adding ValueFromPipelin to your parameter.
Those really just brush the surface of the extra capabilities you get.
Here is some information. I would recommend getting in the habit of writing them like this if you plan to use them in the future.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced?view=powershell-7.1
Error Handling
I'd recommend looking into this documentation by Microsoft on error handling:
https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-exceptions?view=powershell-7.1
Naming Conventions
I would also recommend taking a look at this about PowerShell function naming conventions:
https://learn.microsoft.com/en-us/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands?view=powershell-7
By PowerShell standards it would make more sense to name your function Remove-FilesCreatedBeforeDate with the dash separating verb-action instead of the underscore.
Logging
If you want a little more control and a few more features for logging your functions, here is some information on a tried and true solution for PowerShell using PSFramework:
https://adamtheautomator.com/powershell-logging/
Good luck! Hope some of that helps.
In Unix its Simple
find /var/log/hive -type f -mtime +30 -delete
Could Start-transcript with Try and catch be your solution here?
Start-Transcript logs everything that you do and the errors.
I tried this and this does what you want
Start-Transcript -Path "$PSScriptRoot\RemoveAccountLog.txt" -Force -Append
Get-Date -Format "yyyy-mm-dd HH:MM"
Try
{ # Start Try
$Path = "\\servername\path"
$Date = (Get-Date).AddDays(-30)
$TestPath = Test-Path -Path $Path -PathType Container
If ( $TestPath -Eq $Null )
{ # Start If
Write-Host "The $TestPath String is empty, Path is not a valid"
} # End If
Else
{ # Start Else
Write-host "Path is OK and Cleanup is now running... 0%"
$GetFiles = Get-ChildItem -Path $Path -Recurse -Force |
Where-Object { $_.LastWriteTime -lt $Date } |
Remove-Item -Recurse -force -Verbose |
Write-host "Path is OK and Cleanup is now running... 100%" -ForegroundColor Green
} # End Else
} # End Try
Catch
{ # Start Catch
Write-Warning -Message "## ERROR## "
Write-Warning -Message "## Script could not start ## "
Write-Warning $Error[0]
} # End Catch
Screenshot:

Can I have two "tail"s in one logfile within a while loop that one condition does one thing and the other does something else?

I have a script that is supposed to look at a logfile that starts filling with several hundred lines. While the log is filling, I need to look for two different conditions at the same time. If I find "Completed Successfully" then it will break the while loop and continue with the rest of the script. If it finds "Error" in the script, it will restart the server. Unfortunately this isn't working for me. It just sits until the timeout of the stopwatch. I'm not quite sure what is wrong here. Is there a better way to do this?
$Comp="ServerA.domain"
$Logfile="\\$Comp\Logs\logfile1.txt"
$pattern1 = "Completed Successfully"
$Pattern2 = "Error"
$timeout = new-timespan -Minutes 5
$stopwatch = [diagnostics.stopwatch]::StartNew()
while ($stopwatch.elapsed -lt $timeout){
try{
$logContent2 = Get-Content -Path $Logfile -Tail 1000 | Select-String -Pattern $Pattern2 | Measure-Object | Select-Object -ExpandProperty Count
$logContent1 = Get-Content -Path $Logfile -Tail 1000 | select-string -pattern $pattern1 | Measure-Object | Select-Object -ExpandProperty Count
If ($logContent1 -gt 0) {
Write-Host -Object "Compiled Successfully on $Comp"
#break;
}
ElseIf ($logContent2 -gt 0) {
Write-Host -Object "Error Found on $Comp! Restart required. Rebooting..."
Restart-Computer -ComputerName $Comp -Wait -For PowerShell -Force
}
}
Catch{
$Error[0] > \\$Comp\Logs\ErrorLog.txt
}
}
If ($stopwatch.elapsed -ge $timeout){
Write-Error -Message "$pattern1 did not appear" -ErrorAction Stop
exit;
}
Yes! I accidentally placed a break in the wrong place. This is resolved. The above code works but you need to break the loop (I had a break in my code in the wrong place) and I don't include the break in the code in this example.

why doesnt tee-object work with NoNewline?

i have the following function in my script
function Write-Host($object)
{
if($global:LogFile -eq $null)
{
$global:LogFile = $logFile
}
$object | tee $global:LogFile -Append
}
referencing this post: https://stackoverflow.com/a/25847258/8397835
I am trying specifically this part here:
$job = Start-Job -ScriptBlock { Start-Sleep -Seconds 10 }
while (($job.State -eq "Running") -and ($job.State -ne "NotStarted"))
{
Write-Host ([char]9632) -NoNewLine
Start-Sleep -Seconds 1
}
apparently, with tee, nonewline appears to be ignored...and without tee, i am getting the characters to display on one line as i am seeking
with tee:
without tee
I think i know whats happening. since write-host is being converted to tee, any switches are ignored, be it color or in this case, nonewline. How can i make nonewline work with tee?
After our chat I understand what you're trying to do. You want to write yourself a custom progress bar that both writes to a log file as well as to the console without line breaks in either. For that you can write a function that will accomplish it, but I do recommend picking a new name that doesn't conflict with an existing cmdlet. I'll use Write-MyProgress.
Function Write-MyProgress{
[cmdletbinding()]
Param(
[parameter(valuefrompipeline=$true)]$message,
[switch]$NoNewLine
)
if($global:LogFile -eq $null)
{
$global:LogFile = $logFile
}
Add-Content -Value $message -Path $LogFile -NoNewline:$NoNewLine
Write-Host $Message -NoNewLine:$NoNewLine
}
You could then call it explicitly:
Write-MyProgress ([char]9632) -NoNewLine
or pipe things to it:
[char]9632 | Write-MyProgress -NoNewLine
Or, if you don't want to use a function, you could just do it all with native cmdlets like in this example:
1..10 | ForEach-Object -Process {
[char]9632 | Add-Content $LogFile -NoNewLine -PassThru | Write-Host -NoNewLine
start-sleep -Sec 1
} -End {Add-Content -Value '' -Path $LogFile}
(Note that I add '' to the log file at the end, so the log file gets a new line after the progress bar is done)

What is the easiest way to create log file in powershell?

I have a code which modify user attribute in active directory.
How can I modify the code, to see in a log file what changed? I would like to see too if there was an error or not. If was an error what was that.
The code:
$csvdata = Import-Csv $csv -Delimiter $delimiter -Encoding "UTF8"
ForEach-Object -InputObject $csvdata {
$params = #{Identity = $_.ObjectGUID}
$sn = $_.Surname.Trim()
$gn = $_.GivenName.Trim()
$Manager = $_.Manager
if (-not [string]::IsNullOrWhiteSpace($_.Surname)) {
$params.Surname = $_.Surname
}
if (-not [string]::IsNullOrWhiteSpace($_.Givenname)) {
$params.Givenname = $_.Givenname
}
Set-ADUser #params
}
You can use transcript commands.
Start-Transcript
& ".\Script.ps1"
Stop-Transcript
I wrote the following function for logging in a little script I made a while back, feel free to rip it up and abuse it!
function Write-Log {
param(
[Parameter(ValueFromPipeline =$true,
ValueFromPipelineByPropertyName=$true,
Position=0
)]
[string[]]
$Message,
[string]
$Level = "Informational"
)
if (!$loglocation)
{
write-error -Message 'No $LogLocation set.'
break
}
$logarchive = split-path $loglocation
$logarchive = $logarchive + "\Archive.log"
$truedate = get-date -f yyyy-MM-dd_HH:MM:ss
if (test-path $loglocation)
{
if ((get-item $loglocation).length -ge 5242880)
{
"$truedate : Log exceeds 5MB, archiving and creating new log." | out-file -append -filepath $loglocation
if (test-path $logArchive)
{
"$truedate : Log archive already exists, removing this log first before archiving current log." | out-file -append -filepath $loglocation
get-item $logArchive | remove-item
}
get-childitem $loglocation | rename-item -NewName {"Archive.log"}
"$truedate : New log created, old log archived." | out-file -append -filepath $loglocation
}
}
"$truedate : $level - $message" | out-file -append -filepath $loglocation
}
You just need to set a variable in your script called $loglocation to the file path you want the log to save too, i.e. "C:\MyLogs" etc. Then when you want to log something out to it, just use write-log -Message "Whatever you are logging".

Executing $true statement when $false

I am having a bit of trouble with a PowerShell script. The intent of this is to spider the network and look for files/folders that exist on any PC.
Here is the original source:
#FiToFin Script#
$Fltr = "how_recover*.*"
$Online = "C:\Users\<username>\Scripts\Logs\Online.log"
$CSV = "C:\Users\<username>\Scripts\Devices.csv"
#$Tstpath = test-path "\\$computer\c$"
$Offline = "C:\Users\<username>\Scripts\Logs\Offline.log"
##################################################
$devices = Get-Content "$CSV"
foreach ($computer in $devices) {
Test-Path "\\$computer\c$" > $Tstpath
if ($Tstpath -eq $True) {
ls -Path "\\$computer\c$\users" -Filter $Fltr -Recurse |
Out-File -Append $Online
} else {
Write-Host "$computer is NOT Online" | Out-File -Append $Offline
}
}
##################################################
Write-Host "_____________________"
Write-Host "Online file = $Online"
Write-Host "Offile file = $Offline"
Write-Host "_____________________"
I have changed the if statement to if($Tstpath -eq "True"), if ($lastexitcode -eq $true) and if($Tstpath -eq $false) and they all just parse the first {Do command} no matter what. They never drop into else. Even tried the Tstpath = Test-Path \\$computer\c$ as a variable and just running that.
When it parses the first {Do Command} the return is
ls : Cannot find path '\\<computerName>\c$\u' because it does not exist.
At C:\Users\<username>\Scripts\FiToFin.ps1:19 char:3
+ ls -Path "\\$computer\c$\users" -Filter $Fltr -Recurse | Out-File -Append $On ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\\<computername>\c$\u:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
What does work:
If my test machines are on I can ls -Path "\\$computer\c$\users" -Filter $Fltr -Recurse | Out-File -Append $Online just fine.
I get True or False from Test-Path \\$computer\c$ and can even > $var and Write-Host the results just fine.
I have no idea why this is and would love to know.
This also works:
###################################################################
$computer = "TestPC"
$Tstpath = Test-Path \\$computer\c$
####################################################################
$Tstpath > $null
if($Tstpath -eq $True) {
Write-Host "$computer is Online"
} else {
Write-Host "$computer is NOT Online"
}
But when you add the command ls or Get-ChildItem it freaks out.
So, question is: Why is it never executing the else portion?
I see two issues that would be causing your issues. How you initialize and update the variable $Tstpath
# Presumably Initialize
$Tstpath = test-path "\\$computer\c$"
# Updating in loop
test-path "\\$computer\c$" > $Tstpath
I will assume that you are testing in PowerShell ISE and that $Tstpath had a $true value at some point.
The issue is that you were never updating the variable. Looking at TechNet for about_redirection you will see that:
Operator Description Example
-------- ---------------------- ------------------------------
'>' Sends output to the Get-Process > Process.txt
specified file.
Your command was trying to output that to "file". You should have had an error about not being able to find the file or a file somewhere on your system with a single boolean in it (since it was not an append redirector).
What you should have done to stay with your logic is save the result via assignment.
$Tstpath = Test-path "\\$computer\c$"
Then you can test that.
However it is redundant since you do not ever need that value again. Would just be easier to put it straight in the if statement.
if(test-path "\\$computer\c$"){"Do something"}else{"Fail Trumpet"}
I would also suggest using Export-CSV -Append since you are dealing with objects. Would make for good structured output.
Get-ChildItem -path "\\$computer\c$\users\" -Filter $Fltr -Recurse | Export-CSV -Append $Online -NoTypeInformation