Solved

Help creating a log with Powershell

Posted on 2014-07-28
6
239 Views
Last Modified: 2014-07-29
Hello Powershell experts,

I have the following code to move all the files in a folder called source to a different folder called target:

Get-ChildItem -Path "c:\source"  | select name | Move-Item -Destination "c:\target"

This works as expected.  Now I would like the name of each file moved written to a text file.  I'm not sure how to do this.  My initial thought was something like:

Get-ChildItem -Path "c:\source"  | select name | Move-Item -Destination "c:\target" | foreach-object {write-host "$_.name moved successfully" >> c:\movereport.txt}

This didn't work.  How would I accomplish this?

Thanks,
Nick
0
Comment
Question by:ndalmolin_13
  • 2
  • 2
  • 2
6 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40225674
Get-ChildItem -Path "c:\source"  | select fullname | % {
$filer = $_
try {
Move-Item $filer -Destination  "c:\target"
"$filer move was successful" | c:\success.log
}
catch {
"$filer  was not moved" | c:\failure.log
}

Open in new window

}
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 40226174
There were several issues with your command.
* When you're inside a string and you want to access an object's property (as in "$_.Name moved ..."), you need to use "$($_.Name) moved ...", otherwise the parser will evaluate that to "$_" and the string ".Name moved ....".
* "Write-Host", as its name implies, sends the pipeline output to the Powershell host (that is, the console), and that's where the output stops. Output sent to Write-Host will not be passed further down the pipeline. Just pass the string, or if you want to be explicit about sending it down the pipeline, pipe it to "Write-Output".
Get-ChildItem -Path "C:\Source" | ForEach-Object {
	Move-Item $_.FullName -Destination "C:\Target" -ErrorAction SilentlyContinue
	If ($?) {
		$Result = "Success"
		$Color = [ConsoleColor]::Green
	} Else {
		$Result = "Error"
		$Color = [ConsoleColor]::Red
	}
	"$($_.Name): $($Result)" | Write-Host -ForegroundColor $Color
	"$($_.Name): $($Result)" >> c:\movereport.txt
}

Open in new window

Or instead of using the redirection, you can use Add-Content with -PassThru instead of lines 10 and 11:
"$($_.Name): $($Result)" | Add-Content -Path "c:\movereport.txt" -PassThru | Write-Host -ForegroundColor $Color

Open in new window

Powershell 3.0 and later has the "Tee-Object" cmdlet that can send pipeline output to file and then pass it further down the pipeline; with that, you could use this instead of lines 10 and 11:
"$($_.Name): $($Result)" | Tee-Object -FilePath "c:\movereport.txt" -Append | Write-Host -ForegroundColor $Color

Open in new window


becraig,
Move-Item doesn't throw an exception when it can't move a file, it just errors out. You either need to check $?, or use -ErrorAction Stop.
0
 
LVL 29

Expert Comment

by:becraig
ID: 40226286
Thanks I was actually planning to rewrite with an error.clear at the start of each move and then use if/else to check error condition after the move writing the exception to the log.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 1

Author Comment

by:ndalmolin_13
ID: 40226786
What is the "$?" ?
0
 
LVL 83

Expert Comment

by:oBdA
ID: 40226841
That's a (boolean) default variable that is $True if the last cmdlet executed without error, $False otherwise.
Get-Item $ENV:SystemRoot | Out-Null; "Got a known item; expected result is True: " + $?
Get-Item ".\asd.fgh" -ea si; "Created an error; expected result is False: " + $?

Open in new window

0
 
LVL 1

Author Closing Comment

by:ndalmolin_13
ID: 40227131
Thanks for the help
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question