Solved

Help creating a log with Powershell

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

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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 procedure for exporting installed hotfix details of remote computers using powershell
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now