SOLVED

Running remote commands in server

Copper Contributor

I have TestComplete in my Server name: TestServer and I would like Teamcity to run the TestComplete Projects which is in Server: TestServer. I did some research and found Invoke-Command -ComputerName TestServer, Local Computer -ScriptBlock {}. I am confused on how to use this and I am new to Powershell. Help is highly appreciated.

15 Replies

You can place the commands you want to run inside the ScriptBlock part, and you can use invoke-command with the -Credential (Get-Credential) parameter to get a pop-up in which you can enter the credentials for the test server.

 

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=po...

 



Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.

If one of the posts was helpful in other ways, please consider giving it a Like.

As you can see I have written 2 computer names, TestServer and Local. Do I really need to write both or just TestServer? And for Credential, I should be writing the credential for TestServer right? Help is highly appreciated. Thanks
You specify the Computer or Computers in the -Computername parameter. If you don't specify the -Computername parameter, it will run the ScriptBlock locally. Identify the computer(s) on which the -ScriptBlock command should run, use the -Computername parameter, and separate computers using commas. (See it as if you would log into that server and enter the commands manually there.)

For the Credential, you should enter the username and password for the server you want to let the Scriptblock run on. (Same credentials that you use to login to that machine I guess) Specify it like Domain\Username and password or Computername\Username and password
From my understanding, If I run the following command,
Invoke-Command -ComputerName TestServer, Local Computer -ScriptBlock {Get Credential}, it will run the TestComplete which is in TestServer to the Local Computer? And then we only have to specify Username and Password of TestServer, right?
And what happens if I only use this Invoke-Command -ComputerName TestServer -ScriptBlock {Get Credential}, this means it will run the command in TestServer only right?
Thanks
Invoke-Command -ComputerName TestServer, Local Computer -Credential (Get-Credential) -ScriptBlock {
commands here
}

You specify all the parameters for Invoke-Command, and then in the -Scriptblock {} part, you put the commands you want to start on the remote machine.

By running the below command, you will only run it on the Testserver:
Invoke-Command -ComputerName TestServer -Credential (Get-Credential) -ScriptBlock {
commands here
}

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=po... has good examples.
Could you elaborate more about: "I have TestComplete in my Server name: TestServer, and I would like Teamcity to run the TestComplete Projects" What is TeamCity TestComplete exactly? How would you usually start that? That's the thing you insert in the scriptblock part.
https://www.youtube.com/watch?v=1vUs_EO1AN4 is also lovely to watch and learn more.
So, ok Team city is an Integration Tool and whenever a build kicks off in Teamcity we want TestComplete (Testing tool which is in server: TestServer) to run the project for us which we created in Test Complete. TestComplete is installed in the server TestServer.
Thanks. I am sorry for confusion
Ok, but can you start it using one command-line?
yes
Then you can add that between the brackets, let me know how it goes 🙂
$creds = Get-Credentials
Declare $creds in Scriptblock
$RemoteServer = "TestServer"
$ProjectPath = "E:\Projects\uw-web\uw-web" # actual path of the project
# Define the command to run on the remote server
$CommandToRun = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\SmartBear\TestComplete 15"
$CommandArguments = "$ProjectPath\uw-web\uw-web\uw-web.pjs /project:Smoke /r /sl:E:\Results\new-log.txt"

Invoke-Command -ComputerName TestServer,Local -ScriptBlock {$creds , TestComplete.exe "E:\Projects\uw-web\uw-web\uw-web.pjs" /r /sl:"E:\Results\new-log.txt"}

Do you think this command will kick off build in Teamcity and Run the project Uw-web of TestComplete which is in TestServer?

But should you run this on both servers or not? If all the software is on testserver, then it should be:

$creds = Get-Credentials
Invoke-Command -ComputerName TestServer -Credentials $creds -ScriptBlock {c:\program files\xxxx\yyy\TestComplete.exe "E:\Projects\uw-web\uw-web\uw-web.pjs" /r /sl:"E:\Results\new-log.txt"}

And $commandtorun points to a shortcut, I guess? What is inside that shortcut? You should put that before the testcomplete.exe, and I gave an example above (Enter the full path to the .exe)

 

You could also try :

 

$creds = Get-Credentials
Invoke-Command -ComputerName TestServer -Credentials $creds -ScriptBlock {

cd "c:\program files\xxxx\yyy"

.\TestComplete.exe "E:\Projects\uw-web\uw-web\uw-web.pjs" /r /sl:"E:\Results\new-log.txt"

}

 

This way, you change to the directory and run testcomplete.exe. 

TestComplete is in TestServer and Teamcity is cloud based.
best response confirmed by sohailalam (Copper Contributor)
Solution
If you want to let Teamcity run a command on testserver, you must run it on the Teamcity server. You can't do a invoke-command in a scriptblock already running on a different server.

It's difficult to get the complete picture of the work flow that you're trying to achieve 🙂
1 best response

Accepted Solutions
best response confirmed by sohailalam (Copper Contributor)
Solution
If you want to let Teamcity run a command on testserver, you must run it on the Teamcity server. You can't do a invoke-command in a scriptblock already running on a different server.

It's difficult to get the complete picture of the work flow that you're trying to achieve 🙂

View solution in original post