Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在powershell中打开受密码保护的Excel_Excel_Powershell_Password Protection - Fatal编程技术网

在powershell中打开受密码保护的Excel

在powershell中打开受密码保护的Excel,excel,powershell,password-protection,Excel,Powershell,Password Protection,我正在尝试在powershell中打开一个受密码保护的excel工作表,并输出一个关于工作表中有多少行的报告 如果工作表没有密码保护,脚本工作绝对正常,但是如果设置了密码,我似乎无法让powershell打开它 我现在的剧本是 $Report = "S:\Business Support\excel tests\MI Tool - Live.csv" $path = "S:\Business Support\excel tests" [Array]$Results = $null $excelS

我正在尝试在powershell中打开一个受密码保护的excel工作表,并输出一个关于工作表中有多少行的报告

如果工作表没有密码保护,脚本工作绝对正常,但是如果设置了密码,我似乎无法让powershell打开它

我现在的剧本是

$Report = "S:\Business Support\excel tests\MI Tool - Live.csv"
$path = "S:\Business Support\excel tests"
[Array]$Results = $null
$excelSheets = Get-Childitem -Path $path -Include "MI Tool - Live.xlsm" -Recurse
$excel = New-Object -comobject Excel.Application
$excel.visible = $false
$password = "blablabla"
$updatelinks = 0


foreach($excelSheet in $excelSheets)
{
 $workbook = $excel.Workbooks.Open($excelSheet,$updatelinks,$password)
 $rowCount = $null

  $worksheet = $workbook.sheets.item("Data")
  $rowMax = ($worksheet.usedRange.rows).count
  $rowCount += $rowMax

$Results += New-Object Psobject -Property @{
    "File Name"=$excelSheet.Name
    "Row Count"=$rowCount}
$excelSheet.Name
$workbook.Sheets.count
$rowCount
}
$excel.quit()
Stop-Process -Name EXCEL -Force
$Results | select "File Name","Row Count" | Export-Csv $Report -NoTypeInformation
这是我得到的错误:

Exception calling "Open" with "3" argument(s): "Open method of Workbooks class failed"
At line:3 char:35
+  $workbook = $excel.Workbooks.Open <<<< ($excelSheet,$updatelinks,$password)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

You cannot call a method on a null-valued expression.
At line:5 char:37
+   $worksheet = $workbook.sheets.item <<<< ("Data")
    + CategoryInfo          : InvalidOperation: (item:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
使用“3”参数调用“Open”异常:“工作簿类的Open方法失败”
第3行字符:35

+$workbook=$excel.Workbooks.Open您的打开重载不正确。密码是第五个变量

expression.Open(文件名、updateLink、只读、格式、密码、WriteResPassword、IgnoreReadOnlyRecommended、源代码、分隔符、可编辑、通知、转换器、AddToMru、本地、损坏加载)

因此,我认为您需要先填充ReadOnly和格式化。您必须填充这些值

$excel.Workbooks.open($path,0,0,5,$password)

查看MSDN,了解2、3和4位置的值代表什么

试着运行
$password='blablabla'
-你会得到同样的结果吗?很遗憾,是的,非常感谢。抱歉,如果这是一个重复的问题,但我真的可以找到一个答案,解释它以及在这里!这就是为什么我把答案留在这里。被复制者确实有此代码,但没有解释问题。复制很弱,但答案包含了您需要看到的内容。@Matt-如果我只有VBA代码密码保护,但需要从powershell访问,该怎么办?