Here is another variation on the popular subject: PowerShell remoting. I have a system with a number of VMs hosted on a Hyper-V server. I have a need to launch programs on them remotely. I succeeded in doing this from the very Hyper-V host (I believe Microsoft calls this PowerShell Direct). I run
Invoke-Command -VMName MyVM -ScriptBlock {Mybatfile.bat}
which successfully logs into MyVM using the credentials of the current Hyper-V host login and spawns there MybatFile.bat with administrative privileges.
Unfortunately, I expect my system to be transitioned to a VMWare vSphere host, and such option will no longer be available. I hope to learn how to run Invoke-Command from one of the VMs. I understand that this may require some prep work on both the VM I am logged into and the target VM. All the VMs have an administrator account with identical credentials.
What makes this a bit more tricky is several of my VM share the same system name (reported e.g. in the properties of "This PC" as "Device Name"). They have unique VM names as they appear in the Hyper-V manager, and will have unique VMWare names. Ideally I want to keep it that way, and address the target VM by a static private IP of its virtual network adapter, or perhaps by some unique VM ID that will continue to be available on VMWare vSphere.
Thank you in advance.
Related
My goal is to write a Powershell script that will run New-AzureRmResourceGroup and New-AzureRmResourceGroupDeployment in order to provision a resource group according to an ARM .json template file. Said resource group includes a virtual machine, virtual network, network security group, public IP address, network interface attached to the virtual machine, and two storage accounts. After that, I want the same script to go on and copy a specific program installer to the virtual machine in that resource group and run that installer, automatically without further user interaction. However, I can't seem to begin a remote Powershell session with the virtual machine. I run the command:
$sess = New-PSSession -ComputerName **.***.**.*** -Port XXXX -Credential $cred
where the *s are the IP address of the Virtual Machine; XXXX is the Port that is open for RDP according to the Network Security Group associated with the virtual network that the Virtual Machine is on; and $cred contains the credentials of the admin user on the Virtual Machine.
The command always returns an error:
New-PSSession : [**.***.**.***] Connecting to remote server **.***.**.*** failed with the following error message :
The client cannot connect to the destination specified in the request. Verify that the service on the destination is
running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the
destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the
destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the
about_Remote_Troubleshooting Help topic.
Note that I did make sure to add the Virtual Machine's IP address to the list of Trusted-Hosts on my local machine. (Until I did that, I got a different error message.) Also, I am able to connect to the Virtual Machine if I click on its Connect button in the Azure portal and then click on the .rdp file that's downloaded. This remote session uses the same IP address, port, and credentials as the one I'm trying and failing to set up in Powershell. This is what I don't understand.
Why does that happen? Is there some additional work I need to do to prepare the VM for accepting remote Powershell sessions? Is there any way to configure it in the ARM template so that the VM will be ready to accept them from the get-go? (It would be difficult if I need to run some commands on the VM to set Powershell remoting up, since I can't Powershell remote in to run them because of this very problem. Maybe I could run them as a custom script extension?)
Final notes: This VM is "new" style, not "classic" style. I know there is lots of documentation out there for "classic" style Azure VMs, but that's not what I'm working with. Also, even after running winrm quickconfig on the VM as the error suggested, and enabling administrative rights remotely to local users, I get the same error when I run New-PSSession.
Open up 5985-5986 on your NSG
Drop the port part on your command:
$sess = New-PSSession -ComputerName ... -Credential $cred
WinRM endpoint is set up automatically for new VM's (if you don't somehow override it). But you need to open 5985 for nonsecure and 5986 for secure remoting
Can I use DHCP PowerShell cmdlets from a Windows 10 desktop? I have searched online a lot but only come up with results about using the cmdlets from the server. If my PC is on the DHCP server I would think there is a way to use the cmdlets from my PC. I am looking to get information about the IP addresses that are currently in use or available, etc.
Yes, you can. You have to install RSAT for Windows 10 and then make sure the DHCP tools part of RSAT is enabled.
Note that you will have to specify the DHCP server in the -ComputerName parameter of each call, or create a CIM session and use that.
What is the way to trigger a task that is in task manager of AzureVM from command prompt of my local machine??
I tried this (with AzureVm and task names)
schtasks /run /s <VMName> /tn <TaskName>
This gave me the error
ERROR: The network path was not found.
Both AzureVM and local machine are running windows server 2012 R2.
To properly answer this question it will depend on what network connectivity exists between your local machine and the Azure VM. If you are connecting to the Azure VM over the internet then you need to either open a port for the VM on the load balancer, or you need to create a public IP address for the specific VM so that you can access all of the ports of the VM over the internet. Alternatively if you have a site to site or point to site connection to the VM then you can access the VM directly as you are doing.
Assuming that you are connecting to the VM over the internet, the easiest approach is going to be to create a public IP address for the VM, then connect to the VM using either the DNS name of the public IP address or just using the IP address directly. If you wanted to go through the load balancer then you would need to determine which port and protocol (UPD/TCP) schtasks is using so that you can open the correct port.
Also remember that the Windows Firewall on the VM may need to be updated to allow scheduled tasks to be executed remotely.
An alternate option is to use Remote PowerShell to execute the schtasks on the VM. Please see the following blog post that provides great overview on using remote powershell with Azure VMs: http://michaelwasham.com/windows-azure-powershell-reference-guide/introduction-remote-powershell-with-windows-azure/
I have a HyperV server with over 10 VMs hosted on it. They all use a base image so they are not connected to a domain nor do they have there NIC configured. Is there a way that I could run commands from the host to configure the VMs NIC and join the domain as if I was actually logged in?
This is easily done if I just pop open the VM from HyperV:
###Joining domain###
$domain = "mydomain.gbl"
$cred = get-credential mydomain\defaultuser
Add-Computer -DomainName $domain -Credential $cred
Now this approach is ok if I had only a few machines but since I have so many and even more machines to come that need configuration, I am trying to find a way to automate these tasks.
Maybe I could create a scheduled task on the VMs from the host?
Since you're running Server 2008 your only option (AFAICS) would be to configure the network interfaces of the VMs with static MAC addresses (e.g. with the PowerShell Management Library for Hyper-V), set up a DHCP to provide the VMs with IP addresses. After that you should be able to do something e.g. with PsExec if the Windows Firewall is disabled on the VMs. If it isn't, you'll have to log in manually. You need at least some basic configuration for remote management.
My software can discover all the hyperv vms (a software agent is installed in the hyperv host which returns the information). From my software I can do any operation on the vm. Now I want to take a remote console view of the hyperv vms from my software. It can be a web based or flash based or any other way.... How to accomplish this ?
Do you want to get the printscreen from the console or to control the VM through console remotely?
for the first one, Hyper-V provides an API through WMI where you can get the printscreen as a picture.
for the second one, you can simply use vmconnect.exe to connect to the console which comes with RSAT (remote administrative management pack, you can install it on Win7) or inbox on server edition. The remote control of the console is done by RDP (Remote Desktop Protocol) which is the same as you run mstsc.exe to connect to a remote server. But it's using a different port and some customized metadata during initialization. You may look at the implementation by reverse-engineering vmconnect.exe with Reflector.