This is an example of how to modify a service named Windows_Exporter:
# Enabling Collectors for a Hyper-V Server
$serviceName='Windows_Exporter'
$enabledCollectors='os,cpu,cs,logical_disk,net,tcp,hyperv,service,textfile'
$switches=" --log.format logger:eventlog?name=$serviceName --collectors.enabled $enabledCollectors"
function modifyService{
param(
$serviceName,
$switches
)
$wmiService=Get-WmiObject win32_service| ?{$_.Name -like "*$serviceName*"}
$exePath=[regex]::match($wmiService.PathName,'\"(.*)\"').groups[1]
$binarySwitches='\"' + $exePath + '\"' + $switches
sc.exe config $serviceName binpath= $binarySwitches
sc.exe qc windows_exporter
restart-service $serviceName
}
# Local execution
modifyService $serviceName $switches
# Remote execution
$computerNames=@(
'HyperV001',
'HyperV002',
'HyperV003',
'HyperV004'
)
foreach($computer in $computernames){
write-host "Executing function on $computer..."
invoke-command -computername $computer -scriptblock {
param($modifyService,$servicename,$switches)
[ScriptBlock]::Create($modifyService).Invoke($servicename,$switches)
} -Args ${function:modifyService},$servicename,$switches
}