replacing values in property

This topic contains 3 replies, has 2 voices, and was last updated by  Wei-Yen Tan 1 week, 5 days ago.

  • Author
    Posts
  • #79210

    Wei-Yen Tan
    Participant

    hi ,

    I have an object that I would like to change some values for:

    example:

    
    $object = New-Object -TypeName psobject -property @{
        ScheduledDowntime = $ThrukhostService.host_scheduled_downtime_depth | Select-Object -Unique
        InCheckPeriod = $Thrukhost.in_check_period
        InNotificationPeriod	= $Thrukhost.in_notification_period
        NotificationPeriod = $Thrukhost.notification_period
    }
    
    Write-output $object
    

    The property value will produce a value of either 0 or 1. I would like to translate that to $true or $false.
    One way is to manually go through each one and do a replace of 1 or 0.

    What I would like to do is be a bit smarter and iterate through the properties and change it based on what is already in the property value.

    I read through some documentation with what out there on Google, and help on Powershell...but its a bit unclear how to do that.

    TIA

  • #79214

    Olaf Soyk
    Participant

    There is actually no question. I your example you just create an object based on one other object. If you like to change the value of a property based on its "value" you simply read it, make a conditiponal check and change it accordingly. Then you write it back where you got it.

    I would like to reccomend for you to start to learn the basics of Powershell. Her you can find some great resources to start with:
    https://powershell.org/forums/topic/using-csv-files/

  • #79220

    Wei-Yen Tan
    Participant

    Thanks for the reply @olaf. I am going to revise what I m going to do with this object.

    I have already added a type to this object, so I am going to add in typedata with a scriptproperty. I will then create a default view adding the scriptproperties to the default view.

  • #79561

    Wei-Yen Tan
    Participant

    Just to follow up on this. This is what I did:

    Update-TypeData -TypeName thrukhost.info -DefaultDisplayPropertySet HOSTNAME, services, Hostflapping, Ipaddress, Uptime, NextScheduledActiveCheck, ScheduledDowntime, ObsessHost -DefaultDisplayProperty HOSTNAME -Force
    
    Update-TypeData -TypeName 'Thrukhost.info' -MemberType ScriptProperty -MemberName "Hostflapping" -Value {
    	if ($this.Is_flapping -eq '1') { return $true }
    	elseif ($this.Is_flapping -eq '0') { return $false }
    }
    
    Update-TypeData -TypeName 'Thrukhost.info' -MemberType ScriptProperty -MemberName "ScheduledDowntime" -Value {
    	if ($this.Scheduled_Downtime -eq '1') { return $true }
    	elseif ($this.Scheduled_Downtime -eq '0') { return $false }
    	
    }
    
    Update-TypeData -TypeName 'Thrukhost.info' -MemberType ScriptProperty -MemberName "NotificationsEnabled" -Value {
    	if ($this.Notifications_Enabled -eq '1') { return $true }
    	elseif ($this.Notifications_Enabled -eq '0') { return $false }
    	
    }
    
    Update-TypeData -TypeName 'Thrukhost.info' -MemberType ScriptProperty -MemberName "ActiveChecks" -Value {
    	if ($this.Active_Checks -eq '1') { return $true }
    	elseif ($this.Active_Checks -eq '0') { return $false }
    	
    }
    
    Update-TypeData -TypeName 'Thrukhost.info' -MemberType ScriptProperty -MemberName "PassiveChecks" -Value {
    	if ($this.Passive_Checks -eq '1') { return $true }
    	elseif ($this.Passive_Checks -eq '0') { return $false }
    	
    }
    
    Update-TypeData -TypeName 'Thrukhost.info' -MemberType ScriptProperty -MemberName "ObsessHost" -Value {
    	if ($this.Obsessing -eq '1') { return $true }
    	elseif ($this.Obsessing -eq '0') { return $false }
    	
    }
    
    Update-TypeData -TypeName 'Thrukhost.info' -MemberType ScriptProperty -MemberName "FlapDetection" -Value {
    	if ($this.Flap_Detection -eq '1') { return $true }
    	elseif ($this.Flap_Detection -eq '0') { return $false }
    	
    }
    
    

    This way I have created a predefined view. I have created script properties of all the numbers that I want.(This can be added to).

You must be logged in to reply to this topic.