VMware Cloud Community
eaphilipp
Contributor
Contributor

Restart Service Script ?

Hello,

I am still a little new to powershell but I am trying really hard to learn. Currently I am trying to write a little script that will restart a couple services and I am getting a few errors. Am I close here or way off?

# Variables for the services #

$mgmt = Get-VMHostService -VMHost $a | where {$_.Key -eq 'mgmt-vmware'}

$vpxa = Get-VMHostService -VMHost $a | where {$_.Key -eq 'vmware-vpxa'}

 

# Restarts the services #

Restart-VMHostService $mgmt -Confirm

Restart-VMHostService $vpxa -Confirm

Thanks in advance.

eric

0 Kudos
11 Replies
GrantOrchardVMw
Commander
Commander

Hi Eric,

mgmt-vmware isn't manageable through powercli - see Luc's post here

http://communities.vmware.com/message/1388491

The only other modification you need to make is to change

Restart-VMHostService $vpxa -Confirm

to

Restart-VMHostService $vpxa -Confirm:$true

Grant

Grant http://grantorchard.com
0 Kudos
LucD
Leadership
Leadership

Afaik, you don't have to specify the switch as -Confirm:$true, just -Confirm is enough.

If you include the switch, it is considered as $true.

If the switch is not mentioned it is considered as $false.

Although, I admit that -Confirm:$true improves the readability of the cmdlet call.


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

0 Kudos
eaphilipp
Contributor
Contributor

Ok, Crud. Here is the deal, I am having an ESX host that continuely gets a "cannot syncronize host" error in vCenter. In order to fix this error it seems I have to putty into the host and restart the mgmt-vmware and the vpxa service and then reconnect the host. I thought maybe I could just do a script for this.

Any thoughts?

0 Kudos
LucD
Leadership
Leadership

As long as ESX is still around you can create a script that uses plink.exe to start commands inside the COS.

See get-vmhosthba  (port speed) for an example


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

0 Kudos
eaphilipp
Contributor
Contributor

Could you give me a hint how I could make this work for me? I looked at the script you provided and now my head hurts. Smiley Happy

0 Kudos
LucD
Leadership
Leadership

Sure, this is a minimal script that restarts both services

$esxName = <esx-hostname>
$user = <user>
$pswd = <password>


$plink = "<PuTTY dir>\plink.exe"
$plinkoptions = " -v -batch -pw $pswd"
$cmd = "/sbin/service mgmt-vmware restart; /sbin/service vmware-vpxa restart"
$remoteCommand
= '"' + $cmd + '"'
$command
= '"' + $plink + '"' + " " + $plinkoptions + " " + $User + "@" + $esxName + " " + $remoteCommand

Invoke-Expression -command $command


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

0 Kudos
eaphilipp
Contributor
Contributor

So I tried to pull in that script:

$plink = "c:\Program Files(x86)\PuTTY\plink.exe"

$plinkoptions = " -v -batch -pw $pswd"

$cmd = "/sbin/service mgmt-vmware restart; /sbin/service vmware-vpxa restart"

$remoteCommand = '"' + $cmd + '"'

$command = $plink + " " + $plinkoptions + " " + $User + "@" + $esxName + " " + $remoteCommand

Invoke-Expression-command $command

I get the following errors:

The term 'C:\Program' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At :line:1 char:11

+ C:\Program <<<< Files (x86)\PuTTY\plink.exe -v -batch -pw System.Security.SecureString root@ssistesxm1 "/sbin/service mgmt-vmware restart; /sbin/service vmware-vpxa restart"

0 Kudos
LucD
Leadership
Leadership

It's a quotes thing because of the blanks, the $plink and $cmd strings should be between single quotes.

I corrected my previous reply.


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

0 Kudos
eaphilipp
Contributor
Contributor

Hmmm

I made the change with the quotes on got the same errors.

The term 'c:\Program' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At :line:1 char:11

+ c:\Program <<<< Files (x86)\PuTTY\plink.exe -v -batch -pw back2myroots root@ssistesxm1 "/sbin/service mgmt-vmware restart; /sbin/service vmware-vpxa restart"

I wish I understood this stuff better

0 Kudos
LucD
Leadership
Leadership

The easiest bypass is to use a path to plink.exe without blanks in there 🙂

Can't seem to find an immediate solution to cope with the blank.


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

0 Kudos
GrantOrchardVMw
Commander
Commander

I know this isn't the answer to the question you asked, but if it's ESX then why not create a CRON job to do this?

Grant

Grant http://grantorchard.com
0 Kudos