Do Functions execute differently than in-line code?


i running issue, , need understanding how powershell handles things.  have function defined.

function checkucsstate   {      $error.clear()        try      {  	$ucsstate = get-ucsstatus -erroraction silentlycontinue          $ucsconnected = $true  	}      catch  	{          write-log "lost ucs connection ucs manager domain '$($ucs)'" "warning"  		write-log "  error equals: $error" "warning"  		disconnect-ucs  		  		write-log  "retrying login ucs manager domain '$($ucs)' ..." "normal"  		$mycon  = connect-ucs -name $ucs -credential $ucscred -erroraction silentlycontinue     	    $error.clear()          try          {  	        $ucsstate = get-ucsstatus -erroraction silentlycontinue              write-log "successfully re-connected ucs manager domain" "normal"              $ucsconnected = $true  	    }          catch  		{  			write-log "failed create session ucs manager domain: '$($ucs)'" "warning"  			write-log "  error equals: $error" "warning"              $ucsconnected = $false          }      }      return $ucsconnected  }  

i think purpose of ucs cmdlets pretty obvious.  don't think giving me problem.  write-log function wrote gives me option of writing a log file on disk or displaying output console debugging purposes.

i have powershell command window (not using ise) that has variables used in script properly defined.  before run test, disconnect from the ucs service can test that this function reconnect. 

the issue have if cut , paste contents of function powershell window, starting try command , ending proper closing bracket, commands execute expected.  tells me lost ucs connection, gives me reason why, tells me trying reconnect, tells me reconnected, , returns status of $true in $ucsconnected variable.  can test connection service , find connected.

then cut , paste full function definition same powershell window.  follow same steps of disconnecting before test. execute function $ucsstatus = checkucsstate.  exact same messages displayed, when test connection service, tells me not connected.

is there inherently different in way powershell executes in function versus in-line code?  example, function run child process when function exits, process teardown automatically disconnect service?


. : | : . : | : . tim

change of these  "-erroraction silentlycontinue" "-erroraction stop".  see error.

try/catch not trap when "silentlycontinue" in force.


\_(ツ)_/




Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

some help on Event 540

WMI Repository 4GB limit - Win 2003 Ent Question

Event ID 1302 (error 1307) DFS replication service encountered an error while writing to the debug log file