3

I have been tasked with a job of creating a power shell script that can be run from the command prompt that will make use of Microsoft Word (2013) to open a specified word document and save it as a PDF document.

First time PowerShell user, but I was successful in creating a short script to do the job, as follows…

$word=new-object -comobject word.application
$doc=$word.documents.Open("c:\TEST.docx")
$doc.SaveAs([ref]"c:\test.pdf", [ref]17)
$doc.Close()
$word.Quit()

Which once saved as a ps1 file works well when run with the use of following command…

powershell.exe -ExecutionPolicy ByPass -file c:\SaveToPDF.ps1

However I have subsequently been asked too allow this script to be run on a remote computer. So I have started investigating this feature and have been able to create another power shell script that will run a command line job on a remote computer, as follows, that will run the help command

$Username = "Username"
$Password = "PWD123"
$ComputerName = "PC-RemoteApps"

$Script = {help}

$SecurePassWord = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Username, $SecurePassWord

$Session = New-PSSession -ComputerName $ComputerName -credential $Cred

$Job = Invoke-Command -Session $Session -Scriptblock $Script

echo $Job

Remove-PSSession -Session $Session

Which also works and the results of the help command are echoed back to the local PC. So the next step is to simply change the $Script variable to include the command I wish to run to action the conversion of the Word document to PDF, as follows…

$Script = {powershell.exe -ExecutionPolicy ByPass -file c:\SaveToPDF.ps1}

However, this generates an error (See Below) that does not make much sense seeing how the script works when run on the remote PC just fine…

You cannot call a method on a null-valued expression.

  • CategoryInfo : NotSpecified: (You cannot call...ued expression.:String) [], remoteException
  • FullyQualifiedErrorId : NativeCommandError

At C:\SaveToPDF.ps1:4 char:12

  • $doc.SaveAs <<<< ([ref]"c:\test.pdf", [ref]17)

    • CategoryInfo : InvalidOperation: (saveas:String) [], RuntimeException
    • FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.

At C:\SaveToPDF.ps1:5 char:11

  • $doc.Close <<<< ()

    • CategoryInfo : InvalidOperation: (Close:String) [], RuntimeException
    • FullyQualifiedErrorId : InvokeMethodOnNull

I have even tried to include the power shell script command directly into the remote processing script, as follows…

$Script = {

$word=new-object -comobject word.application
$doc=$word.documents.Open("c:\TEST.docx")
$doc.SaveAs([ref]"c:\test.pdf", [ref]17)
$doc.Close()
$word.Quit()

}

Where I is still get an error that basically tells me the same thing, as follows…

You cannot call a method on a null-valued expression.

  • CategoryInfo : InvalidOperation: (SaveAs:String) [], RuntimeException
  • FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.

  • CategoryInfo : InvalidOperation: (Close:String) [], RuntimeException
  • FullyQualifiedErrorId : InvokeMethodOnNull

It looks to me as though the $doc variable is not set to anything and therefore the SaveAs() and Close() methods cannot be called. Yet the source file ("c:\TEST.docx") exists in this location on the remote PC and the commands do run OK when run on the remote PC.

I have even moved the test files to network locations that both the Local and Remote PC have read write access too and I still get the same set of errors.

So at this point I have come to you guys to see if you can let me know what I am missing.

Hope you can help

Thanks

Carl

3
  • Can you check if your $doc variable is getting filled by the open call. Since we are working in a remote session, I have my doubts that Word can work correctly without a user session. Ususally when we want to do office related operations over computer boundaries we use a library like ASPose or similar to avoid needing to install Word or excell everywhere... Jun 5, 2014 at 11:12
  • Yes the $doc variable is null when run remotely, which is why I think it is throwing errors. Jun 5, 2014 at 13:02
  • Can you please elaborate on what you mean by "without a user session", as the session that the script creates uses a valid users credentials etc. or is there something more I should include in the script to open a user session? Jun 5, 2014 at 13:05

1 Answer 1

0

Unfortunately you cannot create comobject word.application using Invoke-Command or Enter-PSSession. Word or any other Office product rely on a GUI environment in which to launch their binaries.

I would really like to back this up with a citation but alas I have nothing..

1
  • The script does indeed open Word, which is simply enough to determine with the use of task manager on the remote PC, but it is not behaving right, and actually just keeps consuming more and more memory until the remove PC crashes. Which supports the need for office to work as expected in a GUI environment. Unless any one has another idea i am about ready to give up on this now... Jun 9, 2014 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.