繁体   English   中英

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

[英]Using newer PowerShell code on an old PowerShell version

抱歉,这是我的第一篇文章,我相信我会犯一些错误。 无论如何,我正在编写一个代码,该代码将从文件中检索计算机列表,然后从每台计算机获取用户和组,然后对于每台计算机,将保存两个文件,其中用户和组分别带有一个.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()
>

我在该代码中看不到任何Powershell版本特定的内容。 如果将错误编辑到问题中,将有帮助。 但我能想到的是,在没有安装Microsoft Word的服务器上; 您将需要实例化COM对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM