Answered by:
Output Remove-Item to logfile

Question
-
Hi all,
I want to put my Remove-Item outcome to a logfile, but nothing happens.
Here's my script so far:
#### PowerShell Script for installing ScheduledTaks for Hoogvliet OEG #### ### Setting up variables ### ## Set computername as variable ## $server = $env:computername #$server ## Set working dir as variable ## $dir = "\\$server\extapps\oeg" #$dir ## Set current date + time as variable ## $date = Get-Date -format "yyyy-MM-dd" $time = Get-Date -format "HH:mm:ss" #$Date ## Set logfiles directory as variable ## $log1 = "\\$server\extapps\oeg\logs" $log2 = "$log1\SYSOEGDAGLOG_$date.$server" #$log1 ## Set File Variables ## $ok = "$dir\system\OEG_ScheduledTasks.ok" #$ok ### Setting up functions ### function Make_Logfile1 { Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "*******************************" -Force Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "Bestand: OEG_ScheduledTasks.ps1" -Force Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "Datum+Tijd: $date $time" -Force Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "*******************************" -Force } function Remove_OK { $CheckOK = Test-Path $ok IF ($CheckOK -eq $true) {Remove-Item $ok | Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "File Removed" -Force} Else {Write-Out "Bestand bestaat niet" | Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "File does not exist" -Force} } Make_Logfile1 Remove_OK
So the logfile only contains the Add-Content part from the first function, but not from the second function.
Im kinda lost atm..
Thanks in advance!Friday, December 8, 2017 3:30 PM
Answers
-
There is too much noise in your code for you to see your mistake.
$server = $env:computername $dir = "\\$server\extapps\oeg" $logfile = "\\$server\extapps\oeg\logs\SYSOEGDAGLOG_$date.$server" @" ******************************* Bestand: OEG_ScheduledTasks.ps1 Datum+Tijd: $(Get-Date -format 'yyyy-MM-dd HH:mm:ss') ******************************* "@ | Out-File $logfile function Remove_OK { Param ( $FIleName ) if (Test-Path $FIleName) { Remove-Item $FIleName -Whatif 'File Removed' | Out-File $logfile -Append } else { 'File does not exist' | Out-File $logfile -Append } } Remove_OK "$dir\system\OEG_ScheduledTasks.ok"
When you over-comment and create and assign too many variables it becomes difficult to understand your own code. Starting over an simplifying can help you find your logic and design errors.
\_(ツ)_/
- Edited by jrv Monday, December 11, 2017 12:52 PM
- Proposed as answer by frank_songMicrosoft contingent staff Thursday, December 21, 2017 9:16 AM
- Marked as answer by jrv Thursday, December 21, 2017 12:12 PM
Monday, December 11, 2017 12:52 PM
All replies
-
Does Remove-Item generate any output?
Friday, December 8, 2017 3:44 PM -
Well, if you read carefully, you see that I do a Test-Path and after using Test-Path to get my output.Friday, December 8, 2017 3:50 PM
-
Even if the file exists, Remove-Item doesn't generate any output.
- Edited by JS2010 Friday, December 8, 2017 4:08 PM
Friday, December 8, 2017 4:08 PM -
The other problem is
write-output hi | add-content -path log
and
add-content -path log -value hi
do the same thing. I'm surprised you don't see an error when you try to do
both:
write-output hi | add-content -path log -value hi
add-content : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. At line:1 char:19 + write-output hi | add-content -path log -value hi + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (hi:PSObject) [Add-Content], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.AddContentCommand
EDIT: oh I see, remove-item somehow suppresses the error.
Also, write-out is not a valid alias, unless you made it yourself. Isn't
there an error message?
- Edited by JS2010 Sunday, December 10, 2017 3:46 PM
Friday, December 8, 2017 6:34 PM -
Bootstrap. Here is how PowerShell is designed to work:
function Remove_OK { Param( $FileName ) if(Test-Path $FileName){ Remove-Item $FileName 'File Removed' | Out-File "$log1\SYSOEGDAGLOG_$date.$server" -Append }else{ 'File does not exist' | Out-File "$log1\SYSOEGDAGLOG_$date.$server" -Append } }
Of course there are many more things that need to be done to make this reliable.
Do not place all code on one line. Learn how to format code so that you can read it and see your mistakes.
\_(ツ)_/
- Proposed as answer by PRASOON KARUNAN V Sunday, December 10, 2017 8:42 PM
Saturday, December 9, 2017 3:25 PM -
Hi,
I agree with Jrv, you need to call the value declared for the variable $ok when function begins.
Sunday, December 10, 2017 11:55 AM -
Thank you for your input. It has been almost 2 years since I last played with PowerShell, so I was kinda lost here.
I'll go out and try your stuff :)Retail Systems Specialist
Monday, December 11, 2017 7:19 AM -
***
I've rem'ed the Param-part and now it works!
***
function Remove_OK { # Param( # $ok # ) if(Test-Path $ok){ Remove-Item $ok 'File Removed' | Out-File "$log1\SYSOEGDAGLOG_$date.$server"-Append }else{ 'File does not exist' | Out-File "$log1\SYSOEGDAGLOG_$date.$server" -Append } }
Hi again,
I've tried you solution, but I get the following error:
#### PowerShell Script for installing ScheduledTaks for Hoogvliet OEG #### ### Setting up variables ### ## Set computername as variable ## $server = $env:computername #$server ## Set working dir as variable ## $dir = "\\$server\extapps\oeg" #$dir ## Set current date + time as variable ## $date = Get-Date -format "yyyy-MM-dd" $time = Get-Date -format "HH:mm:ss" #$Date ## Set logfiles directory as variable ## $log1 = "\\$server\extapps\oeg\logs" $log2 = "$log1\SYSOEGDAGLOG_$date.$server" #$log1 ## Set File Variables ## $ok = "$dir\system\OEG_ScheduledTasks.ok" #$ok ### Setting up functions ### function Make_Logfile1 { Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "*******************************" -Force Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "Bestand: OEG_ScheduledTasks.ps1" -Force Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "Datum+Tijd: $date $time" -Force Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "*******************************" -Force } function Remove_OK { Param( $ok ) if(Test-Path $ok){ Remove-Item $ok 'File Removed' | Out-File "$log1\SYSOEGDAGLOG_$date.$server"-Append }else{ 'File does not exist' | Out-File "$log1\SYSOEGDAGLOG_$date.$server" -Append } } Make_Logfile1 Remove_OK Test-Path : Cannot bind argument to parameter 'Path' because it is null. At line:34 char:18 + if(Test-Path $ok){ + ~~~ + CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand
But I've tested the $ok, and it returns the expected value of the path and file...
Retail Systems Specialist
- Edited by Equit Monday, December 11, 2017 10:31 AM
Monday, December 11, 2017 7:33 AM -
There is too much noise in your code for you to see your mistake.
$server = $env:computername $dir = "\\$server\extapps\oeg" $logfile = "\\$server\extapps\oeg\logs\SYSOEGDAGLOG_$date.$server" @" ******************************* Bestand: OEG_ScheduledTasks.ps1 Datum+Tijd: $(Get-Date -format 'yyyy-MM-dd HH:mm:ss') ******************************* "@ | Out-File $logfile function Remove_OK { Param ( $FIleName ) if (Test-Path $FIleName) { Remove-Item $FIleName -Whatif 'File Removed' | Out-File $logfile -Append } else { 'File does not exist' | Out-File $logfile -Append } } Remove_OK "$dir\system\OEG_ScheduledTasks.ok"
When you over-comment and create and assign too many variables it becomes difficult to understand your own code. Starting over an simplifying can help you find your logic and design errors.
\_(ツ)_/
- Edited by jrv Monday, December 11, 2017 12:52 PM
- Proposed as answer by frank_songMicrosoft contingent staff Thursday, December 21, 2017 9:16 AM
- Marked as answer by jrv Thursday, December 21, 2017 12:12 PM
Monday, December 11, 2017 12:52 PM -
Hi,
Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.
Best Regards,
FrankPlease remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.Wednesday, December 13, 2017 8:50 AM -
Hi,
Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
FrankPlease remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.Thursday, December 21, 2017 9:16 AM -
Hi Equit,
Please try ;(semi-colon) instead of | (pipe) and check
function Remove_OK { $CheckOK = Test-Path $ok IF ($CheckOK -eq $true) {Remove-Item $ok ; Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "File Removed" -Force} Else {Write-Out "Bestand bestaat niet" ; Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "File does not exist" -Force} }
Make sure to vote me or mark this as answer if you find this needful.
Have a nice day!!
kam
Thursday, December 21, 2017 12:04 PM -
Hi Equit,
Please try ;(semi-colon) instead of | (pipe) and check
function Remove_OK { $CheckOK = Test-Path $ok IF ($CheckOK -eq $true) {Remove-Item $ok ; Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "File Removed" -Force} Else {Write-Out "Bestand bestaat niet" ; Add-Content -Path "$log1\SYSOEGDAGLOG_$date.$server" -Value "File does not exist" -Force} }
Make sure to vote me or mark this as answer if you find this needful.
Have a nice day!!
kam
This is not really helpful. The issue was resolved months ago.
\_(ツ)_/
Thursday, December 21, 2017 12:13 PM