我是一名系统管理员,我正在努力学习如何使用powershell ...我以前从未做过任何类型的脚本或编码,而且我通过technet脚本中心和在线论坛学习在线自学
我想要完成的是打开一个excel电子表格从中获取信息(用户名和密码),然后将其输出到PowerShell的命令提示符中。当我尝试这样做时,无论如何我得到一个名为“InvokeMember”的异常,这是我到目前为止的代码:
function Invoke([object]$m, [string]$method, $parameters)
{
$m.PSBase.GetType().InvokeMember(
$method, [Reflection.BindingFlags]::InvokeMethod, $null, $m, $parameters,$ciUS )
}
$ciUS = [System.Globalization.CultureInfo]'en-US'
$objExcel = New-Object -comobject Excel.Application $objExcel.Visible = $False $objExcel.DisplayAlerts = $False
$objWorkbook = Invoke $objExcel.Workbooks.Open "C:\PS\User Data.xls" Write-Host "Numer of worksheets: " $objWorkbook.Sheets.Count
$objWorksheet = $objWorkbook.Worksheets.Item(1) Write-Host "Worksheet: " $objWorksheet.Name
$Forename = $objWorksheet.Cells.Item(2,1).Text $Surname = $objWorksheet.Cells.Item(2,2).Text
Write-Host "Forename: " $Forename Write-Host "Surname: " $Surname
$objExcel.Quit() If (ps excel) { kill -name excel}
我在论坛和文章上阅读了很多关于如何尝试解决美国问题的不同帖子,但我似乎无法解决这个问题,并希望有人可以提供帮助!
这是我提到的Exeption问题:
Exception calling "InvokeMember" with "6" argument(s): "Method 'System.Management.Automation.PSMethod.C:\PS\User Data.x
ls' not found."
At C:\PS\excel.ps1:3 char:33
+ $m.PSBase.GetType().InvokeMember <<<< (
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Numer of worksheets: You cannot call a method on a null-valued expression. At C:\PS\excel.ps1:18 char:45 + $objWorksheet = $objWorkbook.Worksheets.Item <<<< (1) + CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Worksheet: You cannot call a method on a null-valued expression. At C:\PS\excel.ps1:21 char:37 + $Forename = $objWorksheet.Cells.Item <<<< (2,1).Text + CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression. At C:\PS\excel.ps1:22 char:36 + $Surname = $objWorksheet.Cells.Item <<<< (2,2).Text + CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Forename:
Surname:
这是我曾经问过的第一个问题,试着变得更好! :))
非常感谢
最高
答案 0 :(得分:2)
我发现OLE DB和Office 2007驱动程序更容易使用。如果您没有Office 2007,则可以下载drivers from MS。
$filepath = 'C:\Users\u00\documents\backupset.xlsx'
$connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=`"$filepath`";Extended Properties=`"Excel 12.0 Xml;HDR=YES`";"
#Connection String for Excel 2003:
#$connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=`"$filepath`";Extended Properties=`"Excel 8.0;HDR=Yes;IMEX=1`";"
$qry = 'select * from [sheet1$]'
$conn = New-Object System.Data.OleDb.OleDbConnection
$conn.ConnectionString = $connString
$conn.open()
$cmd = new-object System.Data.OleDb.OleDbCommand($qry,$conn)
$da = new-object System.Data.OleDb.OleDbDataAdapter($cmd)
$dt = new-object System.Data.dataTable
[void]$da.fill($dt)
$conn.close()
$dt
答案 1 :(得分:1)
感谢您的帮助......我已经解决了这个问题!我使用的是PowerShell V2 CTP2,现在我已经升级到CTP3,这已经修复了语言障碍问题,我现在可以运行脚本而不必担心调用功能,一切都很好!
非常感谢
最高