Jump to content

Powershell script: Piping output not working


Recommended Posts

Posted (edited)

Hi,Trying to get this script to output a list of files that have been deleted but it doesn't seem to log correctly

 

foreach($computer in (Get-Content C:\Computers.txt)){
   Invoke-Command -ComputerName $computer -ScriptBlock {
       $logfile = "C:\LOG.txt"
       Get-ChildItem C:\Windows\Temp | Remove-Item -Force -Recurse -Verbose -ErrorAction SilentlyContinue | Out-File -FilePath $logfile -Verbose -Append
   }
}

The files/folders within the C:\Windows\Temp folders are deleted from all the PCs this script runs against and a "LOG.txt" is created on all PCs but all the files are empty. I'd thought it may confirm what files have successfully been deleted or am I wrong?

 

Thanks

Edited by toffee_paul
Posted
Could be a permissions issue on root of C:\ the location of the LOG.txt file?

 

This would be my guess too - how about saving the logs to a network location so they're all in one place and easier to access rather than having to hop between machines if you want to check logs, you could set permissions to the location easily too.

Posted

Originally I was trying to save to a network location and this failed too, that's when I tried locally. Thought I'd try and figure out why the local file wasn't working first.

 

It's strange as the files are created locally they're just empty. So the script will create the file so am I misinterpretating how PowerShell piping works?

Posted (edited)

foreach($computer in (Get-Content C:\Computers.txt)){
  Write-Host Cleanup starting: $computer...
   Invoke-Command -ComputerName $computer -ScriptBlock {
       $logfile = "\\SERVER\Software" + $env:computername + ".txt"
       #$logfile = "C:\" + $env:computername + ".txt" # File created but is empty
       Get-ChildItem C:\Windows\Temp | Remove-Item -Force -Recurse -Verbose -ErrorAction SilentlyContinue | Out-File -FilePath $logfile -Verbose -Append
      }
Write-Host Cleanup complete: $computer...
}

 

On line 4 I get this error when trying to save to a network share with correct permissions set:

Access to the path '\\SERVER\Software\PC1.txt' is denied.

On line 5 I don't get an error, file is created but is empty.

I think there's two issues here, 1. why is the file empty when saved locally, 2. why if permissions are correct do I get access denied.

Edited by toffee_paul
Posted

Just had a look again at your code again and you are not actually appending anything to the log file. You need to do something like this .

 

Remove-Item -Force -Verbose  | Add-Content C:\LOG.txt

Posted (edited)

Unfortunately this didn't work either. Files are created locally but still empty.

 

Here's the console output and this is the info I'd like to see in the log file...

Cleanup starting: PC1 ...
VERBOSE: Performing the operation "Remove Directory" on target "C:\Windows\Temp\7d034bea-5d9f-42ad-a6c9-98e972294306".
VERBOSE: Performing the operation "Remove File" on target "C:\Windows\Temp\7d034bea-5d9f-42ad-a6c9-98e972294306\AgileDotNetRT.dll".
VERBOSE: Performing the operation "Remove Directory" on target "C:\Windows\Temp\PDQInventoryScanner".
VERBOSE: Performing the operation "Remove File" on target "C:\Windows\Temp\ASPNETSetup_00000.log".
VERBOSE: Performing the operation "Remove File" on target "C:\Windows\Temp\ASPNETSetup_00001.log".
VERBOSE: Performing the operation "Remove File" on target "C:\Windows\Temp\dd_NDP46-KB4457016-x64_decompression_log.txt".
Cleanup complete: PC1 ...

Edited by toffee_paul
Posted

After a quick google it appears that Remove-Item doesn't have an output so your script is working correctly, advice being to redirect the verbose output with "4>>C:\LOG.txt" (i haven't tested this)

 

foreach($computer in (Get-Content C:\Computers.txt)){
   Invoke-Command -ComputerName $computer -ScriptBlock {
       $logfile = "C:\LOG.txt"
       Get-ChildItem C:\Windows\Temp | Remove-Item -Force -Recurse -Verbose -ErrorAction SilentlyContinue 4[color=#666600]>> [/color][font=Verdana]C:\LOG.txt[/font]
   }
}

Posted

Yep seen that too a few mins ago on the TechNet page under Outputs for the Remove-Item command. Seen the 4>&1 trick on another forum and added it to my code which works well now.

 

Get-ChildItem C:\Windows\Temp | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue -Verbose 4>&1 | Tee-Object -FilePath $logfile -Append

 

Just got to figure out why these won't save to the network now. I'm sure it's something to do with the Invoke-Command

 

Thanks ThomL

Posted

Isn't 4>&1 more of a linux shell way of doing things? 4>> reduces piping further to Tee-Object so I'd assume runs quicker/cleaner.

 

The fileshare connectivity issues are going to be credential driven, I'd assume because you are already remoting to a device the second hop to another device (fileshare) is being blocked. Messy but a way of resolving without messing with credentials/credssp could be that once the script is done with the first for-each loop you start another for-each looping through to harvest the files from the clients C$ to your local machine?

Posted

Yes been looking into the CredSSP and it's a faff to say the least for such a small basic script. I agree the way to go for now will be to move the logs over to my machine by way of another for-each loop.

Thanks ThomL

  • Thanks 1
Posted (edited)

 
Try{
     Remove-Item C:\test\test1.txt - erroraction stop
     Write-Output "Deleted File Test1.txt - From dir C:\test" | out-file c:\test\log.txt -append
}  Catch {
   Write-Output "Failed to delete File test1.txt - from dir C:\test" | out-file C:\test\log.txt - append
}

 

Just thrown that together on my phone, should work. You need to add it in to your % loop but i would always say try to use try catch in your scripts when actually doing something.....

Edited by HPlum78

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...