Z value shader

I want to create a surface shader that outputs a grayscale value based on the pixel’s z value. How can I do that?

Here you go.

To more experiences guys : is it possible to retrieve some depth information in vertex shader with position.w * frustrumLength ?