enable ssh service on vm deployed inside esxi using powercli - powershell

I have few vm's deployed inside an esxi,whose details I can find out using "Get-Vm"
command,now I want to enable ssh for these all vm's ,but unable to do so.I am aware
of command to enable ssh for host/esxi like:
"Get-VMHost | Foreach {Start-VMHostService -HostService ($_ | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )}"
But cant figure out a way to enable ssh for the vm's inside a esxi,any pointers will be appreciated.

The command you're referencing is for controlling the ESXi Host's SSH service and has no bearing on the guest VM's SSH status.
To enable SSH on those guests will require a couple things, namely VMware Tools installed and running as well as some valid credentials to the guest OS.
Once you have those, you can use the Invoke-VMScript cmdlet to send commands to the VM through VMware Tools.
Example:
Invoke-VMScript -VM vmName -ScriptType Bash -ScriptText "systemctl start sshd.service" -GuestUser username -GuestPassword password

Related

How can I resize the Docker Desktop Virtual Machine on Windows 10 from a PowerShell script?

I am attempting to write a PowerShell script (using PS core 7.0) to install and configure a Kubernetes cluster running on Kind on Windows 10 machines used by my teams. I have a working script to start up and configure the cluster the only issue is that I would like to (need to) ensure the Docker Desktop VM has enough memory available to run a few of our micro services inside the cluster at the same time.
I've got a bit of code cobbled together to perform the task and it works up to the very last step where I attempt to get the docker daemon working again after the restart. As soon as I run the command to do that, the VM is reconfigured back to its previous memory size.
Here's what I have to perform the resizing:
Stop-Service *docker*
Get-VM DockerDesktopVM | Stop-VM
Get-VM DockerDesktopVM | Set-VMMemory -StartupBytes 12888MB
Get-VM DockerDesktopVM | Start-VM
Start-Service *docker*
# https://stackoverflow.com/questions/51760214/how-to-restart-docker-for-windows-process-in-powershell
&$Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchDaemon
&$Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchDaemon
Note: I found the post # How to restart docker for windows process in powershell? which is were I got the last 2 lines.
In researching the issue further I have found that I can use the following single line instead, but I still have the same issue in that the memory size is reverted back once the command is run.
&$Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchLinuxEngine
If I do not run either DockerCli.exe -SwitchDaemon twice or DockerCli.exe -SwitchLinuxEngine once then I get the error:
error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.40/containers/json: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Window
s, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running.
Is there a better way to go about resizing the VM memory or to shutdown and restart docker without causing the change to be reverted?
For anyone else who is attempting the same thing, or something similar I got a hint from the Docker Desktop for Windows Community on GitHub that helped me find a solution. In a nutshell the recommendation was to simply change the settings file directly. What I found worked was to:
Stop the Docker Services (There are 2 of them)
Update the settings file (# ~\AppData\Roaming\Docker\settings.json)
Start the Docker Services
Switch the Daemon Context to Linux (Same as it was before, but it appears to need a nudge to pick things up after restarting the services).
Here's the PowerShell:
Stop-Service *docker*
$settingsFile = "$env:APPDATA\Docker\settings.json"
$settings = Get-Content $settingsFile | ConvertFrom-Json
$settings.memoryMiB = 8192
$settings | ConvertTo-Json | Set-Content $settingsFile
Start-Service *docker*
&$Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchLinuxEngine

Connect to Azure VM from local

i'm absolutely beginner in power-shell scripting and windows automation.so any help would be appreciated.
i have access to a Azure-VM in my local pc at office. There are some tasks that i need to do daily on my local and VM machine. i am trying to automate these tasks with powershell.
At one step i need to copy some CSV files from my VM to local and this is where i'm stuck.
How do i connect to provided AzureVM from my local machine?(i'm not an admin for azure portal)
I have User-Name , Password & my VM name(****.cloudapp.net) to connect in Remote Desktop Connection. i want to know if i can connect to provided VM and copy some files if yes then how!
i have been searching for ways to connect but cant find any solution.
i have installed azure module for powershell, tried Get-AzureVM but this does not work. As i said earlier any help would be really appreciated. thanks
First, please check port 5985 is listening on Azure VM using
netstat -ano
Then you need to add an inbound rule to allow the port 5985 on the NSG associated with the Azure VM in the azure portal. For the error message
The WinRM client cannot process the request
you need to add the remote Azure VM to the local machine's TrustedHosts list by executing the following command in the commands prompt as an administrator:
winrm set winrm/config/client #{TrustedHosts="Azure VM public IP address"}
I can copy a file folder named test from Azure VM to a local folder named tesp with powershell commands as below:
$c = Get-Credential
$sess =New-PSSession -ComputerName ip address -Port 5985 -Credential $c
Copy-Item -FromSession $sess -Path d:\test -Destination c:\tesp -Recurse
More information about Powershell Remoting.

Change BIOS password through powershell

I want to build a script to change and/or set up BIOS password to HP workstations.
Script i run as follows:
C:\> $computers=Get-Content -Path c:\computers.txt
C:\> foreach ($computer in $computers) {
$passChange=Get-WmiObject -computername $computer -Namespace root/hp/instrumentedBIOS -Class HP_BIOSSettingInterface
$passChange.SetBIOSSetting('Setup Password','<utf-16/>MYNEWPASSWORD','<utf-16/>')
}
Now, the following happen:
If my BIOS has no password, the script works just fine!
If my BIOS has password already, script has Return: 6. I suppose there is
a different option for changing the BIOS password?If yes, any help
is appreciated!
If i run the script for my computer, it works.
If i run the script for another computer i get the following error:
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA).
Is there a way to enable an option to enable the RPC for this feature and then disable it again?
Thank you in advance
According to HP's documentation HP Client Management Interface the WMI interface supports remote interfacing.
You need to ensure all remote computers you're attempting to connect to have the HP custom WMI Namespace.
You also need to ensure the account you're running under has administrative permissions on all of the remote computers.
You may also need to explicitly set the impersonation to 3 which is impersonate.
For more information: Connecting to WMI Remotely with PowerShell
Also ensure the firewall on the remote computers is either off or has exclusions for WMI

Install RDS with Powershell on local server

I'm pretty new to Powershell and wanted to create a script that install the Remote Desktop Service which is a prerequisite of my application. (I'm on R2012 btw)
I already found that it's possible to do so with a domain account on a remote server (due to the restart needed during installation). I used:
New-RDSessionDeployment [-ConnectionBroker] <String> [-SessionHost] <String[]> [[-WebAccessServer] <String> ]
Now, I want to install RDS on my local server when I launch my Powershell script (as I can do with the Server Manager GUI). The goal is to install RDS and my application in the same Powershell script without the need to do it using a remote server.
Is it possible to do so ? Should I use the role-based RDS installation or is there any tricks I can use to bypass the local server restart (like maybe a workflow) ?
You just need to add the RDS Feature
Add-WindowsFeature –Name RDS-RD-Server –IncludeAllSubFeature -Restart
If you don't include -Restart the restart is not performed - but will be needed before the feature can be used.
Yes , on the context that you need to use New-SessionDeployment but having being said you still need the remotedesktop module to be imported first using
Import-Module RemoteDesktop
Now you need have RD Connection Broker, RD Web Access, and RD Session Host by using:
New-SessionDeployment –ConnectionBroker server.domain.com
–WebAccessServer server.domain.com –SessionHost server.domain.com
Now you need a Licensing Role, use:
Add-RDServer -Server server2.domain.com -Role RDS-LICENSING
-ConnectionBroker server1.domain.com
NOw we have use the deployment for the licensing , use :
Set-RDLicenseConfiguration -LicenseServer server2.domain.com -Mode PerUser
-ConnectionBroker server1.domain.com
Now you can use ,
New-RDSessionCollection and can publish New-RDRemoteapp
This should help you in proceeding further.

