在旧的PowerShell版本上使用更新的PowerShell代码
我提前道歉这是我的第一篇帖子,我相信我会犯一些错误。无论如何,我正在编写一个代码,它将从一个文件中检索一个计算机列表,然后从每台计算机中获取用户和组,然后对于每台计算机,它将有两个与用户和组一起保存的文件,一个是.txt,一个是.pdf。这个脚本在我编写代码的windows 10计算机上非常有效。但当我去虚拟服务器进行测试时,他们对代码的PDF部分有问题。我有两台windows 2008-r2和两台2012-r2虚拟机。他们无法运行这部分代码,我非常感谢在这种情况下提供的任何帮助。这是用于PDF的代码块在旧的PowerShell版本上使用更新的PowerShell代码,powershell,pdf,powershell-2.0,powershell-3.0,powershell-4.0,Powershell,Pdf,Powershell 2.0,Powershell 3.0,Powershell 4.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()
}
>
<
#制作pdf
#必需的字变量
$wdExportFormatPDF=17
$wdDoNotSaveChanges=0
#创建一个隐藏的单词窗口
$word=新对象-ComObject word.application
$word.visible=$false
#添加Word文档
$doc=$word.documents.add()
#将文本放入Word文档中
$txt=获取内容$txtPath
$selection=$word.selection
foreach($txt中的行){
$selection.typeText($line)|格式宽度
$selection.type段落()
}
#导出PDF文件并关闭而不保存Word文档
$doc.ExportAsFixedFormat($pdfPath,$wdExportFormatPDF)
如果($?){write host'用户和组保存到“$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()
>
<
#新对象:检索CLSID为{00000000-0000-0000-0000-0000-000000000000}的组件的COM类工厂失败,原因是以下错误:80040154类未注册(HRESULT异常:0x80040154(REGDB_E_CLASSNOTREG))。
新对象-ComObject word.application
#在此对象上找不到属性“visible”。验证该属性是否存在并可以设置。
$word.visible=$false
#不能对空值表达式调用方法。
$doc=$word.documents.add()
#不能对空值表达式调用方法。
$selection.typeText($line)|格式宽度
#不能对空值表达式调用方法。
$selection.type段落()
#不能对空值表达式调用方法。
$doc.ExportAsFixedFormat($pdfPath,$wdExportFormatPDF)
#不能对空值表达式调用方法。
$doc.close([ref]$wdDoNotSaveChanges)
#不能对空值表达式调用方法。
$word.Quit()
>
我在代码中没有看到任何特定于powershell版本的内容。如果把这个错误引入到这个问题中去,会有帮助的。但我所能想到的是,在服务器上没有安装MicrosoftWord;您需要实例化COM对象。非常感谢。我相信您对Microsoft Word的理解是正确的。本周早些时候我做了这些服务器,但忘了把它放在那里。