VMware Cloud Community
nhickey
Enthusiast
Enthusiast

Would like to restart a Windows service on a VM via PowerCLI

I am trying to find a easy way (I am sure I am missing it here) to restart a Windows service on a VM by using PowerCLI and a Powershell cmdlet "Restart-Service" within a variable.

I guess I am calling it wrong in the script cause I can't seem to get the service to restart & get some cool CLI errors too. I am testing with my VM "ana-vm10" for right now so that is why this shows in the script. I am not sure if using Invoke-VMscript is the best option.

I have also tried options like this using the Get-VM -Name "VMname" in front of the pipes below - but no luck. I think it was a permissions error of some sort on this usage, can't remember off top.

| foreach-object {Get-Service -ComputerName $_ | Where-Object { $_.Name -eq "TP AutoConnect Service" }| ForEach-Object {$_.Stop() }}

| foreach-object {Get-Service -ComputerName $_ | Where-Object { $_.Name -eq "TP AutoConnect Service" } | ForEach-Object {$_.Start() }}

Here is what I have in the script:

#PowerCLI script for restarting ThinPrint services on a VM

#Created by Nigel Hickey @vCenterNerd - October 2013

#

# Add PowerCLI snap-in for PowerShell

Add-PSSnapin VMware.VimAutomation.Core

# Set variables for script

$MyVMs = "ana-vm10"

$ESXi = "192.168.40.29"

$username = "root"

$password = "pass-goes-here"

$script = "Restart-Service TPAutoConnSvc"

$guestUser = "nhickey"

$guestPass = "pass-goes-here!"

# Establish Connection to ESXi

Connect-VIServer -server $ESXi -user $username -password $password

# Connect to VM & Restart TP services on that VM

Invoke-VMscript -ScriptText $script -vm $MyVMs -guestUser $guestUser -guestPass $guestPass

# Disconnect Connection from ESXi

Disconnect-VIServer -Server $ESXi -confirm:$false

#END

I am fairly new to PowerCLI and Powershell so I am sure I have jacked this up a tiny bit.

Any help or direction would be awesome!!

Nigel

Twitter: @vCenterNerd

Thanks, Nigel VCIX-DTM @vCenterNerd nigelhickey.com
0 Kudos
1 Reply
LucD
Leadership
Leadership

When you logon to the VM with the account you use on the Invoke-VMScript cmdlet, can you start a PowerShell prompt ?

And execute the Restart-Service cmdlet ?

Can you perhaps include the error messages you are getting when you run the script ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos