简体   繁体   中英

Using newer PowerShell code on an old PowerShell version

I apologize ahead of time this is my first post, I am sure I will make a few mistakes. Anyway, I am writing a code that will retrieve a list of computer from a file, then get the users and groups from each computer, then for each computer it will have two files saved with the users and groups with one .txt and one .pdf. This script works great on my windows 10 computer that I wrote the code on. But when I go to my virtual servers to test it out, they have problems with the PDF part of the code. I have two windows 2008-r2 and two 2012-r2 virtual machines. They can not run this part of the code and I am grateful for any help with this situation. This is the chunk of code that is for the 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()
}
>

These are the lines of code from above that came back with errors.

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

I don't see anything powershell version-specific in that code. It would help if edit the error into the question. But all I can think of is that on the servers you don't have Microsoft Word installed; which you will need to instantiate the COM object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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