Get host machine name when a script/command is executed powershell Invoke-Command - powershell

I have some Powershell scripts for deploying our applications to test machine. These have to be executed using Invoke-Command from the developer machine. Is there anyway to log host machine name/IP from which these scripts are called.
For example:
If developer foo deployed an application using the script from devmachine1 i want to get the name/IP of the devmachine1 and log it in our test machine.
We are using Powershell V4.0. Can upgrade to 5.0 if required.

answering this to mark the question as solved. just incase if some one is looking for an answer.
I am passing computer name $env:COMPUTERNAME as a parameter to the remote script.
Getting the remote username using $PSSenderInfo.ConnectedUser in the remote script.

Related

PSS-Remoting problem while connecting to a connection broker to retrieve sessions

Tryin to be synthetic, I'm trying to connecting from a W7 operating system to a connection broker in order to retrieve all of the user's sessions with this simple function:
Get-RDUsersession -connectionbroker 'xx' -collectionname 'xx'
Since I discovered that it can't be done from W7 because the remotedesktop module can't be downloaded even after installed RSAT, some users advise me to use PSSRemoting commands in order to send command on a remote computer where surely the function works and thanks to this way run the entire script i've made on the local computer.
The problem, as you can see in the image attacched, is that it keeps me prompt out this error:
A remote desktop services deployment does not exist on server.se
The same command works on W10 and i'm sure the RDS Deployment exist on that server. I've tried a lot of ways but i can't figure out why it doesn't works.
Someone can help me out ?
You're not really running your command using PowerShell Remoting (H: is not on the remote system as you can see in your screenshot).
Connect to the remote system using:
Enter-PSSession -ComputerName "yourRemoteMachine"
and the run your Get-RDUsersession -connectionbroker "xx" command.

Execute remote PowerShell script from VSOnline fails - Access Denied

I am facing a very strange issue trying to run a Remote PowerShell script from the new build system of Visual Studio Online.
Context:
I have a Build server that I host on my own VM. I have also created a second VM where I want the PowerShell script to be run on.
When I run a build from the Build server, I want to create a task that run a PowerShell script on a remote machine. Sounds easy.
Both machines have everything configured to run the Remote PowerShell. (I think)
Problem:
If I manually run the script from the build server, the script is properly executed on my remote machine.
But, if I run the script by starting a new build, it doesn't work, I always get an error message
New-PSSession : [xxx.cloudapp.net] Connecting to remote server xxx.cloudapp.net failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.
Have you faced this issue before with VSOnline?
This can happen if there isn't a trust relationship between the remote machine and the build machine. E.g. you could look at the hosts.equiv file on the remote machine and make sure it trusts the build machine.
If that doesn't help then more info might be needed, like how you're trying to sign in, what build agent account you're running as.
Guy

How to uninstall a service on remote machine using powershell?

I am a newbie in powershell script. I want to uninstall a service on remote machine and re-install it again. I am able to stop the service, change the files that service is using and again start it.
But, now I want to completely uninstall it.
One more thing, I will be executing these scripts via TeamCity.
I have tried following ways, but each one is giving trouble:
using wmi class
running installutil command on remote machine. But, cant execute this on remote machine since there is problem in accessing that service.
Using Get-WmiObject command and then calling delete() on retrieved service.
Is there any promising solution ??
Any help will be appreciable..
After playing a lot with TC and many commands, finally I got my answer to uninstall a service on remote computer, and its working properly.
I have made use of "wmic" command and provided the remote computer name, username and password for /node:, /username:, and /password: parameters. And, provided 'installutil.exe' command to be executed remotely. Here, the actual command is..
"wmic /NODE:$serverName /USER:$userName /PASSWORD:$password process call create '$path_to_installutil_exe\installutil /u $exe_location_path_of_service'"

Need to run command on a remote server with no access to remote desktop

As the title describes I need to run a command on a remote server and I do not have access to remote desktop! I head that this should be possible using Powershell, but I can't find any good tutorials on this.
Recommendations?
The easiest way is to use psexec.exe (Part of Microsoft Sysinternals).
You can also use powershell.
I wrote a blogpost about these two alternatives a few days ago:
http://blog.degree.no/2012/03/executing-commands-and-programs-on-a-remote-machine-using-powershell/
When using PSEXEC or PowerShell remoting you can execute a command on a remote machine, but it is not interactive with any currently logged on user. Using PowerShell requires 2.0 to be installed on your machine and remotely AND remoting must be enabled AND you must have admin rights on the remote computer.

Why would a get-process -computername return a couldn't connect to remote machine error?

I can execute the following command on a specific machine and it can return a list of processes on several target computers but returns a "Couldn't connect to remote machine" error on one machine.
get-process -ComputerName 192.168.1.101
None of the target machines have powershell installed.
The machine I am executing the powershell script on has 2.0 installed.
I have admin privileges on all machines.
The target machines that are able to return a list of processes remotely are server 2003 Standard Edition with SP 1.
The target machine that does not return a list of processes remotely is server 2003 R2 Standard Edition SP2.
What would prevent me from getting a list of processes from a remote machine using powershell?
It turns out the machine that was returning the error did not have the Remote Registry service started. Once I started this service the "Couldn't connect to remote machine" error went away. This blog post helped me find the solution http://sebastienlachance.com/post/ProcessGetProcesses-common-errors.aspx
I think it uses WMI, so if the service isn't running, that would do it. Can you execute Get-WMIObject queries against those PCs at all?