I have a script that does the following:
Check to see if the designated file backup location is online/present ... if NO... alert persons via e-mail ... if Yes, check to see if there are files in a $Source directory, if NO ... write to a log file a timestamp & that no files where copied, if YES ... rename the files by tacking on a prefix to the filename, then copy it to a $Destination and then move it to a backup location. In addition write to the log file the file name and time it was moved. Then copy the log file to the backup location as well. Finally I want to write/overwrite a Success.txt file to a monitored location where another script will utilize that file for other purposes.
Here's my issue, putting in the code for NEW-ITEM breaks the whole thing, every time and I don't understand why; here's the code, the errors I get are listed below and I have scrubbed the errors "to protect the innocent" :p
If I comment out this line, everything works as intended, but I don't get that Success.txt file that I need. ALSO if I run that new-item cmdlet from a PS prompt it works just fine.
$error.Clear()
#-----Create Objects-----#
$Source="\\Source\directory"
$Destination="\\Destination\directory"
$Bkp="\\BackUp\directory"
$Monitor="\\Path\to\write\text\file"
$Time=GET-DATE
$DateTime=Get-Date-format g
$folder='directory'
if(test-path-path$bkp)
{
if(Test-Path-Path$Source\*-include*.pdf, *.tif)
{
Get-ChildItem$Source\*-include*.jpg,*.tif,*.pdf-exclude*.log | % {Rename-item$_.Fullname ("HIRS_"+$_.Name)}
Add-Content-Path$Source\copy.log-value"**********BEGIN ACTION @ $time**********"
Copy-Item-path$Source\*-include*.pdf,*.tif-destination$Bkp-passthru | format-table-property LastWriteTime,Name -autosize | out-file$Source\copy.log-Append-Encoding ascii
Move-Item-path$Source\*.pdf-destination$Destination
Move-Item-path$Source\*.tif-destination$Destination
Add-Content-Path$Source\copy.log-value"**********STOP ACTION @ $time**********`r`n================================================`r`n"
Copy-Item$Source\copy.log$Bkp\copy.log
New-Item–itemtype file -Path$Monitor-name'SUCCESS.txt'-Value"Success at $time!"-force
}
ELSE
{
Add-Content-Path$Source\copy.log-value"**********BEGIN ACTION @ $time*********`r`n`r`n0 Files Copied`r`n`r`n**********STOP ACTION @ $time**********`r`n================================================`r`n"
}
}
ELSE
{
Add-Content-Path$Source\copy.log-value"**********BEGIN ACTION @ $time*********`r`n `r`n BACKUP LOCATION NOT AVAILABLE!!!`r`n$bkp`r`n0 Files Copied`r`n `r`n**********STOP ACTION @ $time**********`r================================================`r`n"
}
# Write each error to the custom Event Log
foreach ($errorRecordin$error)
{
Write-EventLog-LogName psLogs -Source'directory'-Message"$($errorRecord | Out-String)"-EventId 0 -EntryType Error
}
#-----END OF SCRIPT-----#
ERRORS:
+Add-Content-Path$Source\copy.log-value"**********BEGIN ACTION @ $time*** ...
+ ~
Unrecognized token in source text.
At \\Domain\netlogon\scripts\ps\FAX\5131.ps1:51 char:139
+ ... ***STOP ACTION @ $time**********`r`n============================================ ...
+ ~
Unrecognized token in source text.
At \\Domain\netlogon\scripts\ps\FAX\5131.ps1:56 char:69
+ Add-Content -Path $Source\copy.log -value "**********BEGIN ACTION @ $time***** ...
+ ~
Unrecognized token in source text.
At \\Domain\netlogon\scripts\ps\FAX\5131.ps1:56 char:184
+ ... ***STOP ACTION @ $time**********`r============================================== ...
+ ~
Unrecognized token in source text.
At \\Domain\netlogon\scripts\ps\FAX\5131.ps1:57 char:65
+Send-MailMessage-From mailer@domain.com-To"Recipient <Recipient@domain.com ...
+ ~
The '<' operator is reserved for future use.
At \\Domain\netlogon\scripts\ps\FAX\5131.ps1:57 char:99
+ ... .COM>", "XXXX, XXXX <XXX@XXXXX.COM>", "yyy, YYYY <YYY@YYYY.COM> ...
+ ~
Missing argument in parameter list.
At \\Domain\netlogon\scripts\ps\FAX\5131.ps1:57 char:109
+ ... (LIST OF EMAIL ADDRESSES) ...
+ ~
The '<' operator is reserved for future use.
At \\Domain\netlogon\scripts\ps\FAX\5131.ps1:57 char:139
+ ... (LIST OF EMAIL ADDRESSES) ..
+ ~
Missing argument in parameter list.
At \\Domain\netlogon\scripts\ps\FAX\5131.ps1:57 char:146
+ ... (LIST OF EMAIL ADDRESSES) ...
+ ~
The '<' operator is reserved for future use.
At \\Domain\netlogon\scripts\ps\FAX\5131.ps1:57 char:176
+ .... (LIST OF EMAIL ADDRESSES) ...
+ ~
Missing argument in parameter list.
Not all parse errors were reported. Correct the reported errors and try again.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnrecognizedToken