规格:
-
OS Windows 10
-
SQL Server 2012
使用的语言和软件:
-
Powershell
-
Excel 2010
码:
#Define locations and delimiter
$csv = "\\READYSHARE\USB_Storage\Trucking_Inc\SomeFile.csv" #Location of the source file
$xlsx = "\\READYSHARE\USB_Storage\Trucking_Inc\SomeFile2.xlsx" #Desired location of output
$delimiter = "," #Specify the delimiter used in the file
# Create a new Excel workbook with one empty sheet
$excel = New-Object -ComObject excel.application
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
# Build the QueryTables.Add command and reformat the data
$TxtConnector = ("TEXT;" + $csv)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
$query.TextFileOtherDelimiter = $delimiter
$query.TextFileParseType = 1
$query.TextFileColumnDataTypes = ,1 * $worksheet.Cells.Columns.Count
$query.AdjustColumnWidth = 1
# Execute & delete the import query
$query.Refresh()
$query.Delete()
# Save & close the Workbook as XLSX.
$Workbook.SaveAs($xlsx,51)
$excel.Quit()
作业错误:以用户身份执行:NT Service \ SQLAgent $ Server . ... l.exe -file“C:\ Scripts \ trasaction.ps1”Microsoft Excel无法访问文件'\ READYSHARE \ USB_Storage \ Trucking_Inc \ SomeFile2.csv' . 有几个可能的原因:文件名或路径不存在 . 该文件正被另一个程序使用 . 您尝试保存的工作簿与当前打开的工作簿具有相同的名称 . 在C:\ Scripts \ trasaction.ps1:12 char:1 $ Workbook = $ excel.Workbooks.Open($ ExcelFilePath)~~~~~~~~~~~~~~~~~~~~~~~~ 〜/ ~~~~~~~~~~~~~~~~~~~~~~~分类信息:OperationStopped:(:) [],COMException FullyQualifiedErrorId:System.Runtime.InteropServices.COMException你不能调用一个null值表达式的方法 . 在C:\ Scripts \ trasaction.ps1:13 char:1 $ Worksheet = $ Workbook.Sheets.Item(1)~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~ CategoryInfo:InvalidOperation:(:) [],RuntimeException FullyQualifiedErrorId:InvokeMethodOnNull您不能在空值表达式上调用方法 . 在C:\ Scripts \ trasaction.ps1:21 char:1 Out-File -FilePath C:\ Scripts \ process.txt -InputObject $ Columns.Cells ... ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~ CategoryInfo:InvalidOperation:(:) [],RuntimeException FullyQualifiedErrorId:InvokeMethodOnNull您不能在空值表达式上调用方法 . 在C:\ Scripts \ trasaction.ps1:22 char:1 Write-Host $ Columns.Cells(5,5).Value ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~ CategoryInfo:InvalidOperation:(:) [],RuntimeException FullyQualifiedErrorId:InvokeMethodOnNull您不能在空值表达式上调用方法 . 在C:\ Scripts \ trasaction.ps1:161 char:1 $ eRow = $ Worksheet.cells.item(1,1).entireRow ~~~~~~~~~~~~~~~~~~~~类别信息:InvalidOperation :( :) [],RuntimeException:FullQualifiedErrorId:InvokeMethodOnNull您不能在空值表达式上调用方法 . “~~~~~~~~在C:\ Scripts \ trasaction.ps1:162 char:1 $ active = $ eRow.activate()~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :InvalidOperation:(:) [],RuntimeException FullyQualifiedErrorId:InvokeMethodOnNull您不能在空值表达式上调用方法 . 在C:\ Scripts \ trasaction.ps1:163 char:1 $ active = $ eRow.insert($ xlShiftDown)~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~ CategoryInfo:InvalidOperation:(:) [],RuntimeException FullyQualifiedErrorId:InvokeMethodOnNull您不能在空值表达式上调用方法 . 在C:\ Scripts \ trasaction.ps1:164 char:1 $ Worksheet.Cells.item(1,1)=“TRANSACTION_DATE”~~~~~~~~~~~~~~~~~~~~~~ 〜/ ~~~~~~~~~表达 . 在C:\ Scripts \ trasaction.ps1:165 char:1 $ Worksheet.Cells.item(1,2)=“AMOUNT”~~~~~~~~~~~~~~~~~~~~~~ 〜/ ~~~~~~~~~~~~~~ CategoryInfo:InvalidOperation:(:) [],RuntimeException FullyQualifiedErrorId:InvokeMethodOnNull您不能在空值表达式上调用方法 . 在C:\ Scripts \ trasaction.ps1:166 char:1 $ Worksheet.Cells.item(1,3)=“TYPE_OF_AUTHORIZATION_AND_DATE”~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~分类信息:InvalidOperation :( :) [], RuntimeException FullyQualifiedErrorId:InvokeMethodOnNull您无法在空值表达式上调用方法 . 在C:\ Scripts \ trasaction.ps1:167 char:1 $ Worksheet.Cells.item(1,4)=“CHECK_NUMBER”~~~~~~~~~~~~~~~~~~~~~~类别信息:InvalidOperation :( :) [],RuntimeException FullyQualifiedErrorId:InvokeMethodOnNull你不能...处理退出代码0.步骤成功 .
经过尝试的事情 .
-
给出SqlServerAgent的perminsions . 哪个readyshare不会让我这样做 .
-
将带有powershell脚本名称的文件路径放入要从C:驱动器执行的bat文件中以进入文件路径 .
笔记:
通过手动打开cmd并执行,PowerShell脚本在命令提示符下运行良好 . 甚至可以手动从ISE(x86)中运行 .
但是,从作业中使用它时,您会收到上面列出的错误 .
我试图让readyshare驱动器授予SQLSeverAgent权限,但恢复原始权限 . 我的路由器是一个nightHawk路由器,我没有看到在路由器中的任何设置,以通过Windows 10尝试,但恢复到原始权限 .
从邮件中检查了这些建议
-
文件名或路径不存在 .
-
该文件正由另一个程序使用 .
-
您尝试保存的工作簿与当前打开的工作簿具有相同的名称 .
没有申请 .
有什么工作或我可能不知道的选项吗?需要一些指导或指向正确的方向 .