Powershell Workflow: Using if/else statement? -
i trying use workflow in order make script run faster running ping on several machines in parallel. getting stuck trying use if/else statement within workflow , adding log entries based on results of ping. right now, not writing logs based on ping results. assistance appreciated. open suggestions different methods this, long not involve using module. want stick powershell code (i not have permissions install modules on server running). thank you! here current code:
<# .notes =========================================================================== created on: 10/6/2016 8:06 created by: organization: filename: get-mposofflineprinters.ps1 =========================================================================== .description #> $logfile = "d:\logs\mposprinterpinglog.txt" $offlineprinters = "d:\reports\mpos\mposofflineprinters.txt" if (test-path $logfile) {remove-item $logfile} if (test-path $offlineprinters) {remove-item $offlineprinters} add-content $logfile "setting local path" $localpath = "c:\temp\mpos" add-content $logfile "gathering server list" $serverlist = (get-adcomputer -filter "name -like 'q0*p30' -or name -like 'q0*p32'" -searchbase "ou=print,ou=prod,ou=pos,dc=company,dc=net").name | sort-object | out-file c:\temp\mpos\mposprintservers.txt $serverlistpath = "c:\temp\mpos\mposprintservers.txt" #retrieve list of mpos print servers text file , set $servernames add-content $logfile "compiling text file" $servernames = get-content -path $serverlistpath #iterate through each of server names foreach ($servername in $servernames) { add-content $logfile "processing $servername" #check if server online before doing remote command if (test-connection -computername $servername -quiet -count 1) { add-content $logfile "$servername online" #copy config file mpos print local server processing $timestamp1 = (get-date -format g) add-content $logfile "$timestamp1 - copying xml file server local path" $configpath = "\\$($servername)\c$\programdata\microsoft\point of service\configuration\configuration.xml" copy-item $configpath $localpath #process xml file parse ip addresses ping $timestamp2 = (get-date -format g) add-content $logfile "$timestamp2 - processing xml file $servername parse data csv" $xml = [xml](get-content c:\temp\mpos\configuration.xml) $printernames = $xml.selectnodes('//pointofserviceconfig/serviceobject/device') | foreach {new-object -typename psobject -property @{logicalname=$_.logicalname.name}} $printerips = $xml.selectnodes('//pointofserviceconfig/serviceobject/device') | foreach {new-object -typename psobject -property @{hardwarepath=$_.hardwarepath}} workflow test-wfconnection { param( [string[]]$printerips ) foreach -parallel ($printerip in $printerips) { $pingableip = $printerip.hardwarepath if (test-connection -computername $pingableip -quiet -count 1 -erroraction silentlycontinue) { $timestamp3 = (get-date -format g) add-content -path $logfile -value ("$timestamp3 - $servername, $pingableip online , pingable") } #if (test-connection $pingableip -quiet -count 1 -erroraction silentlycontinue) { else { $timestamp3 = (get-date -format g) add-content -path $offlineprinters -value ("$timestamp3 - $servername, $pingableip offline!") } #else } #foreach -parallel ($printerip in $printerips) { } #workflow test-wfconnection { test-wfconnection -pscomputername $printerips } #if (test-connection -computername $servername -quiet -count 1) { else { add-content $logfile "$servername offline!" } #else } #foreach ($servername in $servernames) {
Comments
Post a Comment