PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 14, 2022

[FIXED] How to log copied files and check network availability with powershell?

 November 14, 2022     copy, error-handling, logfile, move, powershell     No comments   

Issue

I'm currently working on my first powershell script and can't make any progress.

The aim of the script is that I can automatically move or copy files from one network drive to another at a specific time. And the whole thing should be logged. My code so far looks like this:

Param(
    [string]$src,    
    [string]$trgt,
    [string]$log,
    [string]$plot
)

<# 
try {
     New-SmbMapping -LocalPath $src -RemotePath $trgt -Persistent $True
    } catch {
     Write-Host "There was an error mapping $src to $trgt"
}
#>
# New-SMBMapping -Localpath $src -Remotepath $trgt -persistent $true

Start-Transcript $log

if ($plot -eq "copy")
{
    Copy-Item -Path $src -Destination $trgt -Recurse
    Write-Host -ForegroundColor Green "copy finished."
    Stop-Transcript
}

elseif ($plot -eq "move")
{
    Move-Item -Path $src -Destination $trgt
    Write-Host -ForegroundColor Green "move finished."
    Stop-Transcript
}

First of all, I'm still missing error handling here, in case something shouldn't work. If, for example, the source path or the target path, i.e. the network drives, are not available. How can I check if a network drive is available?

And secondly, I'm not really able to log rightly yet. I would like to record in a log file what is moved or copied where and when.

Can someone help me?


Solution

In case of error handling try/catch - as you did - is the right way to go, you may set the erroraction to stop -erroraction:stop to ensure any error is returned as terminating error because try/catch will only consider those. Also if you catch an error you should return an error by using write-error instead of write-host, and also include the exception message returned by the cmdlet:

try {
    New-SmbMapping -LocalPath $src -RemotePath $trgt -Persistent $True -ErrorAction:stop
}
catch {
    write-error "There was an error mapping $src to $trgt - Exception: $_"
}

In case of logging, you may add the parameter -passthru which returns the result of the operation. By doing so you can collect the operation result in variable and write it to the logfile, e.g.:

if ($plot -eq "copy"){
    try {
        $result = Copy-Item -Path $src -Destination $trgt -Recurse -PassThru -ErrorAction:stop
        "copy item $src to $result.fullname" | Add-Content -Path [logfilepath]
    }
    Catch {
        write-error "Failed to copy file: $src to $trgt - Exception: $_"
        "Failed to copy item: $src to $trgt - Exception: $_" | Add-Content -Path [logfilepath]
    }
}
elseif ($plot -eq "move"){
    try {
        $result = Move-Item -Path $src -Destination $trgt -PassThru -ErrorAction:stop
        "move item $src to $result.fullname" | Add-Content -Path [logfilepath] 
    }
    Catch {
        write-error "Failed to move file: $src to $trgt - Exception: $_"
        "Failed to move file: $src to $trgt - Exception: $_" | Add-Content -Path [logfilepath]
    }
}

But in the current scenario move/copy-item does not return you more information as you already did know (source/destination). Also if you process a batch of files and not only a single item it may be better to collect the operation results in a array and write it after the processing one time to the disk...



Answered By - Toni
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing