Trigger external remote script - powershell

I have a powershell script located on a different server, and I want to trigger this script from the build server(TFS 2018) using a network path.
//MyServer/MyScripts/run.ps1
In the Powershell build step I need to specify script path under TFS source path. Is there a way to trigger the external script?
This is what I've tried without success....
Invoke-Command -ComputerName MyServer -ScriptBlock {Get-Process -Name //MyServer/MyScripts/run.ps1} -ArgumentList 'arg1', 'arg2'

Something like this:
Invoke-Command -Session $session -scriptblock {Pushd $rootFolder
.\$scriptname.ps1}
Or you could Invoke-Command -FilePath
Depends on where you want to run it.

Related

How to run a .bat file in a VM from Powershell

I am trying to parallelize an operation by running .bat files in several remote VMs simultaneously using a Powershell script from a local VM. I can successfully run the .bat file in the local VM by using the following:
Start-Process cmd.exe -ArgumentList $batOptimization_path, $vmNumberParam, $rInstanceParam, $optDataParam, $dataSetParam
The first param in -ArgumentList is the path where the .bat file is located, the rest are command line parameters for the .bat file. To try to run the .bat file on a VM, I am using the following:
$psSession = New-PSSession -ComputerName $script:FsvRConfig.VmList[$i].IpAddress
Invoke-Command -ArgumentList $batOptimization_path, $vmNumberParam, $rInstanceParam, $optDataParam, $dataSetParam `
-Session $psSession `
-ScriptBlock {Start-Job -ScriptBlock { Start-Process cmd.exe -ArgumentList $args[0], $args[1], $args[2], $args[3] -AsJob}} `
-ErrorAction SilentlyContinue
This does not run the .bat files properly. The jobs start and then fail, so obviously this is not the way to launch the jobs. Any advice on getting the .bat files to launch in the remote VMs the way it does in the local VM would be greatly appreciated!

Using PowerShell to execute a remote script that calls a batch file

I am having an issue with running a batch file that is located on a remote server.
I have a batch file located on a remote server that i want to run that kicks off an automated Selenium test. For simplicity, let's say the name of my batch file is mybatch.bat
I have the following code in a Powershell script located on the server:
$BatchFile = "mybatch.bat"
Start-Process -FilePath $BatchFile -Wait -Verb RunAs
If I run this PowerShell script locally on the server in ISE then it runs fine and it kicks off the selenium test which takes a couple minutes to run.
Now I want to try to execute this test from another machine by using PowerShell remoting. Let's assume that remoting is already configured on the servers.
I have a PowerShell script located on another server which has the following code segment. Assume that all of the session variables have the correct information set:
$CMD = "D:\mybatch.bat"
$TargetSession = New-PSSession -ComputerName $FullComputerName -Credential $myCreds -ConfigurationName RemoteExecution
$command = "powershell.exe -File $CMD -Wait"
Invoke-Command -Session $TargetSession -ScriptBlock { $command }
When this script runs, it does connect to the remote machine and create a remote session. It does look like it kicks off the batch file because PowerShell does not give me an error. But it does not wait for the full 3 or 4 minutes for the Selenium test to finish. It seems like it just times out. Also if I am logged onto the other machine, I don't see any Selenium web test running. No Selenium log files or results files are created on remote server as should be expected.
I was wondering what I could be doing wrong with my code.
Also, it seems that the server always returns the echo outputs of the batch file to my local machine. I see these random blinking white screen on ISE which looks like output from the batch file
$command = "powershell.exe -File $CMD -Wait"
Invoke-Command -Session $TargetSession -ScriptBlock { $command }
There are 2 issues with the above code:
$command inside the scriptblock and $command outside the scriptblock are different variables due to different scopes. The variable inside the scriptblock is thus undefined and the scriptblock will simply echo an emtpy value.
Even if $command weren't undefined, the scriptblock would still just echo its value, since it's defined as a string. PowerShell does not execute strings unless you're using something like Invoke-Expression (which you shouldn't).
This should do what you want:
$CMD = "D:\mybatch.bat"
$TargetSession = New-PSSession -ComputerName $FullComputerName -Credential $myCreds -ConfigurationName RemoteExecution
Invoke-Command -Session $TargetSession -ScriptBlock { & $using:CMD }
If you would like execute a bat file from another machine by using PowerShell Remote Session, simply enter dot and then follow by a whitespace, then enter the exact path of bat file located on that remote machine.
Example
Invoke-Command -ComputerName RemoteComputerName -Credential $credential -ScriptBlock {. "D:\Temp\Install_Something.bat"}

Launching a batch file on a remote server using invoke-command

Recently I've been having a slight problem with executing batch files on a server. Whenever I try to use Invoke-Command to launch a bat file (which simply creates a txt file for testing purposes), it doesn't return any errors nor creates the text file. I've been googling that thing for hours but didn't manage to find any helpful information. Perhaps I don't have specific permissions?
I use CredSSP for authentication. And I need to use Invoke-Command with ScriptBlock.
Code that I've been using:
Batch:
echo 123>test.txt
Powershell:
invoke-command -ComputerName $s -ScriptBlock $block -Credential $cred -Authentication CredSSP -ArgumentList $test

How to use invoke-command in powershell, to run a script on remote machine

Is it possible to use Invoke-Command in PowerShell to run a script on a remote machine?
I have tried :
Invoke-Command -ComputerName $MyPC -Credential $mycreds -ScriptBlock {
& "C:\Users\MyPC\Desktop\scripts\Script1.ps1"
}
which returns
script1.ps1 is not recognized as the name of a cmdlet
The scenario here is, I have some scripts on a remote folder, and I need to use Invoke-Command or some other ways to run the script on a remote machine.
Also, how to write if I want to pass some parameters for script1.ps1? Thanks in advance!
Instead of this:
Invoke-Command -ComputerName $MyPC -Credential $mycreds -ScriptBlock {& "C:\Users\MyPC\Desktop\scripts\Script1.ps1"}
Try this:
Invoke-Command -ComputerName $MyPC -Credential $mycreds -FilePath C:\Users\MyPC\Desktop\scripts\Script1.ps1
to avoid confusion with filepaths on local machines/remote machines; i always run stuff from smb-shares; you can enter UNC as filepath... eg:
-FilePath \server\Netscripts\script.ps1

PowerShell run .ps1 with parameters remotely

How do I run a .ps1 script with parameters remotely? Currently I have come across
Invoke-Command -Session $Session -FilePath "./testdelete.ps1 -location $Folder"
I have tried $using:Folder it does not work. It does not seem to pass the folder info to the script.
The Invoke-Command cmdlet has a -ArgumentList parameter:
Enter the values in a comma-separated list. Values are associated with
variables in the order that they are listed.
Example:
Invoke-Command -Session $Session -FilePath "./testdelete.ps1" -ArgumentList $Folder