Solved

Help creating a log with Powershell

Posted on 2014-07-28
6
240 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 84

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Author Comment

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
A brief introduction to what I consider to be the best editor for PowerShell.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

791 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