How to update variable product stock status with code

Most parts of your code were unnecessary.

Assuming you get the correct set of posts with your get_posts(), you can just loop through those posts, and update the meta.

You also had a confusion of the variables and objects in your loop, and you tried to update/get information passing a post object instead of an $id.

$children = get_posts( array(
    'post_parent'   => $posts,
    'posts_per_page'=> -1,
    'post_type'   => 'product_variation',
    'post_status'  => 'publish'
) );

$out_of_stock_staus="outofstock";

foreach ( $children as $thischild ) {

    update_post_meta( $thischild->ID, '_stock_status', wc_clean( $out_of_stock_staus ) );

}