Find SCVMM Management Server within a VM Host/VM

Is it possible to locate a SCVMM Management server from within a Hyper-v host? Is this possible via powershell? I am trying to find which machine manages one of my Hyper-v hosts. I've had no luck searching through registry/wmi. Any ideas?
Thanks in advance!
Not sure about powershell, but I just had to do this today.
1) looked up the port config for scvmm. So far, 5985 looks like the one: Link
2) On your host, run netstat -ano |find "5985"
3) That should return a list scvmm management servers connected.
As far as I know, no. What you could do is to query all of your SCVMM servers and see which happens to know the guest.
Load up the VMM module and connect to your VMM.
# VM name, might or might not be hostname
$guestToLook = "myLostVM"
# A list of all of your VMM servers
$VMMServers = #("vmmsrv01", "vmmsrv02", "vmmsrv03")
$VMMServers | % {
# Connect to VMM server
Get-VMMServer $_
# Get a VM from the VMM host by guest name
$vm = Get-VM -name $myLostVM
# If VM object is returned, you got the VM from current VMM host.
if($vm.Name -eq $myLostVM) { "{0} is managed by VMM host: {1}" -f $vm.Name, $_}
}
Here's a solution via PowerShell.
First we need the registry path on the host for SCVMM config values.
$scvmmAgentKeys = 'HKLM:\SOFTWARE\Microsoft\Microsoft System Center Virtual Machine Manager Agent\Setup'
Now we grab the SCVMM agent port from the registry path.
$wsManTcpPort = (Get-ItemProperty -Path $scvmmAgentKeys -Name WSManTcpPort).WSManTcpPort
Finally, collect the addresses which are listening on that port.
$scvmmAddress = (Get-NetTCPConnection -LocalPort $wsManTcpPort).LocalAddress | Get-Unique | where { $_ -ne "::" }
Note that if anything else has a connection established on the same port (default port at time of writing is 5985) then $scvmmAddress will be an array including the addresses of those other established connections, which are not necessarily SCVMMs.
Fantastic . The only correction shall be
3. $scvmmAddress = (Get-NetTCPConnection -LocalPort $wsManTcpPort).**Remoteaddress** | Get-Unique | where { $_ -ne "::" }