James Truher MSH Program Manager jimtru@microsoft.com

tunisianbromidrosisInternet and Web Development

Feb 5, 2013 (4 years and 5 months ago)

324 views

1

Building Manageable Apps:
Admin Scripting & Automation

Jeffrey Snover

Management Architect

Microsoft Corporation

jsnover@microsoft.com

Session Code:

ARC
-
334

2

Make the connection

Kernel Mode

Framework

Models

Base Services

Kernel

Hardware Abstraction Layer

Base Operating System Services

CLR

Health & Instrumentation
















Management Services

System.Management.Instrumentation

System.Management

System.Diagnostics

System.Diagnostics.Events

System.Management.Monitoring

Task & Automation










System.Management.TaskScheduler

System.Management.Automation

System.Management.TaskScheduler.UI

Settings & Configuration




System.Configuration.Settings

3





Windows

DSI Architecture (ARC230)

Design for Operations

Dynamic System Services





SDM

Store

Dynamic Data Center

STORAGE

SERVERS

NETWORKING

Settings
ARC333

Tasks
ARC334

Health ARC332

Hardware

Managed

Node

Managed

System

Management

Tools

Dev

Tools

Local

Node
Mgmt

System Level Management

Remote

Node
Mgmt

Your Application

Your

System

Definition

SDM Service

4

Task
-
Based Management

Abstractions match activities of the user

Add a user


canonical example

Create account in AD

Create home directory

Add to Groups

Customer
-
specific setup



Surfaced to enable automation

Requires command line access!

5

Introduction

Weak cmd shell

Weak language

spotty coverage

GUI focus

Hard to automate

SDK Focus

Programmers


Foundation for task
-
based management

Focused on power
users and admins

Provides:

Interactive shell

Cmdlets

Utilities

Scripting language

Remote scripting

Solution: MSH

Problem

6

MSH Elements

Core concepts

Pipelines and utilities

Parameters and confirmation

Records and errors

Navigation

7

Core Concepts

Command line scripting language

Best of sh/ksh, Perl/Ruby, DCL/CL

Commands are classes (Cmdlets)

Hosting model

8



using



public class

{


public override void


{


(Process.GetProcesses());


}

}

System.Management.Automation

Commands Are Classes

GetPs : Cmdlet

[CmdletDeclaration("get", "ps")]

WriteObjects

StartProcessing()

9

Cmdlets Or …

What Do I Get For 5

Lines Of Code?



James Truher

MSH Program Manager

jimtru@microsoft.com

10

How It Works

AutoRegistration


Parser maps to your DLL


MSH manages the DLL and Class lifecycle


11

Pipeline And Utilities
Concepts

1.
Pipelines are Cmdlets passing

structured objects


2.
Reflection
-
based utility Cmdlets

12

Pipeline & Utilities Or…

Was That All I Got For 5
Lines Of Code?



James Truher

MSH Program Manager

jimtru@microsoft.com

13

How It Works

get/process | where “handlecount

gt 500” |

sort handlecount | format/table

GetProces
s

Class

Common MSH Parser

MSH Pipeline Processor

Where
Class

Sort

Class

Format

Class

14

CLIs And GUIs

Not either/or

Admins need more than GUIs

typically deliver

Planning & review

Guaranteed consistency

Massive productivity

You need to support both

Background

15

Composable Management

Traditional Model

A | B | C

Tight coupling between

Parse, Get, Process, Output

Relies on parsing text

.NET allows us to do better

Finer grain pipeline

Objects instead of text


Background

16

Cost

Benefits

# of Functions

Traditional

Model

Economics

Costs:


Dev


Test


Training


MSH Common Engine

combined with Utility Cmdlets

New
Model

Background

17

Glide Path

ShowScript

Reflection

Background

GUI

Cmd

VS

18

MMC or…

How Do GUIs Work

With This?





Mark Hong

MMC Dev Lead

markhong@microsoft.com

19

Parameter And Confirmation

Concepts

Common parser


Driven by fields/properties + attributes


Use Confirmation model

20

Parameters And Confirmation

