在旧的 PowerShell 版本上使用更新的 PowerShell 代码
Using newer PowerShell code on an old PowerShell version
我提前道歉,这是我的第一次 post,我相信我会犯一些错误。无论如何,我正在编写一个代码,它将从一个文件中检索计算机列表,然后从每台计算机中获取用户和组,然后对于每台计算机,它将保存两个文件,其中包含一个 .txt 和一个 .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 版本的内容。如果 edit 错误进入问题,这将有所帮助。但我能想到的是,在服务器上你没有安装 Microsoft Word;您需要实例化 COM 对象。
我提前道歉,这是我的第一次 post,我相信我会犯一些错误。无论如何,我正在编写一个代码,它将从一个文件中检索计算机列表,然后从每台计算机中获取用户和组,然后对于每台计算机,它将保存两个文件,其中包含一个 .txt 和一个 .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 版本的内容。如果 edit 错误进入问题,这将有所帮助。但我能想到的是,在服务器上你没有安装 Microsoft Word;您需要实例化 COM 对象。