views:

20

answers:

1

I am trying to write a powershell script that will take in a text file (or xml file or whatever I want ti to be) with a list of servername and some service names on that server to stop. I can get powershell to read in a line from a text file, but I can't figure out how to get powershell to import the data into seperate variable for me to pass on to other functions. if this were arguemnts it seems trivial but there must be a simple way to do that rather than me using a regex on each line.

+1  A: 

Consider the XML below:

$xml = [xml]@"
<Servers>
    <Server name="SERVER1">
        <Process>Process1</Process>
        <Process>Process2</Process>
    </Server>
    <Server name="SERVER2">
        <Process>Process3</Process>
    </Server>
</Servers>
"@

You could process the $xml variable like so:

foreach ($server in $xml.Servers.Server) {
    foreach ($process in $server.Process) {

        # Execute your kill script here.

    }
}

For a kill script on remote machines, you could use the Invoke-Script cmdlet (requires PSRemoting be enabled), or PsExec, or WMI, whichever you're most comfortable with. The $server and $process variables will be required to execute the kill against the correct process on the correct server.

kbrimington

related questions