Function for list services on multiple servers


hi

i'm trying write function list services status 'auto' on multiple servers. have that:

function running_services{      param(      [parameter(mandatory=$true,                 valuefrompipeline=$true,                 helpmessage='input path file server list')]      [validatescript({test-path $_ -pathtype 'leaf'})]      $input_patch      ,      [parameter(mandatory=$true,                 valuefrompipeline=$true,                 helpmessage='input path file result written')]      [validatescript({      test-path $_ -pathtype 'leaf'      })]      $output_patch      )      $servers = get-content $input_patch            foreach($s in $servers)      {          $service = write-output $s          $service += gwmi win32_service -computername $s | {$_.startmode -eq "auto"} | select-object name,state,status | sort state      }      $service | out-file $output_patch  }

and couple questions:

1. want verify if output file exist if not create made a:

[parameter(mandatory=$true,                 valuefrompipeline=$true,                 helpmessage='input path file result written')]      [validatescript({      if(!(test-path $_ -pathtype 'leaf')){new-item $output_patch -type file}      })]      $output_patch

but when try run got: running_services : cannot validate argument on parameter 'output_patch'. cannot bind argument parameter 'path' because null.r

2. how write list file. it's not working, writing host names.

1. won't work that. use proper variable test, reason - not file creation... ;) use $_ there too:

function test-mypath { param (     [parameter(mandatory = $true)]     [validatescript({         if (!(test-path $_ -pathtype leaf)) {             new-item -itemtype file -path $_ -force         }     })]     [string]$path )     'foo' | add-content @psboundparameters }

2. if building strings may idea force output of command become one:

$s = 'localhost'  $service = write-output $s $service += gwmi win32_service -computername $s |      {$_.startmode -eq "auto"} |      select-object name,state,status |      sort state | out-string   


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Group Policy Event ID 1058 Error Code 1326 (The user name or password is incorrect)

Suspicious event log Event ID: 4905

DCOM received error "2147746132" from...