在旧的PowerShell版本上使用较新的PowerShell代码

时间:2016-07-21 17:06:49

标签: powershell pdf powershell-v2.0 powershell-v3.0 powershell-v4.0

我提前道歉这是我的第一篇文章,我相信我会犯一些错误。无论如何,我正在编写一个代码,它将从一个文件中检索一个计算机列表,然后从每台计算机中获取用户和组,然后为每台计算机保存两个文件,用户和组保存一个.txt和一个。 PDF格式。这个脚本在我编写代码的Windows 10计算机上运行良好。但是,当我去我的虚拟服务器进行测试时,他们遇到了代码的PDF部分问题。我有两个Windows 2008-r2和两个2012-r2虚拟机。他们无法运行这部分代码,我很感激这种情况的任何帮助。这是PDF的代码块。

<
#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()
>

1 个答案:

答案 0 :(得分:2)

我在该代码中没有看到任何与PowerShell版本相关的内容。如果edit错误进入问题,这将有所帮助。但我能想到的是,在没有安装Microsoft Word的服务器上;您将需要实例化COM对象。