function - Use ping output to create a CSV -
i have written following code use ping command ping multiple computers, capture following values it:
- reply / request timed out etc.
- actual ip address of remote host
this function working fine however, if trying returned values csv unable so.
# declaration of function name , expected parameters function ping-check{ [cmdletbinding()] param ( [parameter(mandatory=$true, valuefrompipeline=$true, valuefrompipelinebypropertyname=$true, helpmessage='enter name of remote host.')] [string]$objcompname ) begin{ # setup process startup info $pinfo = new-object system.diagnostics.processstartinfo $pinfo.filename = "ping.exe" $pinfo.arguments = " -a " + $objcompname + " -n 2" $pinfo.useshellexecute = $false $pinfo.createnowindow = $true $pinfo.redirectstandardoutput = $true $pinfo.redirectstandarderror = $true } process{ $p = new-object system.diagnostics.process $p.startinfo = $pinfo $p.start() | out-null $p.waitforexit() # redirect output $stdout = $p.standardoutput.readtoend() $stderr = $p.standarderror.readtoend() } end{ write-output "stdout: $stdout" write-output "stderr: $stderr" write-output "exit code: " + $p.exitcode } }
there test-connection
cmdlet available in powershell returns object array properties can use. if wan't stick attempt, aware have parse output yourself.
Comments
Post a Comment