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是正确的。本周早些时候我制作了这些服务器,并忘记将其放在那里。 –

相关问题