?
Solved

Help creating a log with Powershell

Posted on 2014-07-28
6
Medium Priority
?
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 85

Accepted Solution

by:
oBdA earned 2000 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
Need protection from advanced malware attacks?

Look no further than WatchGuard's Total Security Suite, providing defense in depth against today's most headlining attacks like Petya 2.0 and WannaCry. Keep your organization out of the news with protection from known and unknown threats.

 
LVL 1

Author Comment

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

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrā€¦

741 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