script on remote machine - powershell

I have a VBScript file that I would like to run on a remote machine using invoke:
Invoke-Command -ScriptBlock { cscript.exe "C:\test.vbs" } -Computer Machine1
When I execute this command, nothing happens. Neither on the remote machine nor in the shell window on my local machine; the only lines that appear in my local shell window after executing are:
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
But it stays like this and I am not able to do anything anymore with the shell. I also dont get any error msg. The VBScript itself is quite small and takes just a few seconds to run. Also I did try and run the VBScript on the remote host directly (in remote desktop) and it is working.
Any clue where I could get some details on why the remote call doesn't do anything?

Related

Programs running on Hyper-V with Invoke-Command hang

I'm trying to run my software on Hyper-V VM using powershell Invoke-Command, without success. Host OS -Windows 10. Guest OS - also Windows 10. VM Generation 1.
I did some simple experiments and found out this:
If I run
Invoke-Command -VMName MY_VM -Credential $Cred -ScriptBlock { calc }
then I can see launched calc.exe on the guest system right with UI.
But if I run mspaint or any non-Microsoft program, nothing happens. The program just hangs in the VM TaskManager without any effect.
I also tried to run several different programs using CLI calling Invoke-Command several ways, but got the same result.
What could be the problem?
The basic answer is that powershell remote connections (or any remote connection like rdp, ssh, etc) take place in a separate logon session, and can't really interact with each other.
There are two reasonable ways to do this:
Use PsExec - part of the microsoft sysinternals tools group.
# List sessions - note the session ID of the session you want the process to start in
quser /server:$computername
# Run a process remotely, specifying the logon ID
PsExec.exe -s -i $ID notepad.exe
Use a scheduled task that runs when you are logged in and is visible. You can do this with powershell's various New-ScheduledTask commands to create one, or follow this guide by Scripting Guy! using WMI Win32_ScheduledJob methods.
See use powershell to start a gui program on a remote machine for more details on both options, and a well-written description of why it's hard to do in windows.

Executing powershell script from linux to windows machine

We would like to execute powershell scripts which are located at windows share drive eg: winvmx01\folder1\folder2\script.ps1 from linux machine.
We installed powershell in linux machine and running Invoke-Command -Computername winvmx01 -ScriptBlock { Get-ChildItem "C:\Program Files" }
but it cant able to find commands of powershell and posting an error /bin/bash: line 69: Invoke-Command: command not found.
when we tried to check version it is showing up fine
pwsh
PowerShell v6.0.4
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.`
can someone please suggest how we can proceed. if not powershell is there any other client that we can use to execute a script in windows shared machine.
Thanks!

Not able to execute the Power shell script from C# with Invoke-Command on a remote machine

I am not able to execute the Powershell file script from C# with Invoke-Command on a remote machine but at the same time I am able to execute the same script file using Invoke-Command on a remote machine through the PowerShell Command window with out any issue.
My remote machine as well as local machine has been enabled for Remoting by executing the call Enable-PsRemoting on both the machines. I also set the ExecutionPolicy on both the machines as Unrestricted.
While executing the script I get the following error/execption:
File G:\x.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
I cannot understand what is the issue here.
Is this machine running a 64-bit OS? If it is, there will be both 32 and 64 bit PowerShell environments, and each has it's own execution policy. Depending on how you're invoking the script you may not be running in the same environment and execution policy as what you're using interactively in the command window.

Running power shell on a remote VM

Say I have a got a virtual machine VM1 with windows server 2008 installed on it. So it probably has PowerShell 2.0 installed on it but I can install PowerShell version 3.0 if needed to solve this problem. Now say my machine is Windows 7 64 bit with SP 1, I want to execute a PowerShell script on my machine that opens the PowerShell (not literally opening the window) on VM1 and executes some commands on the PowerShell of that VM1. And I do have administrative login credentials for that VM1 and my machine (both).
How can something like this be achieved ?
Thanks
It will work the same on version 2 & version 3, but you must have PSRemoting enabled. Once that's done, you can use Enter-PSSession to access the remote system interactively (similar to sshing into a Linux box) or Invoke-Command to execute individual commands or scriptblocks remotely.

Installing an .exe on a remote machine from PowerShell

So, I have been trying to do the following via a PowerShell script:
For a list of computers, do:
Ping the computer (via WMI) to see if it's available; if not, log & break, if so, continue on
Create a folder on the root of the C:\ drive (via Invoke-WmiMethod); if fails, log & break, if successful, continue on
Copy files (includes an .exe) from another machine into that folder; if fails, log & break, if successful, continue on
Run the .exe file (via Invoke-WmiMethod); if fails, log & break, if successful, log success, done (with this computer.)
The problem I'm running into is the execution of the .exe (program installer) -- the Invoke-WmiMethod command usually works, but for some machines, it hangs (not fails, but hangs.) I've tried a whole bunch of stuff to try to get it to run as a job so I can set a timeout on the install (running the Invoke-WmiMethod command with -AsJob param, always returns Failed...; Start-Job -Computer $compname { Invoke-WmiMethod..., returns Completed but the install never happens; making sure the remote machines have Windows Firewall disabled, UAC turned off, etc. but still if I run the Invoke-WmiMethod command on them, not running as a job, it hangs. And yes, I'm running PS as a Domain Admin, so I should have rights on the target machines.)
So being a newb at all things PowerShell, I'm now at a complete loss as to what to try next... How would you tackle running a .exe on a remote system from a PowerShell script? One caveat is that the target machines don't all run PowerShell [V1|V2] (target PCs are a mix of XP, Vista and 7) or don't have remoting enabled. The other caveat being that the installer is an .exe, and not an .msi, and this can't be changed (it's a third-party app.)
Thanks in advance to anyone who can point me in the right direction here (and give me some sample code...)
What OS is running on the system (management station or central system) where these scripts are getting executed? If Windows XP, there is a known issue with WMI and -asJob.
Check this: WMI Query Script as a Job
In such a case, I'd suggest moving to a Windows 7 system and then run the script to remotely install .exe on all other machines.