2013-04-30 50 views
-1

試圖讓我的腳本工作,需要一些幫助在這裏是我的代碼。我需要幫助找到PowerShell腳本中的錯誤

#excel 
#open ap 
$XL = new-object -com "Excel.Application" 
$XLbooks = $XL.workbooks 
$netci = [system.Globalization.CompareInfo]"en-us" 
$wkbk = $XLbooks.PSBase.GetType().Invokemember("Add",[Reflection.BindingFlags]::InvokeMethod,$null,$XLbooks,$null,$newci) 
$sheet = $XLbooks.worksheets.item(1) 
$sheet.name = "name" 
$sheet.cells.item($row,1).formulalocal = "Fred Nurk" 
$file = "c\windows\scripts\test.xlsx" 
[void]$wkbk.PSBase.GetType().InvokeMember("SaveAs",[Reflection.BindingFlags]::InvokeMethod,$null,$wkbk,$file,$newci) 
("Close",[Reflection.BindingFlags]::Invokemedthod,$null,$wkbk,0,$newci) 
$XL.quit() 

錯誤

Cannot convert the "en-us" value of type "System.String" to type "System.Globalization.CompareInfo". 
At C:\scripts\test.ps1:5 char:44 
+ $netci = [system.Globalization.CompareInfo] <<<< "en-us" 
    + CategoryInfo   : NotSpecified: (:) [], RuntimeException 
    + FullyQualifiedErrorId : RuntimeException 

You cannot call a method on a null-valued expression. 
At C:\scripts\test.ps1:7 char:34 
+ $sheet = $XLbooks.worksheets.item <<<< (1) 
    + CategoryInfo   : InvalidOperation: (item:String) [], RuntimeException 
    + FullyQualifiedErrorId : InvokeMethodOnNull 

Property 'name' cannot be found on this object; make sure it exists and is settable. 
At C:\scripts\test.ps1:8 char:8 
+ $sheet. <<<< name = "name" 
    + CategoryInfo   : InvalidOperation: (name:String) [], RuntimeException 
    + FullyQualifiedErrorId : PropertyNotFound 

You cannot call a method on a null-valued expression. 
At C:\scripts\test.ps1:9 char:18 
+ $sheet.cells.item <<<< ($row,1).formulalocal = "Fred Nurk" 
    + CategoryInfo   : InvalidOperation: (item:String) [], RuntimeException 
    + FullyQualifiedErrorId : InvokeMethodOnNull 

Exception calling "InvokeMember" with "6" argument(s): "Microsoft Excel cannot access the file 'C:\Users\Jared\Document 
s\c\windows\scripts\5ADD7000'. There are several possible reasons: 
The file name or path does not exist. 
The file is being used by another program. 
The workbook you are trying to save has the same name as a currently open workbook." 
At C:\scripts\test.ps1:11 char:42 
+ [void]$wkbk.PSBase.GetType().InvokeMember <<<< ("SaveAs",[Reflection.BindingFlags]::InvokeMethod,$null,$wkbk,$file,$n 
ewci) 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : DotNetMethodTargetInvocation 
+0

它應該做什麼? – ash 2013-04-30 04:23:35

+0

在Excel中創建電子表格和數據。 – mjared 2013-04-30 04:24:58

+0

「它不起作用」有點模糊。你有沒有試過調試器?怎麼了? – ash 2013-04-30 04:27:47

回答

4

你需要解決的是創建CompareInfo的主要問題。第一個錯誤告訴你此行不工作:

$netci = [system.Globalization.CompareInfo]"en-us" 

所以,你需要做的是創建CompareInfo對象是這樣的:

$netci = ([system.Globalization.CultureInfo]"en-us").CompareInfo 

雖然不是使用這個瘋狂的方式創建一個工作簿:

$wkbk = $XLbooks.PSBase.GetType().Invokemember("Add",[Reflection.BindingFlags]::InvokeMethod,$null,$XLbooks,$null,$newci) 

...嘗試,而不是該更理智的方式:d

$wkbk = $XL.workbooks.Add() 

如果你這樣做,你不必擔心創建CompareInfo對象。

0

跳出來的問題是$netci = [system.Globalization.CompareInfo]"en-us"是無效的語法。你沒有調用System.Globalization.CompareInfo類的任何方法,你只是在它後面放置一個字符串。 PowerShell將[System.Globalization.CompareInfo]解釋爲類型轉換運算符,並且抱怨字符串「en-us」無法轉換爲數據類型System.Globalization.CompareInfo - 因爲該數據類型不存在。

您需要調用在「en-us」上運行的方法。您可以從MSDN獲得的方法列表:

http://msdn.microsoft.com/en-us/library/system.globalization.compareinfo.aspx

假設你想用的方法GetCompareInfo(似乎最有可能,我 - 它返回一個CompareInfo對象)時,你會寫這行是這樣的:

$netci = [System.Globalization.CompareInfo]::GetCompareInfo('en-us') 

注意,順便說一句,你有這樣的變量作爲$netci當您創建它,但作爲腳本的其餘部分$newci

我沒有看太深的其他錯誤,但他們可能是由於未能正確創建$ newci而產生的級聯效應,所以我懷疑如果你修復這個錯誤,其他錯誤將會消失。