This is my remote computer file transfer PowerShell script:
#--------------------------------------------------------[Initialisations]-------------------------------------------------------
#Clears the contents of the DNS client cache
Clear-DnsClientCache
#Loading script configuration
$configuration = Get-Content '.\Resources\Remote Computer File Transfer Configuration.cfg' | Select-Object | ConvertFrom-StringData
#Initializing report file
New-Item -Path $configuration.ReportFile -ItemType File
$fileList = Get-Content -Path $configuration.FileList
$computerList = Get-Content -Path $configuration.ComputerList
#Initializing file counters
$successfulTransfers = 0
$failedTransfers = 0
#---------------------------------------------------------[Functions]----------------------------------------------------------
function Write-Log
{
param
(
[Parameter(Position = 0, Mandatory = $false)]
[String]
$OperationSuccessful,
[Parameter(Position = 1, Mandatory = $false)]
[String]
$Message,
[Parameter(Position = 2, Mandatory = $false)]
[String]
$LogSeparator
)
if($null -eq $LogSeparator)
{
$timestamp = Get-Date -Format "yyyy.MM.dd. HH:mm:ss:fff"
$logEntry = $timestamp + " - " + $Message
}
else
{
$logEntry = $LogSeparator
}
if($OperationSuccessful -eq "Successful")
{
Write-Host $logEntry -ForegroundColor Green -BackgroundColor Black
}
elseif($OperationSuccessful -eq "Failed")
{
Write-Host $logEntry -ForegroundColor Red -BackgroundColor Black
}
elseif($OperationSuccessful -eq "Partial")
{
Write-Host $logEntry -ForegroundColor Blue -BackgroundColor Black
}
else
{
Write-Host $logEntry -ForegroundColor Yellow -BackgroundColor Black
}
Add-content -Path $configuration.LogFile -Value $logEntry
Add-content -Path $configuration.ReportFile -Value $logEntry
}
function Send-Report
{
param
(
[Parameter(Position = 0, Mandatory = $true)]
[string]
$FinalMessage
)
if($configuration.SendReport -eq "true")
{
$body = $configuration.Body + "`n" + $FinalMessage
Send-MailMessage -SmtpServer $configuration.SmtpServer `
-Port $configuration.Port `
-To $configuration.To `
-From $configuration.From `
-Subject $configuration.Subject `
-Body $body `
-Attachments $configuration.ReportFile
Remove-Item -Path $configuration.ReportFile
}
}
#---------------------------------------------------------[Execution]----------------------------------------------------------
Write-Log -LogSeparator $configuration.LogTitle
Write-Log -LogSeparator $configuration.LogSeparator
#Get credential from user input
$credential = Get-Credential
$message = "User " + $credential.UserName + " entered credentials"
Write-Log -Message $message
foreach($file in $fileList)
{
if((Test-Path -Path $file) -eq $true)
{
$message = "Successfully checked " + $file + " file - ready for transfer."
Write-Log -OperationSuccessful "Successful" -Message $message
}
else
{
$message = "Failed to access " + $file + " file. It does not exist."
Write-Log -OperationSuccessful "Failed" -Message $message
$message = "Script stopped - MISSING FILE ERROR"
Write-Log -OperationSuccessful "Failed" -Message $message
Write-Log -Message $configuration.LogSeparator
Send-Report -FinalMessage $message
Exit
}
}
$message = "Successfully accessed all files - ready for transfer"
Write-Log -OperationSuccessful "Successful" -Message $message
#Start file transfer
Write-Log -Message "Started file transfer"
foreach($computer in $computerList)
{
#Mapping network drive
if((Test-Connection -TargetName $computer -Quiet -Count 1) -eq $true)
{
$message = "Successfully accessed " + $computer + " remote computer"
Write-Log -OperationSuccessful "Successful"-Message $message
#Network path creation to D partition on the remote computer
$partition = "\D$"
$networkPath = "\\" + $computer + $partition
#Try to create network drive to D partition on the remote computer
if(New-PSDrive -Name "T" -PSProvider "FileSystem" -Root $networkPath -Credential $credential)
{
$message = "Successfully mapped network drive to D partition on the " + $computer + " remote computer"
Write-Log -OperationSuccessful "Successful" -Message $message
$driveMappingSuccessful = $true
}
else
{
$message = "Failed to map network drive to D partition on the " + $computer + " remote computer"
Write-Log -OperationSuccessful "Failed" -Message $message
#Network path creation to C partition on the remote computer
$partition = "\C$"
$networkPath = "\\" + $computer + $partition
#Try to create network drive to C partition on the remote computer
if(New-PSDrive -Name "T" -PSProvider "FileSystem" -Root $networkPath -Credential $credential)
{
$message = "Successfully mapped network drive to C partition on the " + $computer + " remote computer"
Write-Log -OperationSuccessful "Successful" -Message $message
$driveMappingSuccessful = $true
}
else
{
$message = "Failed to map network drive to C partition on the " + $computer + " remote computer - Credential not valid"
Write-Log -OperationSuccessful "Failed" -Message $message
$driveMappingSuccessful = $false
}
}
}
else
{
$message = "Failed to access " + $computer + " remote computer - OFFLINE"
Write-Log -OperationSuccessful "Failed" -Message $message
$driveMappingSuccessful = $false
}
if($driveMappingSuccessful)
{
$path = "T:\" + $configuration.TransferFolder
if((Test-Path $path) -eq $true)
{
$message = "Successfully accessed " + $path + " folder"
Write-Log -OperationSuccessful "Successful" -Message $message
$deployingFolderSuccessful = $true
}
else
{
$message = "Failed to access " + $path + " folder - MISSING FOLDER ERROR"
Write-Log -OperationSuccessful "Failed" -Message $message
try
{
New-Item -Path $path -ItemType "Directory"
}
catch
{
Write-Log -OperationSuccessful "Failed" -LogSeparator $_.Exception
}
if((Test-Path $path) -eq $true)
{
$message = "Successfully created " + $path + " folder"
Write-Log -OperationSuccessful "Successful" -Message $message
$deployingFolderSuccessful = $true
}
else
{
$message = "Failed to create " + $path + " folder"
Write-Log -OperationSuccessful "Failed" -Message $message
$deployingFolderSuccessful = $false
}
}
if($deployingFolderSuccessful)
{
$fileList = Get-Content -Path $configuration.FileList
foreach($file in $fileList)
{
#File name extraction from file full path
$fileName = Split-Path $file -leaf
try
{
Copy-Item -Path $file -Destination $path -Force
}
catch
{
Write-Log -OperationSuccessful "Failed" -LogSeparator $_.Exception
}
$transferDestination = Join-Path -Path $path -ChildPath $file
if(Test-Path -Path $transferDestination)
{
$message = "Successfully transferred " + $fileName + " file to " + $transferDestination + " folder"
Write-Log -OperationSuccessful "Successful" -Message $message
$successfulTransfers ++
}
else
{
$message = "Failed to transfer " + $fileName + " file to " + $path + " folder"
Write-Log -OperationSuccessful "Failed" -Message $message
$failedTransfers ++
}
}
}
else
{
$message = "Canceld file transfer to " + $computer + " remote computer"
Write-Log -OperationSuccessful "Failed" -Message $message
}
}
else
{
$message = "Canceld file transfer to " + $computer + " remote computer"
Write-Log -OperationSuccessful "Failed" -Message $message
}
#Network drive removal
if($driveMappingSuccessful)
{
Remove-PSDrive -Name "T"
}
}
$message = "Completed Remote Computer File Transfer PowerShell Script"
Write-Log -Message $message
if($successfulTransfers -gt 0)
{
$message = "Successfully transferred " + $successfulTransfers + " files to " + $Destination + " folder"
Write-Log -OperationSuccessful "Successful" -Message $message
}
if($failedTransfers -gt 0)
{
$message = "Failed to transfer " + $failedTransfers + " files to " + $Destination + " folder"
Write-Log -OperationSuccessful "Failed" -Message $message
}
if(($successfulTransfers -gt 0 ) -and ($failedTransfers -eq 0))
{
$message = "Successfully transferred all files to " + $Destination + " folder"
Write-Log -OperationSuccessful "Successful" -Message $message
}
elseif(($successfulTransfers -gt 0 ) -and ($failedTransfers -gt 0))
{
$message = "Successfully transferred some files to " + $Destination + " folder with some failed"
Write-Log -OperationSuccessful "Partial" -Message $message
}
elseif(($successfulTransfers -eq 0 ) -and ($failedTransfers -gt 0))
{
$message = "Failed to transfer any file to " + $Destination + " folder"
Write-Log -OperationSuccessful "Failed" -Message $message
}
Write-Log -Message $configuration.LogSeparator
#Sends email with detailed report and deletes temporary report log file
Send-Report -FinalMessage $message
This is .cfg file:
LogTitle = *********************************************** Remote Computer File Transfer PowerShell Script Log ************************************************
LogSeparator = ******************************************************************************************************************************************************
SendReport = true
LogFile = .\\Resources\\Remote Computer File Transfer Log.log
ReportFile = .\\Resources\\Report.log
FileList = .\\Resources\\File-Paths.txt
ComputerList = .\\Resources\\Computer-List.txt
TransferFolder = _INSTALL
SmtpServer = smtp.mail.com
Port = 25
To = [email protected]
From = [email protected]
Subject = Remote Computer File Transfer Report
Body = This is an automated message sent from PowerShell script. Remote Computer File Transfer PowerShell Script has finished executing.
Something is wrong with logging. It must be a problem with Write-Log function. The script actually does the job but nothing is written on the console by Write-Log function, and it just writes this in the picture and I can't figure out why.
Grateful in advance!

<# .SYNOPSISstuff.if($null -eq $LogSeparator)intoif(!$LogSeparator)orif([string]::IsNullOrWhiteSpace($LogSeparator)). Since you declared this parameter as string, if unused, the value will be an empty string, not$null.Rawswitch and leave out the| Select-Objectwhich has no reason to be there. Of course in theExecutionpart of the code, there is nothing to actually get the files listed in the$configuration.FileList, but that's a different question..