Hi,
Could some one help me to get the all the properties which I can see from | gm.
Please find the below pic for more details. I want to have all the properties, especially the highlighted one.
[attachment file=“Capture.PNG”]
Hi,
Could some one help me to get the all the properties which I can see from | gm.
Please find the below pic for more details. I want to have all the properties, especially the highlighted one.
[attachment file=“Capture.PNG”]
Test-ServiceHealth is returning multiple objects, and what you’re seeing in your second command is the properties of the Array object, not the ExchangeServicesStatus objects contained in the array. Try this:
$status = Test-ServiceHealth dagn1 | Select-Object -First 1 $status.PSObject.Properties
hmm, the above code will not help because Test-ServiceHealth returns 3 elements. if we give -First 1 then we will get only first element and remaining 2 will be dropped.
I am trying to achive the below code. The below coding is working fine for all other cases but not working especially for Test-Service. I am not sure what else it is producing speicially.
$Output = New-Object -TypeName PSObject $temp = Test-ServiceHealth dagn1 ForEach($t in $temp.PSObject.Properties) { $Output | Add-Member -MemberType NoteProperty -Name $t.Name -Value $t.Value } Write-Output $Output
[attachment file=“Capture.PNG”]
What do you want your $Output object to look like? You have 3 input objects, all with the same set of properties (Role, RequiredServicesRunning, ServicesRunning, ServicesNotRunning).
Dave, I am actually trying hold all the properties of several command’s properteis in a single PSObject.
My PSObject is already having several parameters of other commands like Test-MailFlow, get-mailbox…etc. The code I wrote above is working fine for all other cmdlets. But when I am trying to do the same with test-servicehealth cmdlet its not working alike other commands.
In fact I got this clue form you only
You’re going to run into trouble there if any of your input objects have properties with the same names, but for this specific case, you could try something like this:
$Output = New-Object -TypeName PSObject $temp = Test-ServiceHealth dagn1 $propertyNames = $temp | Get-Member -MemberType Properties | Select-Object -ExpandProperty Name ForEach($property in $propertyNames) { $value = foreach ($t in $temp) { $t.$property } $Output | Add-Member -MemberType NoteProperty -Name $property -Value $value } Write-Output $Output
Here, I went with Get-Member rather than .PSObject.Properties, but the code could have been written either way. The important part is that you’re fetching the properties of the items in the array (if $temp is an array), rather than the properties on a System.Array object.
I bow you, it got worked. I may need to time to understand it. If I have any doubt I will initiate the comms here. Thnx a lot Dave
Function Get-Uptime_Host([String]$ComputerName) # Function to calculate update of the given host. { ForEach($C in $Computername) { Write-Progress -Activity "Uptime" -Status 'InProgress' -PercentComplete 50 -CurrentOperation $("Calculating System Uptime for :"+$C) #sleep 1 Write-Verbose $("Uptime Calculation : "+"Calculating uptime for the host '"+$C+"'") Try { # The blow WMI command is to get the system uptime [Array]$Uptime_Temp+= Get-WmiObject -Class Win32_PerfFormattedData_PerfOS_System -ComputerName $C -ea 1 $Uptime = New-TimeSpan -Seconds $Uptime_Temp.SystemUpTime } catch # This will executed when the above command is unable to get the uptime of the given host. { Write-Verbose $("Uptime Calculation : "+"Host '"+$C.ToUpper()+"' has failed to get the uptime. Now proceeding to check whether the host is pingable or not?") $PingTest=Get-WMIObject -Query "select * from win32_pingstatus where Address='$H'" # Performing PING test. We are doing the ping test only when unable to get the uptime of the host because to increase performance of the script (ping test do delay for packet reply) If($PingTest.StatusCode -eq 0) # Status code =0 is a one of the standard value defined in Win32_PingStatus class. For more status codes information please visit https://msdn.microsoft.com/en-us/library/aa394350%28v=vs.85%29.aspx { Write-Verbose $("Uptime Calculation : "+"Host '" +($C) +"' is pingable but unable to get the uptime of the host.") Write-warning $("Host: '" +($C).ToUpper() +"' is pingable but unable to get the uptime of the host. Please do manual checks") # For your information the below are the standard status codes in Win32_PingStatus class } Else # This will get executed when ping status code is not '0' only. As I am not bather about any other status codes. { Write-Verbose $("Uptime Calculation : "+"Host '" +($C) +"' is NOT pingable.") Write-Host $("Host: '" +($C).ToUpper() +"' is NOT pingable. Please check the host") -foreg RED } } Write-Progress -Activity "Uptime" -Status 'Completed' -PercentComplete 100 #sleep 1 } Return [String]$Uptime # Returning the final output of this function to the function call location. } Function Test-SRVHealth([String]$S) { #************************** Coding starts here for Test-ServiceHealth *************************** Try { Write-Verbose 'Get-HealthCheck:: Test-ServiceHealth: Started' Write-Verbose $("Get-HealthCheck:: Test-ServiceHealth: Proceeding to get the service health for the host '"+$S+"'") $BlackHole = Test-ServiceHealth -Server $S -ErrorAction Stop $Health | Add-Member -MemberType NoteProperty -Name 'RolesFound' -Value $RolesFound For([int]$i=0;$i -lt $BlackHole.Count;$i++) { If($BlackHole[$i].Role -Contains 'Mailbox Server Role') { Write-Verbose $("Get-HealthCheck:: Test-ServiceHealth: Found Mailbox Server Role on the host '"+$S+"'") $Health | Add-Member -MemberType NoteProperty -Name $('MBX:RequiredServicesRunning') -Value $BlackHole[$i].RequiredServicesRunning $Health | Add-Member -MemberType NoteProperty -Name $('MBX:ServicesRunning') -Value $BlackHole[$i].ServicesRunning $Health | Add-Member -MemberType NoteProperty -Name $('MBX:ServicesNotRunning') -Value $BlackHole[$i].ServicesNotRunning [Array]$RolesFound = 'Mailbox Server Role' } If($BlackHole[$i].Role -eq 'Client Access Server Role') { Write-Verbose $("Get-HealthCheck:: Test-ServiceHealth: Found Client Access Server Role on the host '"+$S+"'") $Health | Add-Member -MemberType NoteProperty -Name $('CAS:RequiredServicesRunning') -Value $BlackHole[$i].RequiredServicesRunning $Health | Add-Member -MemberType NoteProperty -Name $('CAS:ServicesRunning') -Value $BlackHole[$i].ServicesRunning $Health | Add-Member -MemberType NoteProperty -Name $('CAS:ServicesNotRunning') -Value $BlackHole.ServicesNotRunning [Array]$RolesFound += 'Client Access Server Role' } If($BlackHole[$i].Role -eq 'Hub Transport Server Role') { Write-Verbose $("Get-HealthCheck:: Test-ServiceHealth: Found Hub Transport Server Role on the host '"+$S+"'") $Health | Add-Member -MemberType NoteProperty -Name $('HUB:RequiredServicesRunning') -Value $BlackHole[$i].RequiredServicesRunning $Health | Add-Member -MemberType NoteProperty -Name $('HUB:ServicesRunning') -Value $BlackHole[$i].ServicesRunning $Health | Add-Member -MemberType NoteProperty -Name $('HUB:ServicesNotRunning') -Value $BlackHole[$i].ServicesNotRunning [Array]$RolesFound += 'Hub Transport Server Role' } } If($Health."MBX:RequiredServicesRunning" -and $Health."CAS:RequiredServicesRunning" -and $Health."HUB:RequiredServicesRunning") { Write-Verbose $("Get-HealthCheck:: Test-ServiceHealth: All services for MBX,CAS and HUB servers are running on the host '"+$S+"'") $Health | Add-Member -MemberType NoteProperty -Name 'ServicesHealth' -Value 'Pass' } Else { Write-Verbose $("Get-HealthCheck:: Test-ServiceHealth: Some of the services for MBX,CAS and HUB servers NOT running on the host '"+$S+"'") $Health | Add-Member -MemberType NoteProperty -Name 'ServicesHealth' -Value 'Fail' } Write-Verbose 'Get-HealthCheck:: Test-ServiceHealth: Completed' } Catch { Write-Verbose $("Get-HealthCheck:: Test-ServiceHealth: Unable to get service health for the host '"+ $S+"'") Write-Error ("Unable to get the service health of the Server: '"+$S+"'") Write-Verbose 'Get-HealthCheck:: Test-ServiceHealth: Completed' } #************************** Coding ends here for Test-ServiceHealth *************************** } Function Get-WhoRanIt # Function to check who ran the script. { Write-Verbose "Who Ran The Script: Started." Write-Progress -Activity "Identifying" -Status 'Initializing' -ID 0 -PercentComplete 50 -CurrentOperation "Checking that who ran the script" $Global:WhoRanIt = $Null #Global Variable to catch who ran the script $WhoRanIt=$($ENV:UserName).ToUpper() Write-Verbose $("Who Ran The Script: '"+$WhoRanIt+"'"+" Ran the script") Write-Progress -Activity "Identifying" -Status 'Initializing' -ID 0 -PercentComplete 100 -CurrentOperation $($WhoRanIt+" Ran the script.") Write-Verbose "Who Ran The Script: Completed." Return [String]$WhoRanIt } Function Get-HealthCheck { [CMDLetBinding()] Param( [String]$Server # Function argument ) BEGIN { Try { Write-Verbose 'Get-HealthCheck:: Creating PSObject' $Health = New-Object -TypeName PSObject -ErrorAction Stop # Creating a PSObject type of variable to hold all the health check outputs in to it. If($Health -ne $null) { Write-Verbose 'Get-HealthCheck:: PSObject created' } } Catch # For some un-know reasons, unable to create the PSObject. The catch block is never expected to be run { Write-Verbose 'Get-HealthCheck:: Unable to create PSObject and quiting from Get-HealthCheck function.' Write-Error $("Unable to create PSObject") Break; # This function never proceeds if unable to create PSObject. } } PROCESS { $BlackHole = Get-WhoRanIt # Calling the function which fetches the user information that who ran the script. $Health | Add-Member -MemberType NoteProperty -Name "WhoRanIt" -Value $BlackHole # Adding 'WhoRanIt' value to the PSObject $Health | Add-Member -MemberType NoteProperty -Name "RanItFromTheHost" -Value $ENV:ComputerName # Adding 'RanItFrom' value to the PSObject. i.e., from ` # which Machine the script ran. $BlackHole = Get-Uptime_Host -ComputerName $Server $Health | Add-Member -MemberType NoteProperty -Name "Uptime" -Value $BlackHole # Adding 'uptime' value of the host to the PSObject Test-SRVHealth -S $Server # Calling the function which performs service health of the given host. } END { $Health } }