[CommandDeclaration("
stop
", "
ps
")]

public class
StopPs
: Cmdlet

{





public string
ProcessName
;


public override void
ProcessRecord
()


{


Process [ ]ps;


ps = Process.GetProcessesByName(
ProcessName
);


foreach (Process p in ps)


{



{


p.Kill
();


}


}


}

}

[
ParsingMandatoryParameter
]

[
ParsingPromptString
(“Name of the process")]


if (
ConfirmProcessing
(p.ProcessName))

21

Parameters/Confirmation
Or… So What Do Those 3
Lines Of Code Get Me?



James Truher

MSH Program Manager

jimtru@microsoft.com

22

How It Works

Parameter Attributes

PrerequisiteMachineRole

PrerequisiteUserRole

PrerequisteScript

PrerequisiteUIType

ParsingParameter

ParsingAllowPipelineInput

ParsingParameterMapping

ParsingVariableLengthParameterLi
st

ParsingInteraction

ParsingPasswordParameter

ParsingPromptString

ValidationRange

ValidationLength

ValidationCount

ValidationFileAttributes

ValidationNetworkAttribute

ValidationPattern

ExpandWildcards

Predicates


Parsing


Data Validation


Data Generation

Data Presentation

23

How It Works

public bool
ConfirmProcessing
(string
Action)

{














}

if (Confirm)

{ return Prompt(Action + “Continue [y/n/!]”);

}

if (Whatif)

{ WriteLine(“# “ + Action);


return false

}

if (Confirm)

{ return Prompt(Action + “Continue [y/n/!]”);

}

if (Verbose)

{ Writeline(Action);


return true;

}

if (Whatif)

{ WriteLine(“# “ + Action);


return false

}

Pseudo
-
code

24

Records And Errors Concepts

1.
MSH is Record oriented

Records come from the Cmdline or

the PipeLine

Parameters define or contribute to a record

Pipeline objects are processed into Records

2.
Errors are first class citizens

Policy driven error handling

25

[CommandDeclaration("format", "ps")]

public class FormatPs: Command

{


public string
ProcessName
;




public int
HandleCount
;


public override void
StartProcessing
()


{

WriteObject(“Caution


data may be false”);


}










public override void
EndProcessing
()


{

WriteObject(“Done”));


}

}

Record Orientation


public override void
ProcessRecord
()


{





WriteObject(
ProcessName

+ “:” +
HandleCount
);


}


public override void
ProcessRecord
()


{





WriteObject(
ProcessName

+ “:” +
HandleCount
);


}

if (record++ > 5 && record < 10)


WriteErrorObject(CurrentPipelineObject,



new
Exception
("Invalid record"));

[
ParsingAllowPipelineInput
]

[
ParsingAllowPipelineInput
]

26

Records And Errors Or …

Why This Isn’t Like Your
Dad’s Shell



James Truher

MSH Program Manager

jimtru@microsoft.com

27

How It Works

StartProcessing()


for each object Pipeline



Convert object into a record


Apply command line params



ProcessRecord()


EndProcessing()

28

Ubiquitous Navigation

FileSystems are easy to use

Navigation and manipulation are universal

Other stores are hard

Require domain
-
specific utilties and concepts


How do we make other stores easy?

Interact with them like FileSystems

29

Navigation Provider Concepts

1.
You Subclass and we provide the Cmdlets

Dir, cd, pushd, popd, cat, delete, rename,

copy, clear,


2.
Providers need to be registered


3.
Requests to providers are stateless and use
absolute names

30

Navigation Provider

[
ProviderDeclaration
("REG", "Registry",

ProviderCapabilityFlags.None)]

public class RegistryProvider :
NavigationCmdletBase

{


protected
override

void
GetItem
(string path)


{


RegistryKey key = GetRegkeyForPath(path, false);



if (key == null)


{
WriteErrorObject
(path,


new ArgumentException("does not exist"));


}


WriteObject
(key);


}


....

}


31

Navigation Provider


Or

What Whackball Would Treat

Registry Like A Filesystem?





James Truher

MSH Program Manager

jimtru@microsoft.com

32

How It Works

MSH Engine

Core Command Base Classes

Core Commands

(get, set, rename, move, push, pop, …)

FileSys

Provider

Cmdlet

Cmdlet

Cmdlet



Registry

Provider

AD

Provider

33

Longhorn Command Shell
(MSH) Preview

Available on
http://betaplace.com

Use the guest account:
mshPDC

Logon and password emailed within

24 hours

Download bits, SDK, samples, private
newsgroup and a feedback/bug reporting
environment.

34

Call To Action

1.
Write managed code interfaces to
your services and objects

2.
Design your task model

3.
Write Cmdlets for invariant actions

4.
Write MSH scripts for actions which
vary between customers

5.
Write Navigation providers to expose
namespaces

35

HAPPY
BIRTHDAY

To

JIM TRUHER

36

© 2003
-
2004 Microsoft Corporation. All rights reserved.

This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.