1

我提前道歉這是我的第一篇文章,我相信我會犯一些錯誤。無論如何,我正在編寫一個代碼,它將從文件中檢索計算機列表,然後從每臺計算機獲取用戶和組,然後爲每臺計算機分配兩個文件,其中包含一個.txt和一個用戶和組。 PDF格式。這個腳本在我編寫代碼的Windows 10電腦上效果很好。但是當我去我的虛擬服務器進行測試時,他們在代碼的PDF部分存在問題。我有兩個windows 2008-r2和兩個2012-r2虛擬機。他們無法運行這部分代碼,我很感激任何有關這種情況的幫助。這是用於PDF的代碼塊。在舊的PowerShell版本上使用較新的PowerShell代碼

< 
#make pdf 

# Required Word Variables 
$wdExportFormatPDF = 17 
$wdDoNotSaveChanges = 0 

# Create a hidden Word window 
$word = New-Object -ComObject word.application 
$word.visible = $false 

# Add a Word document 
$doc = $word.documents.add() 

# Put the text into the Word document 
$txt = Get-Content $txtPath 
$selection = $word.selection 
foreach($line in $txt){ 
$selection.typeText($line) | Format-wide 
$selection.typeparagraph() 
} 
# Export the PDF file and close without saving a Word document 
$doc.ExportAsFixedFormat($pdfPath,$wdExportFormatPDF) 
if($?){write-host 'Users and Groups saved to ' $pdfPath -ForegroundColor Cyan} 
$doc.close([ref]$wdDoNotSaveChanges) 
$word.Quit() 
} 
> 

這些是從上面返回錯誤的代碼行。

< 
#New-Object : Retrieving the COM class factory for component with CLSID{00000000-0000-0000-0000-000000000000} failed due to the following error:80040154 Class not registered (Exception from HRESULT: 0x80040154(REGDB_E_CLASSNOTREG)). 
New-Object -ComObject word.application 

#The property 'visible' cannot be found on this object. Verify that the property exists and can be set. 
$word.visible = $false 

#You cannot call a method on a null-valued expression. 
$doc = $word.documents.add() 


#You cannot call a method on a null-valued expression. 
$selection.typeText($line) | Format-wide 


#You cannot call a method on a null-valued expression. 
$selection.typeparagraph() 

#You cannot call a method on a null-valued expression. 
$doc.ExportAsFixedFormat($pdfPath,$wdExportFormatPDF) 

#You cannot call a method on a null-valued expression. 
$doc.close([ref]$wdDoNotSaveChanges) 

#You cannot call a method on a null-valued expression. 
$word.Quit() 
> 

回答

2

我沒有在該代碼中看到任何powershell特定版本。這將有助於如果edit的錯誤成爲問題。但我能想到的是,在服務器上沒有安裝Microsoft Word;您將需要實例化COM對象。

+0

非常感謝。我相信你對微軟Word是正確的。本週早些時候我製作了這些服務器,並忘記將其放在那裏。 –

相關問題