Simply trying to cycle through a csv containing VC names and Service names. The script should cycle through and check each VC for the services listed in the csv and then pass through if all of the services are running.
$vCenterCred = Get-Credential
$vCenters = Import-Csv ".\vCenters.csv"
$vCenterServices = Import-Csv ".\vCenterServices.csv"
#Check for running servie status before continuing
$ServiceUp = "Running"
foreach ($vCenter in $vCenters) {
Do {
foreach ($vCenterService in $vCenterServices) {
[string]$status = (Invoke-Command -ComputerName $vCenter.Name -ScriptBlock {Get-Service -Name $vCenterService.Name | select Status} -Credential $vCenterCred).status
Write-Host $status
Write-Host $vCenterService.Name
}
}
While ($ServiceUp -ne $status)
}
I note only one VC and only one service, but it seems to be cycling through all the services:
Results:
vspherewebclientsvc
Running Running Running Running Stopped Running Stopped Running Stopped Stopped Stopped Stopped Running Stopped Stopped Running Stopped Stopped Runnin
g Running Running Stopped Stopped Running Stopped Running Stopped Stopped Running Running Running Stopped Stopped Stopped Running Running Stopped Runn
ing Running Running Stopped Stopped Stopped Running Stopped Running Stopped Stopped Running Running Stopped Running Running Running Running Running St
opped Running Running Stopped Stopped Stopped Running Running Running Stopped Running Running Stopped Stopped Running Running Running Running Running
Stopped Stopped Stopped Running Running Stopped Running Running Stopped Stopped Running Stopped Running Stopped Running Running Running Stopped Stoppe
d Running Stopped Running Stopped Stopped Stopped Running Stopped Stopped Stopped Stopped Stopped Running Running Stopped Running Running Stopped Stop
ped Running Running Stopped Running Stopped Stopped Running Stopped Running Running Running Stopped Running Running Running Running Running Stopped Ru
nning Running Running Stopped Running Stopped Stopped Stopped Stopped Stopped Stopped Stopped Running Running Running Running Stopped Running Stopped
vspherewebclientsvc
Running Running Running Running Stopped Running Stopped Running Stopped Stopped Stopped Stopped Running Stopped Stopped Running Stopped Stopped Runnin
g Running Running Stopped Stopped Running Stopped Running Stopped Stopped Running Running Running Stopped Stopped Stopped Running Running Stopped Runn
ing Running Running Stopped Stopped Stopped Running Stopped Running Stopped Stopped Running Running Stopped Running Running Running Running Running St
opped Running Running Stopped Stopped Stopped Running Running Running Stopped Running Running Stopped Stopped Running Running Running Running Running
Stopped Stopped Stopped Running Running Stopped Running Running Stopped Stopped Running Stopped Running Stopped Running Running Running Stopped Stoppe
d Running Stopped Running Stopped Stopped Stopped Running Stopped Stopped Stopped Stopped Stopped Running Running Stopped Running Running Stopped Stop
ped Running Running Stopped Running Stopped Stopped Running Stopped Running Running Running Stopped Running Running Running Running Running Stopped Ru
nning Running Running Stopped Running Stopped Stopped Stopped Stopped Stopped Stopped Stopped Running Running Running Running Stopped Running Stopped
vspherewebclientsvc
Many thanks for any direction on this one.
Inside the Do-While block you have a ForEach-Object cmdlet that runs through all the vCenter services.
The test in the While condition is only executed when the ForEach-Object completes,
I'm not sure what exactly you want to do here.
Do you want to stop looking at a vCenter when you encounter the 1st service from your list that is "running" ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc. To back up a bit. I have two CSV files. One with the names of vCenter servers and one with the names of vCenter related services. The overall script will be used to automate the editing of various configuration details - but focusing now on JVM settings. Any way, this part of the script was going to be used to stop the services in the csv for each vcenter in the csv and then only move forward once they are back up. This section specifically was just the check to see that they are back up before moving. The more that I think about it, it may be best to just have a huge foreach loop for each VC and adjust the JVM settings for VC1 and then move to VC2 etc.. We have quite a few, I'm trying to wrangle .
Something like this. I'd really like to polish it up at the end with menus regarding which JVM settings you want to change and to what value etc, but time constraints may step in .
$vCenterCred = Get-Credential
$vCenters = Import-Csv ".\vCenters.csv"
$vCenterServices = Import-Csv ".\vCenterServices.csv"
$vspherewebclientsvcJVM = 3072
$vctomcatJVM = 3072
foreach ($vCenter in $vCenters) {
#Check JVM sizes before change
Write-Host " "
[string]$VCWebClientHeap = Invoke-Command -ComputerName $vCenter.name -ScriptBlock `
{gc 'C:\Program Files\VMware\Infrastructure\vSphereWebClient\server\bin\service\conf\wrapper.conf' | `
Select-String -Pattern 'wrapper.java.maxmemory'} -credential $VCenterCred
$VCWebClientHeapParse = $VCWebClientHeap.split('=')[1].split('m^')[0]
[string]$VCWebHeap = Invoke-Command -ComputerName $vCenter.name -ScriptBlock `
{gc 'C:\Program Files\VMware\Infrastructure\tomcat\conf\wrapper.conf' | `
Select-String -Pattern 'wrapper.java.additional.9'} -credential $VCenterCred
$VCWebHeapParse = $VCWebHeap.split('x')[1].split('M^')[0]
Write-Host "JVM settings before change -"$vCenter.Name, "WebClient:" $VCWebClientHeapParse, ", WebServices:" $VCWebHeapParse
#Backup configuration files
Invoke-Command -ComputerName $vCenter.name -ScriptBlock {Copy-Item "C:\Program Files\VMware\Infrastructure\vSphereWebClient\server\bin\service\conf\wrapper.conf" "C:\Program Files\VMware\Infrastructure\vSphereWebClient\server\bin\service\conf\wrapper_backup.conf"} -credential $VCenterCred
Invoke-Command -ComputerName $vCenter.name -ScriptBlock {Copy-Item "C:\Program Files\VMware\Infrastructure\tomcat\conf\wrapper.conf" "C:\Program Files\VMware\Infrastructure\tomcat\conf\wrapper_backup.conf"} -credential $VCenterCred
#List out original and backup file
Invoke-Command -ComputerName $vCenter.name -ScriptBlock {Get-ChildItem *.conf -Path "C:\Program Files\VMware\Infrastructure\vSphereWebClient\server\bin\service\conf\" } -credential $VCenterCred
Invoke-Command -ComputerName $vCenter.name -ScriptBlock {Get-ChildItem *.conf -Path "C:\Program Files\VMware\Infrastructure\tomcat\conf\" } -credential $VCenterCred
#Stop services on the vCenter server
}
To wait till all the services in the CSV are running, you could do something like this
$vCenterCred = Get-Credential
$vCenters = Import-Csv ".\vCenters.csv"
$vCenterServices = Import-Csv ".\vCenterServices.csv"
#Check for running servie status before continuing
$ServiceUp = "Running"
foreach ($vCenter in $vCenters) {
Do {
foreach ($vCenterService in $vCenterServices) {
$status = ""
while ($status -ne "Running"){
$status = Invoke-Command -ComputerName $vCenter.Name -ScriptBlock {Get-Service -Name $vCenterService.Name | select -ExpandProperty Status} -Credential $vCenterCred
sleep 2
}
}
}
To wait till all the services in the CSV are stopped, you can use the same logic, but then test for the "Stopped" status.
In fact you could make that a function, and use the status to look for as a parameter.
function Wait-ServiceStatus{
param(
[string]$Computer,
[string]$Service,
[string]$TargetStatus,
[System.Management.Automation.PSCredential]$Credential
)
do {
$status = Invoke-Command -ComputerName $Computer -ScriptBlock {Get-Service -Name $Service | Select -ExpandProperty Status} -Credential $Credential
sleep 2
} until($status -eq $TargetStatus)
}
$vCenterCred = Get-Credential
$vCenters = Import-Csv ".\vCenters.csv"
$vCenterServices = Import-Csv ".\vCenterServices.csv"
#Check for running service status before continuing
$ServiceUp = "Running"
foreach ($vCenter in $vCenters) {
Do {
foreach ($vCenterService in $vCenterServices) {
Wait-ServiceStatus -Computer $vCenter.Name -Service $vCenterService.Name -TargetStatus $ServiceUp -Credential $vCenterCred
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference