Steps:
1. I have created a file known as: "test.vbs" under temp folder in machine B and in that script i have msgbox "hi"
2. From machine A - my goal is execute to test.vbs in machine B
3. In machine A. i did the following steps but i didnt get remote script executed. Can anyone please help me.
ps c:/> $s=new -pssession -computername machineB
ps c:/> Invoke-Command -Session $s-ScriptBlock{$filecount=(Get-ChildItem c:\temp\test.vbs -Recurse)}
ps c:/> Invoke-Command -Session $s-ScriptBlock{$filecount}
i received directory c:\temp and a file name as test.vbs
Now i want to execute test.vbs
ps c:/> Invoke-Command -ComputerName MachineB{& "C:\Temp\test.vbs"}
After executing the above command i dont see the test.vbs not executing remotely.
my expectation is to get hi messagebox.
I am not understanding if it got executed or not.
can anyone please help me to execute remote script locally using power shell.
thanks inadvance.
If my understanding to your question is correct, you want to execute test.vbs against machine B from machine A, I don't think it's possible in PowerShell.
I faced the same issue some time ago. Neither Enter-PsSession nor Invoke-Command can do it for you probably just because they are not designed for this purpose. They are working totally fine if you run any PowerShell cmdlets remotely e.g. Get-Item you can easily get items on machine B. But once if you try to do something beyond the PowerShell remote session, like run a vbs script which will start a new CScript.exe process, it just won't work. It's due to some reason in underlying implementation of this PowerShell remoting.
If you do need to do that, I got two workarounds
PsExec can be the best solution. It was designed to do this kind of work originally and it can easily accomplish what you want with a bunch of advanced features.
Create a scheduled task on machine B by using New-ScheduledTask and let it trigger itself on machine B locally.
If you have SCCM installed, it also provides this kind of functionality, check out this blog.
Related
I have a PowerShell script ex1.ps1 which takes user inputs and ex1.ps1 has commands to open a new PowerShell to execute an exe file:
Start-Process -FilePath "$PSHOME\powershell.exe" -ArgumentList "-command C:\APPLICATION1.exe`
I want to execute ex1.ps1 on a remote host. I am trying to call ex1.ps1 using Ansible-playbook as:
# ansible-playbook script
- name: Run basic PowerShell script
win_powershell:
script: |
powershell.exe -ExecutionPolicy ByPass -File C:/Users/ex1.ps1
It is executing fine but in remote host there is no PowerShell prompt open to get the inputs.
You shouldn't be expecting manual input when deploying with a tool like Ansible. I don't know the actual program you are trying to run but the best solution here is to figure out the required parameters for the program to install/start/run without user interaction.
If you are able to provide the name of the program (and it's not something internal to your organization) a more complete answer may be able to be provided.
Unrelated to your question, unless you've over-generalized your code for this question there isn't a reason to call powershell.exe from within PowerShell just to run an executable. You can either use Start-Process or the call operator & directly with the exe path in question.
I have an answer here that goes over Start-Process and the usage of & in a bit more detail.
I'm using a raspberry pi to shut down my Windows 10 Computer.
My RPI uses SSH with a private key to execute a shutdown /s /t 30 command on my computer.
But, for optimization reasons, I want to use the shutdown /sg command; by doing so my applications will open automatically before I unlock my session.
However, shutdown /sg needs to be executed on a Desktop Session.
How I can execute a script on my desktop from command communicated over SSH?
Thanks for your help.
I tried to use Invoke-Command but Powershell is not my cup of tea.
But, I find a solution :
I created a Task (named ShutdownSG_over_SSH_from_RPI) in Task Manager which execute shutdown \sg command.
From my RPI, I execute this command over SSH :
powershell -command "Start-ScheduledTask -TaskPath '\MyTaskPath\' -TaskName 'ShutdownSG_over_SSH_from_RPI'"
I know it isn't the best solution but it works.
If you have PowerShell 6.0, support for SSH connection has been added to New-PSSession, Enter-PSSession and Invoke-Command
This requires software to be installed on both your Raspberry PI and your computer.
Microsoft have provided a nice article explaining how to achieve this:
https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/ssh-remoting-in-powershell-core?view=powershell-6
You should also look at the documentation for Invoke-Command, as that's the easiest way to execute commands and scripts remotely:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-6
Current PS script:
Invoke-Command -ComputerName RemoteServer007.FQDN.com -ScriptBlock {
Set-Variable -Name WOWCONFIG -value "d:\ABCs\WOWzers" `
| Start-Process "d:\da-folder\Do-It-NOW-Pleez.cmd"
}
If I log on locally to the server(RemoteServer007.FQDN.com) and execute the cmd file, it runs through all of the lines(commands) within the cmd file.
When I execute it remotely, it gets about 30% of the way through the commands within the cmd file, the PS execution ends without error, but not all of the lines/commands in the cmd file had been executed.
This was discovered by simply configuring each line of the cmd file to output to txt files.
I even tried re-ranging the commands in the cmd file, thinking that perhaps there was a specific command that was causing it to exit, but that is not the case.
I'm wondering if there is some timeout or response that PowerShell is not getting? and just quitting almost immediately after starting?
Any ideas or help would be greatly appreciated.
There are a couple of things you can do here:
You may have a memory issue. Increasing the value of MaxMemoryPerShellMB might help
set-item WSMan:\$target\Shell\MaxMemoryPerShellMB -Value 0 -Force
You'd need to run this once on the remote machine before you execute your commands again.
You can also see possible error logs in the windows event viewer. There are categories for powershell and for Windows Remote Management which you should look at.
Finally, you can just run this process asynchronously, using the task scheduler for instance. I had a similar problem with windows in the past, and running the process from the task scheduler, outside the powershell session, fixed it. There's an example of how we did this in Cloudify here:
https://github.com/CloudifySource/cloudify/blob/master/esc/src/main/resources/clouds/ec2-win/upload/bootstrap-management.ps1#L220
So i have fairly easy powershell script that contains following:
import-module activedirectory
Get-ADUser -Filter *
remove-module activedirectory
If i run it from powershell it runs OK, but when i try to call it from CMD nothing happens, it just opens powershell and thats it. I am using following command to run it:
powershell.exe -file "D:\test.ps1"
I noticed also following thing, 2 powershell.exe processes run after i execute this. If i exit from CMD from one powershell then i start seeing lists that this PS query should be returning. Is there a way to get this working since i am trying to run ps script as scheduled job. The crucial part here is import module when i run it over cmd which is not happening for some reason.
It's powershell 2.0 running on Windows 2008R2. I tried this script on win 2012r2, works fine from CMD... Looks like ps 2.0 limitation?
Could be a couple of things going on here. Since your windows opens and closes you wont get to see any errors that might be occurring. What is your ExecutionPolicy set to? Get-ExecutionPolicy
When I make scheduled tasks of my scripts I usually set up my action as such
Program/Script = %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
Arguments = -ExecutionPolicy Unrestricted -NoProfile -File C:\data\script.ps1
Start In = %SystemRoot%\system32\WindowsPowerShell\v1.0
Also, I don't believe it matters in this case but be sure you have the script "Running with highest privilege" if required.
I've been playing around with powershell remoting and I've come across a problem that I am unable to resolve. I have a script that creates a remote session and after setting up some variables does the following:
Invoke-Command -Session $remote_session -ScriptBlock $block -ArgumentList $args
Within the block everything works fine except when it comes to a specific line, at which point the script hangs. The pertinent line is:
& '.\external_command.exe' $argument_list
When I log into the computer that is hosting the remote powershell session I can see external_command.exe in the process list but it's not doing anything. Does anyone have any ideas on how to resolve the issue?
I had similar problems with remote execution of installers. As a workaround I now first create a cmd batch file through powershell and then run this batch file. Something like this:
"c:\external_command.exe argument1" > c:\run.cmd
&"c:\Run.cmd" |Out-Null