Run a Powershell command remotely in the background

I am attempting to connect to a Windows machine using SSH and run a command in the background. When I directly run the following command from the terminal after establishing an SSH connection, it works:

start PowerShell.exe -ExecutionPolicy Unrestricted Invoke-Command -ScriptBlock{"cd C:\Users\myuser\AppData\Roaming\npm\node_modules\myserver\; npm start > log.log 2>&1"}

However, when I try to execute the same command remotely using SSH, it does not work as expected:

ssh myuser@machine_ip "start PowerShell.exe -ExecutionPolicy Unrestricted Invoke-Command -ScriptBlock{\"cd C:\Users\myuser\AppData\Roaming\npm\node_modules\myserver\; npm start > log.log 2>&1\"}"

If I remove the 'start' keyword from the second command, it runs but retains control of the terminal. It is not necessary to use PowerShell, so if anyone knows how to run the following command in the background, please let me know:

cd C:\Users\myuser\AppData\Roaming\npm\node_modules\myserver\; npm start > log.log 2>&1

Thank you.

Answer №1

Unfortunately, I am unable to test out this particular snippet of code. However, it seems that Invoke-Command includes a useful command called -asjob.

ssh myuser@machine_ip "start PowerShell.exe -ExecutionPolicy Unrestricted Invoke-Command -ScriptBlock{\"cd C:\Users\myuser\AppData\Roaming\npm\node_modules\myserver\; npm start > log.log 2>&1\"}" -AsJob

If you'd like more information, check out the Microsoft documentation here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-5.1#example-8--run-a-background-job-on-several-remote-computers

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble installing npm package in Angular project

After cloning my project from GitLab, I encountered an issue while trying to install the NPM packages. When I ran npm install, an error popped up: https://i.stack.imgur.com/WNT5s.png Upon checking the log file, I found the following error message: 3060 ...

Launch the dev server in React

Begin by cloning the git code in React. Next, make sure to install npm package manager. Encountered Problem: I am facing an issue with running the dev-server. I have tried the following command in the cmd prompt: 1. npm run dev-server "Here is a sample ...

NPM installation stalls only when attempting to install the specific package, ts-jest

https://i.stack.imgur.com/Cvon1.png I've encountered an issue while trying to set up ts-jest in a new project. Here's what I've tried: $ mkdir test && cd test $ npm init -y $ npm install ts-jest Despite being able to install other ...

Unable to execute the ionic start command

Attempting to kick off a fresh project with ionic start myApp blank while opting for React. Following the enter key press, the ensuing output materialized. ⠦ Downloading and extracting blank starter (100.00%) events.js:174 throw er; // Unhandled ‘erro ...

Accessing global modules path in node scripts is a challenge in node.js

I am setting up a WordPress website with custom mu-plugins and a custom theme, all of which have gulp build processes and rely on the same npm packages. To streamline my workflow, I decided to install these npm packages globally so that a root-level node s ...

Creating a versatile JavaScript library that is compatible with various platforms such as browsers, NodeJS, and single page applications like React

My question is: Is it possible to convert a basic file into a versatile library that can be utilized with Browsers (via script tag), Node JS, and Single Page Applications using a single codebase? In the past, I have primarily used existing libraries witho ...

What steps can be taken to resolve the vulnerability in webpack-pwa-manifest?

Looking for solutions to address the [email protected] and minimist vulnerability. I attempted removing node/modules and package-lock.json, followed by a fresh npm installation, but the issue persists. Any suggestions would be greatly appreciated. Scr ...

Should I use `npm install bootstrap` or manually download the source code

I'm currently experiencing some issues while trying to use Bootstrap's build tools. Specifically, I've been facing difficulties when using npm install bootstrap to set it up. Interestingly, the problem seems to disappear when I opt to manual ...

Are you curious about the array of elements in React's carousel?

I'm currently in the process of constructing a website using React, and I have a specific challenge related to the "news" section. Within this section, I have a list of three components that represent different news items. These components are housed ...

Merge two JSON objects together by matching their keys and maintaining the original values

After having two separate JSON objects representing data, I found myself in the position of needing to merge them into one combined result. The initial objects were as follows: passObject = { 'Topic One' : 4, 'Topic Two' : 10, &apos ...

What is the best way to close the terminal when running an NPM script in the package.json file on a Windows system?

Within my package.json, I have set up scripts that open in new terminals specifically for Windows OS: "automation": "start npm run webdriver:start && npm run timeout" "webdriver:start": "webdriver-manager start", "timeout": "timeout 3", Now, the ...

Exploring Node troubleshooting with WebPack and Feathers

Currently, I am part of a team working on a project that involves using Node, Webpack, TypeScript, and Express/Feathers. While my fellow developers are experienced in these technologies, I have limited experience with JavaScript mainly on the client-side a ...

Encountering a Firebase deployment problem: Issue with build process: npm error! Call stack size surpasses the

I am encountering an issue while trying to deploy my Firebase Cloud Function using the following command: firebase deploy --only functions:helloWorld Although the command succeeds occasionally, most of the time it fails with the error mentioned above. Wha ...

Issues with Docker port publishing on Windows 10

Recently, I've been diving into the world of Docker and ran into an issue with port publishing from a docker container to the host machine. To start off, I built an image for a simple React app (using Vite) by running the command docker build -t reac ...

Setting up JavaScript imports in Next.js may seem tricky at first, but with

Whenever I run the command npx create-next-app, a prompt appears asking me to specify an import alias. This question includes options such as ( * / ** ) that I find confusing. My preference is to use standard ES6 import statements, like this: import Nav f ...

Is it possible to install the lib ldap-client module in node.js?

I'm having trouble installing the lib ldap-client package in Node.js. To try and solve this issue, I consulted the following page: https://github.com/nodejs/node-gyp. In an attempt to fix the problem, I have installed python, node-gyp, and Visual St ...

Avoiding the installation of dependencies within npm packages, particularly in projects utilizing Next.js, Node

I've encountered a puzzling issue that seems to have no solution online. When I install npm packages, their dependencies are not automatically installed. This situation is resulting in numerous warnings appearing in the console, such as: Module not ...

A guide to integrating Material-UI with your Meteor/React application

I encountered an issue while trying to implement the LeftNav Menu from the Material-UI example. The error message I received is as follows: While building for web.browser: imports/ui/App.jsx:14:2: /imports/ui/App.jsx: Missing class properties transf ...

Solving the issue of 'sh: tsc not found' error in a docker container within Gitlab CI's docker-in-docker environment

My current project involves setting up Gitlab CI Docker-in-Docker. However, I'm facing a challenge as the job keeps failing due to the inability to locate installed NPM packages when executing commands. The error message reads: backend_1 | backend ...

The attempt to install eas-cli globally using the npm command was unsuccessful due to an Invalid

When attempting to execute npm install --global eas-cli, I encountered an error with the message TypeError [ERR_INVALID_URL]: Invalid URL. Here is the context: ➜ ~ node -v v16.20.2 ➜ ~ npm -v 8.19.4 ➜ ~ npm config get proxy null ➜ ~ npm config ...