Author Posts

April 4, 2014 at 12:20 pm

< # $searchOption =[IO.SearchOption]::AllDirectories $fileEntries = [IO.Directory]::GetFiles("\\cd1001-c100\public\isantillan", "*.*", $searchOption); foreach($fileName in $fileEntries) { $fileName | Out-File "D:\Backups\Pdrive cleanup\files_to_delete.txt" -Append } or $searchOption =[IO.SearchOption]::AllDirectories $fileEntries = [IO.Directory]::GetFiles("\\cd1001-c100\public\isantillan", "*.*", $searchOption); $fileEntries | Out-File "D:\Backups\Pdrive cleanup\files_to_delete.txt" #>

WriteOutput help

Thanks to Don and Dave I was able to get the full UNC path of files in a network location. (see code above). Now with this text file, i will be deleting all the files that are listed using the code below. Everything works fine but i would like to log the files that are being deleted and in case if it fails for some reason, i'd be able to see which files and why. How do i output Remove-Item to text? Please advise.


Get-Content "D:\Backups\Pdrive cleanup\files_to_delete.txt" | Remove-Item

April 4, 2014 at 4:29 pm

I'd probably do something along these lines:

Get-Content "D:\Backups\Pdrive cleanup\files_to_delete.txt" |
ForEach-Object {
    $path = $_

    try
    {
        Remove-Item -Path $path -ErrorAction Stop
        "'$path' Deleted Successfully"
    }
    catch
    {
        "Error deleting '$path': $($_.Exception.Message)"
    }
} |
Out-File -FilePath "D:\Backups\Pdrive cleanup\fileDeletion.log"

April 7, 2014 at 9:36 am

[quote=14339]I'd probably do something along these lines:

Get-Content "D:\Backups\Pdrive cleanup\files_to_delete.txt" |
ForEach-Object {
    $path = $_

    try
    {
        Remove-Item -Path $path -ErrorAction Stop
        "'$path' Deleted Successfully"
    }
    catch
    {
        "Error deleting '$path': $($_.Exception.Message)"
    }
} |
Out-File -FilePath "D:\Backups\Pdrive cleanup\fileDeletion.log"

[/quote]

Hello Dave, I was able to get it it to output by using the code below. This script is to be run on the background so no status messages needs to be displayed.

$timestamp = Get-Date -UFormat "%Y-%m-%d"
$getContent = Get-Content "D:\Backups\Pdrive cleanup\files_to_delete.txt" | Remove-Item -force -verbose *>> "D:\Backups\Pdrive cleanup\logs\$timestamp.txt"

It works perfectly fine with PowerShell 4.0. However, when I copied it to the server, I received the attached error. After some digging, I found out that it's a version issue. The version that is currently installed on the server is PowerShell 2.0.

http://technet.microsoft.com/en-us/library/hh847746.aspx
NOTE: The All (*), Warning (3), Verbose (4) and Debug (5) redirection operators were introduced
in Windows PowerShell 3.0. They do not work in earlier versions of Windows PowerShell.

How do I get the same results using Powershell 2.0 with the code I provided above?

April 7, 2014 at 10:33 am

Do you need Verbose, Warning or Debug output in the log file, or only errors? If it's errors only, you can use the 2>> operator in PowerShell 2.0 (instead of *>> in your example).

April 7, 2014 at 11:08 am

Unfortunately, it's not only errors that i would like logged but everything that 'Remove-Item' does.

April 7, 2014 at 11:19 am

I don't think I'm understanding your question, then. I suggested code which outputs information when Remove-Item successfully deletes something, and when it produces errors, but you went in a different direction with the redirection operators. Redirection would only give you verbose / debug / error / warning output, since Remove-Item doesn't produce anything on the Output stream by itself.

April 7, 2014 at 11:27 am

I needed to see the what Remove-Item does. Which is why i used -verbose to which i used *>> operator in order to capture it and output it to text. This is what i get in the text file 2014-04-07.txt

Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\20amp.txt".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\Capture.PNG".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\Capture1.PNG".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\Capture2.PNG".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\Capture3.PNG".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\PhotomergeUI.8BF".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\public_drive_log.txt".
Remove-Item : Cannot find path '\\cd1001-c100\public\isantillan\Thumbs.db' because it does not exist.
At line:2 char:77
+ $getContent = Get-Content "D:\Backups\Pdrive cleanup\files_to_delete.txt" | Remo ...
+ ~~~~
+ CategoryInfo : ObjectNotFound: (\\cd1001-c100\p...illan\Thumbs.db:String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\capture\Capture.PNG".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\capture\Capture2.PNG".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\capture\Capture3.PNG".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\capture\Capture4.PNG".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\capture\Capture5.PNG".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\folder1\Robocopy.exe".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\folder2\Robocopy.exe".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\folder3\Robocopy.exe".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\Copier_unstoppable\UnstopCpy_5_2_Win2K_UP.exe".
Performing the operation "Remove File" on target "\\cd1001-c100\public\isantillan\Copier_unstoppable\UnstopCpy_Configure.jpg".
Remove-Item : Cannot find path '\\cd1001-c100\public\isantillan\Copier_unstoppable\Thumbs.db' because it does not exist.
At line:2 char:77
+ $getContent = Get-Content "D:\Backups\Pdrive cleanup\files_to_delete.txt" | Remo ...
+ ~~~~
+ CategoryInfo : ObjectNotFound: (\\cd1001-c100\p...pable\Thumbs.db:String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

April 7, 2014 at 11:41 am

I see. I have a logging module which is compatible with PowerShell 2.0, and can be used to capture that Verbose output, over on the TechNet Gallery: http://gallery.technet.microsoft.com/scriptcenter/Enhanced-Script-Logging-27615f85 .

It may also be possible to make use of the System.Management.Automation.PowerShell class to run your command, which would get you access to all of the stream output from the commands executed by that PowerShell object. Other than those options, I don't think there's any other way you can intercept and log Verbose stream output prior to PowerShell 3.0.

April 9, 2014 at 4:55 am

hi,

Will this work in powershell 2.0?. Add this function to you script. It should capture the calls to write-verbose. I do not have access to anything running powershell 2.0, so I cannot test it:


Function Global:Write-Verbose
{
Param(
[string] $Message
)
Add-Content -Path .\verbose.log -Value $Message
}

April 9, 2014 at 5:02 am

Hi Tore,

That works for intercepting explicit calls to the Write-Verbose cmdlet, but it would not catch verbose output from compiled cmdlets such as Remove-Item. That type of output goes through the Cmdlet.WriteVerbose() method.