File system task error :"could not find file..." - sql-server

I have created a simple package in visual studio which use a ForEach Loop Container with a ForEach File Enumerator.
I got this error:
[File System Task] Error: An error occurred with the following error message: "Could not find file '\ALDRZFS01\altirrepdev01\WFM_Reports\PROD\QVC\tpDelivery-Src\ALDRZFS01\altirrepdev01\WFM_Reports\PROD\QVC\tpDelivery-Src\TP_EXPORT_220116_1.csv'.".
And these are the variables I have created:
Variable Name
Variable Value
varSourceFolder
\ALDRZFS01\altirrepdev01\WFM_Reports\PROD\QVC\tpDelivery-Src
varFileName
*.csv
varFilePath
#[User::varSourceFolder]+ "\" + #[User::varFileName]
varArchiveFolder
#[User::varSourceFolder]+ "\Archive\"+ (DT_WSTR,4)YEAR(GETDATE()) + (DT_WSTR,2)MONTH(GETDATE()) + (DT_WSTR,2)DAY(GETDATE())
varArchivePath
#[User::varArchiveFolder]+ "\" + #[User::varFileName]
Can anyone tell me how to fix this, please?

Related

SQL Error [42601]: ERROR: syntax error at or near "\" Position: 1 when use \copy

I try to copy csv to database table postgres using
\copy sik FROM '/Users/bhaskoromuthohar/dev/generate_file_ruby/12345620201214SIK01.csv' DELIMITER '|' csv;
But it raised
SQL Error [42601]: ERROR: syntax error at or near "\"
Position: 1
I googled the error but couldn't find the answer to my problem. Any bits of help is appreciated, thankyou
“Backslash commands” are only recognized by the command line client psql, so you will have to use that client.

Powershell script invoked by SQL Agent works differently

I have a pretty simple powershell script (to convert .xlsx files to .xls) like below:
$files = Get-ChildItem "C:\MyFolder\*.xlsx"
foreach ($file in $files) {
$Filepath = Get-Item -Path $file
$NewFilepath = Join-Path -path $Filepath.directory.fullname -ChildPath
"$($Filepath.basename).xls"
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false #or false
$Excel.displayalerts = $false
$Workbook = $Excel.Workbooks.Open("$Filepath".fullname,
[Type]::Missing,$true)
$Workbook.SaveAs("$NewFilepath",56)
#https://technet.microsoft.com/en-us/library/ff730962.aspx
$Workbook.Close()
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
Remove-Variable Excel
Remove-Item $Filepath
}
If I run this script from powershell directly it works just as expected. I mean for each .xlsx file it creates a new .xls file and removes given .xlsx file.
But when I invoke it by SQL Agent all the .xlsx files are simply removed with no creation corresponding .xls files.
SQL Job step
Type: Powershell
Run as: SQL Server Agent service account (checked folder permissioned and is full control)
Command: powershell -File "C:\MyScripts\MyConvertToXls.ps1"
The job does not fail. But when I go to the step execution details I can see the following message:
Message
Executed as user: ServiceAccount. ...rors did not stop the script:
A job step received an error at line 1 in a PowerShell script.
The corresponding line is 'powershell -File "C:\MyScripts\MyConvertToXls.ps1"'.
Correct the script and reschedule the job.
The error information returned by PowerShell is: 'Sorry, we couldn't find .
Is it possible it was moved, renamed or deleted? '
A job step received an error at line 1 in a PowerShell script.
The corresponding line is 'powershell -File "C:\MyScripts\MyConvertToXls.ps1"'.
Correct the script and reschedule the job.
The error information returned by PowerShell is:
'At C:\MyScripts\MyConvertToXls.ps1:16 char:1 + $Workbook = $Excel.Workbooks.Open("$Filepath".fullname,[Type]::Missin ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException '
A job step received an error at line 1 in a PowerShell script.
The corresponding line is 'powershell -File "C:\MyScripts\MyConvertToXls.ps1"'.
Correct the script and reschedule the job.
The error information returned by PowerShell is: ' + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException '
A job step received an error at line 1 in a PowerShell script.
The corresponding line is 'powershell -File "C:\MyScripts\MyConvertToXls.ps1"'.
Correct the script and reschedule the job. The error information returned by PowerShell is: ' '
A job step received an error at line 1 in a PowerShell script.
The corresponding line is 'powershell -File "C:\MyScripts\MyConvertToXls.ps1"'.
Correct the script and reschedule the job.
The error information returned by PowerShell is: 'You cannot call a method on a null-valued expression. '
A job step received an error at line 1 in a PowerShell script.
The corresponding line is 'powershell -File "C:\MyScripts\MyConvertToXls.ps1"'.
Correct the script and reschedule the job.
The error information returned by PowerShell is: 'At C:\MyScripts\MyConvertToXls.ps1:18 char:1 '
A job step received an error at line 1 in a PowerShell script.
The corresponding line is 'powershell -File "C:\MyScripts\MyConvertToXls.ps1"'.
Correct the script and reschedule the job. The error information returned by PowerShell is: '+ $Workbook.SaveAs("$NewFilepath",56) '
A job step received an error at line 1 in a PowerShell script.
The corresponding line is 'powershell -File "C:\MyScripts\MyConvertToXls.ps1"'.
Correct the script and reschedule the job.
The error information returned by PowerShell is: '+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '
A job step received an error at line 1 in a PowerShell script.
The corresponding line is 'powershell -File "C:\MyScripts\MyConvertToXls.ps1"'. Correct the script and reschedule the job.
The error information returned by PowerShell is: ' + CategoryInfo : InvalidOperation: (:) [], RuntimeException '
A job step received an error at line 1 in a PowerShell script. The corresponding line is 'powershell -File "C:\MyScripts\MyConvertToXls.ps1"'.
Correct the script and reschedule the job. The error information returned by PowerShell is: ' + FullyQualifiedErrorId : InvokeMethodOnNull '
A job step received an error at line 1 in a PowerShell script. The corresponding line is 'powershell -File "C:\MyScripts\MyConvertToXls.ps1"'.
Correct the script and reschedule the job. The error information returned by PowerShell is: ' '
A job step received an error at line 1 in a PowerShell script.
The corresponding line is 'powershell -File "C:\MyScripts\MyConvertToXls.ps1"... Process Exit Code 0. The step succeeded.
A PowerShell job step type expects a script but it looks like you are specifying the OS command powershell -File "C:\MyScripts\MyConvertToXls.ps1" to invoke the script.
Try a Operating System (CmdExec) job step type to invoke the command. Alternatively, paste the script into the PowerShell job step instead of running the external script.
I had same error, and found this solution.
You need to create folder Desktop
C:\Windows\System32\config\systemprofile\Desktop for 32 bit machine
C:\Windows\SysWOW64\config\systemprofile\Desktop for 64 bit machine
It's amaizing but it works

