Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:49:32+00:00 2026-06-11T20:49:32+00:00

I am trying to install a service on a remote machine using the powershell.

  • 0

I am trying to install a service on a remote machine using the powershell.

So far I have the following:

Invoke-Command -ComputerName  $remoteComputerName -ScriptBlock {
         param($password=$password,$username=$username) 
         $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
         $credentials = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
         New-Service -Name "XXX" -BinaryPathName "c:\XXX.exe" -DisplayName "XXX XXX XXX" -Description "XXXXXX." -Credential $credentials -ErrorVariable errortext 
         Write-Host("Error in: " + $errortext)
        } -ArgumentList $password,$username -ErrorVariable errortext 


Write-Host("Error out: " + $errortext)

When there is an error while executing New-Service the $errortext ErrorVariable get set properly inside the ScriptBlock, because the text: “Error in: shows me the error.

The ErrorVariable of the Invoke-Command does not get set (which I expected).

My question is:

Is it somehow possible to set the ErrorVariable of the Invoke-Command to the error I got inside the ScriptBlock?

I know I could also use InstalUtil, WMI and SC to install the service, but this is not relevant at the moment.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:49:33+00:00Added an answer on June 11, 2026 at 8:49 pm

    No, you can’t get the Errorvariable from the Invoke-Command call to be set the same as in the scriptblock.

    But if your goal is “detect and handle errors in the scriptblock, and also get errors returned back to the context of the Invoke-Command caller” then just do it manually:

    $results = Invoke-Command -ComputerName server.contoso.com -ScriptBlock {
       try
       {
           New-Service -ErrorAction 1
       }
       catch
       {
           <log to file, do cleanup, etc>
           return $_
       }
       <do stuff that should only execute when there are no failures>
    }
    

    $results now contains the error information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to Install a windows service I made with the following command
When trying to install my 32bit service on a Windows 64bit machine my installer
I am trying to install a windows service using MSBuild and CCNET. I am
.NET 4.0, Windows 7, VS 2010: Trying to install 64-bit service using the Setup
I am trying to install a simple java application as a windows service using
I'm trying to install my Windows Service app using Visual Studio 2008 Setup and
I am trying to install a Windows service using InstallUtil.exe and am getting the
I am on win 7 and trying to install a .NET windows service using
I'm trying to install a service using InstallUtil.exe but invoked through Process.Start . Here's
I'm trying to install windows service using the Microsoft.Sdc.Tasks library. <ControlService Action=Install ServiceName=Service1 User=XXX

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.