please see the code below,
I am trying to pass 2 functions to push software to new vm, one function will check the services are up and running and the other will install agents with arguments.
foreach ($Agent in $WinoswsAgnetsList) {
$AgentName=$AgentFile= $ListofServices= $ArgumentList=$MSIArguments= $DataStamp=$logFile=$InstallerFullpath =$AgentDetails= $ArgumentString =$ServiceString=$null
write-host "Now processing $Agent"
$AgentDetails=Select-HashTable -Hashtable $loadVars -Include $Agent
Write-Host $AgentDetails
$AgentName =$AgentDetails.Keys
$AgentFile =$AgentDetails.values.AgentName
$ListofServices =$AgentDetails.values.ListofServices
$Operators =$AgentDetails.values.ArgumentList
$InstallerFullpath=$AgentLocalfolder+"\"+$AgentFile
if (-not([String]::IsNullOrWhiteSpace( $ListofServices))){
$ListofServices= $ListofServices.split(",")}
foreach($Service in $ListofServices){
Write-host "Service $ListofServices" -ForegroundColor Cyan
$Service=$Service.trim()
if ([String]::IsNullOrWhiteSpace($ServiceString)){
$ServiceString = $Service
}
else {$ServiceString = $ServiceString+ -join ","+$Service}
}
$Arguments =$Operators.Split("|")
foreach ($Argument in $Arguments){
Write-host "Argumnet $Argument" -ForegroundColor Cyan
$Argument=$Argument.trim()
if ([String]::IsNullOrWhiteSpace($ArgumentString)){
$ArgumentString = $Argument
}
else {$ArgumentString = $ArgumentString+ -join ","+$Argument}
}
Write-host "AgentName $AgentName
ListofServices $ListofServices
ArgumentList $ArgumentList " -ForegroundColor Green
$AgentInstall={
Function Check-Service{
Param ($ServiceName)
Write-Host "checking $ServiceName "
try{
$Service = Get-Service -Name $ServiceName -ErrorAction stop
}
Catch { $_NoServiceFoundForGivenName
write-host "Still Waiting for the $ServiceName Service on the $VMName, I will try again shortly " -ForegroundColor Yellow
Start-Sleep -Seconds 5
write-host "I am trying $ServiceName Service on the $VMName " -ForegroundColor Yellow
}
if ($Service.Status -eq "Running")
{$Result = "PASS"
Write-Host "Service $ServiceName ......PASS" -ForegroundColor Green}
Else{$Result = "FAIL"
Write-Host "Service $ServiceName ......FAIL" -ForegroundColor Red}
Return $Result
}
Function Install-Agent{
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$AgentName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$AgentArgument,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$AgentsServicesNames)
$ArgumentList=@()
Write-Host "Agent Name is $AgentName"
#if ([String]::IsNullOrWhiteSpace($ArgumentString)){Write-Host "Arguments list is empty"}
$ArgumentString=$ArgumentString.split(",")
foreach ($Argu in $ArgumentString){
Write-host "Argumnet $Argument" -ForegroundColor Cyan
$ArgumentList += "$Argu `n" }
$ArgumentList=@($ArgumentList)
Start-Process "msiexec.exe" -Wait -ArgumentList ($ArgumentList) -Verb runas
#Start-Sleep -Seconds 140
$ListofServices=$ServiceString.split(",")
$AgentCounter= [int] "0"
foreach($Agentservice in $ListofServices){
do {$Status= Check-Service -ServiceName $Agentservice
Write-Host "waiting Status....."
if ($Status -eq "FAIL") {$AgentCounter ++
Start-Sleep -Seconds 2 }
write-host "$AgentCounter" -ForegroundColor Yellow
if ($AgentCounter -eq "240") {write-host "Serivce $AgentName took a long time to Respond " -ForegroundColor Yellow
Send-eMail -MessageType Failure -MessageContent "Serivce $AgentName took a long time to respond on $VMName"
Stop-Process -Name powershell -Force}
}until ($Status -eq "PASS")
Write-Host "waiting....."
}
Start-Sleep -Seconds 20
}
Install-Agent -AgentName $AgentName -AgentArgument $ArgumentString -AgentsServicesNames $ServiceString
#invoke-command -ComputerName $vmname -ScriptBlock {param ($AgentName) & $ServiceString} -ArgumentList $ArgumentList
}
Invoke-VMScript -vm $vmname -ScriptText $install -GuestCredential $WinodwsLocalCredentials -Server $vCentre -ScriptType Powershell -Verbose
Write-Host "Installing $AgentName
Arguments $ArgumentList
Services $ListofServices" -ForegroundColor Yellow
Start-Sleep -Seconds 20
}