Getting an error when trying to execute powershell code via a SQL Agent job

I've setup a SQL Server 2016 agent job that has 2 steps that is executed by the same service account, Step 1 executes an SSIS package that writes 2 files out to a share. Step 2 executes embedded powershell code that combines these 2 files into a single file and creates a file on that same share where the 2 files were created by SSIS. When I execute the job, Step 1, the SSIS package completes successfully and creates the 2 files on this share. But when Step 2 executes I get the error below. Just a point of info, when I run this script locally from my windows 10 box, I have no issues.
I guess my question is, if SSIS can execute successfully with the same service account can this be a permissions issue for powershell scripts? I'm not a server admin so if this is a stupid questions please forgive my ignorance. Any help/direction anyone can provide would be appreciated.
Message
Executed as user: XXX\sqlsvc02. The job script encountered the following errors. These errors did not stop the script: A job step received an error at line 12 in a PowerShell script. The corresponding line is 'Get-Content $pathCounts, $pathDetails | Set-Content $outFile'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find path '\xxx\shared\MonthlyReports\BoundPoliciesAddedAtFaultEndorsementWithin10Days_Counts.csv' because it does not exist. ' A job step received an error at line 12 in a PowerShell script. The corresponding line is 'Get-Content $pathCounts, $pathDetails | Set-Content $outFile'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find path '\xxx\shared\MonthlyReports\BoundPoliciesAddedAtFaultEndorsementWithin10Days_Details.csv' because it does not exist. '. Process Exit Code 0. The step succeeded.
Here is my powershell script:
# Define variables
$path = '\\xxx\shared\MonthlyReports'
$pathCounts = Join-Path -Path $path -ChildPath 'BoundPoliciesAddedAtFaultEndorsementWithin10Days_Counts.csv'
$pathDetails = Join-Path -Path $path -ChildPath 'BoundPoliciesAddedAtFaultEndorsementWithin10Days_Details.csv'
$date = Get-Date
$dateStr = $date.ToString("yyyyMM")
# Define combined file name
$outFile = Join-Path $path -ChildPath("BoundPoliciesAddedAtFaultEndorsementsWithin10Days_" + $dateStr + ".csv")
# Execute combine files code
Get-Content $pathCounts, $pathDetails | Set-Content $outFile
Here is an image of what Step #2 looks like:

Powershell returns negative exit code while script results are correct

I have made the following PowerShell script:
Set-Location D:\folder1\folder2\folder3\folder4;
Get-ChildItem | Rename-Item -NewName {$_.BaseName.insert(19,'loadtime') + (Get-Date -Format HHmm) + $_.Extension};
The goal is to rename a file by adding loadtime at position 19 and a time stamp. While the results of running the scripts the following error message is returned:
Rename-Item : The input to the script block for parameter 'NewName' failed.
Exception calling "Insert" with "2" argument(s): "Specified argument was out
of the range of valid values.
Parameter name: startIndex"
At D:\folder1\folder2\folder3\folder4\folder5\RenameFile.ps1:2
char:38
+ Get-ChildItem | Rename-Item -NewName {$_.BaseName.insert(19,'loadtime') +
(Get-D ...
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Archive:PSObject) [Rename-
Item], ParameterBindingException
+ FullyQualifiedErrorId :
ScriptBlockArgumentInvocationFailed,
Microsoft.PowerShell.Commands.RenameItemCommand
When I run the script from my SSIS package I get the following error:
[Execute Process Task] Error: In Executing "PowerShell.exe" "-F D:\folder1\folder2\folder3\folder4\folder5\exec RenameFile.ps1" at "", The process exit code was "-196608" while the expected was "0".
I have tried to search for this error message but I could not find a page talking about this specific exit code.
The problem is though that while running the script commands from a PowerShell window I would still get the error message but the filename does change as expected. However since an error is returned the Execute Process Task in SSIS will fail.
My question is, what is the cause of this error? I assume it has to do with:
{$_.BaseName.insert(19,'loadtime') + (Get-Date -Format HHmm) + $_.Extension};
Wherein $.BaseName.insert(19,'loadtime') and (Get-Date -Format HHmm) and + $.Extension are seen as separate arguments.
My knowledge of PowerShell is not too great so this is just speculation on my part.
What would help me get rid of the error?
Thanks in advance!
As per the comments, the issue was caused by trying to insert into non-existing part of a string. This will raise an exception.
As a solution, make sure the indexed location exists, or just concatenate at the end. Like so,
$_.basename + 'loadtime' + (get-date -format hhMM) + $_.extension
The weird error code -196608 is actually a result of an error code represented as decimal (base 10) integer instead of hex value (base 16). Consider this:
[int]$i = -196608
$i.ToString('x')
fffd0000
What happens here is that the real error code is, in hex format, 0xFFFD0000. Because of Two's Compliment, large enough hex values actually represent negative decimal numbers.
As for this particular error code, it pops up every here and there without proper documentation. Should I hazard a guess, it has something to do with the fact that Powershell itself works fine, but the script it was told to run didn't.

Powershell Exceptions/Registry Key

I have a script - with its according module - that lists from SQL all the instances from different Servers within the company I'm interning for.
This script works in another host machine but not in mine.
I get a MethodInvocationException and the error message is the following:
Exception calling "OpenSubKey" with "1" argument(s): "Requested registry access is not allowed."
The error comes from a file inside a module, not from the actual script.
I know its very general and Im not allowed to show the code, but any guidlines/tips or heads up on how to solve it? I tried playing around with something called registry key, but I'm not sure how it works.
Help please?
You can use Process Monitor to view all the registry accesses. This will show the one for which access is denied, and from there you can grant yourself the necessary access.
You will need to set suitable filters or you will be overwhelmed by the number of events! I would suggest the following:
Path: Operation is "RegOpenKey"
Result: is "Access Denied"
Option 2
Execute the command in PowerShell, then execute the following command:
$> $Error[0]
This should hopefully print something like the following, which will tell you the key it failed on:
dir : Requested registry access is not allowed.
At line:1 char:1
+ dir
+ ~~~
+ CategoryInfo : PermissionDenied: (HKEY_LOCAL_MACHINE\SECURITY:String) [Get-ChildItem], SecurityException
+ FullyQualifiedErrorId : System.Security.SecurityException,Microsoft.PowerShell.Commands.GetChildItemCommand

Resources