Monday, 9 September 2013

Powershell erroring out of invoke-command with network connection could not be found

Powershell erroring out of invoke-command with network connection could
not be found

Since I find answers to most of my questions on this site, I'm hoping
someone has my answer tucked away in the recesses of their brain cells and
is willing to extract it for me. I have searched this site and the
internet and have not had any success. Here's the situation.
I'm executing a Powershell script on ServerA that looks like this:
"Creating a Session"
$session = New-PSSession –ComputerName $machine -authentication credssp
-credential $cred -Name Upgrade
""
"Launching upgrade installation on $Machine"
Invoke-Command -session $session -ScriptBlock {param ($mc, $un, $pw)
powershell.exe -executionPolicy bypass c:\PXInstall\AutoUpdate.ps1
-Machine $mc -userName $un -password $pw} -ArgumentList $Machine,
$DomainAccount, $DomainAccountPassword
""
"Closing Session"
$rmSession = get-pssession -name Upgrade
#remove-pssession -session $session
remove-pssession -session $rmSession
I have created the $cred variable previously. I have to use the credssp
authentication because the autoupdate.ps1 file launches an installation
powershell script that has to authenticate the domainaccount and
domainaccountpassword that I'm passing in.
This all works fine. The autoupdate.ps1 script is launched and it does a
couple things before the installation, the installation proceeds normally,
and then I do a couple of things after the installation, all this in the
autoupdate.ps1 file.
The Problem
I have the above code in a try catch block. As soon as the autoupdate.ps1
file exits it drops to my catch block with the error "The network
connection could not be found". I'm assuming it's failing in the
autoupdate.ps1 file, see below for other options and more info.
As you can see the very next command in the main script is "" to output a
blank line. The main script uses start-transcript to capture the output to
a file written locally. I don't know if it makes it to this line and is
throwing an error trying to write the comment, or if it errors in
autoupdate.ps1. Note that the installation script that is launched from
autoupdate.ps1 also does a start-transcript and stop-transcript. I see the
start and stop in my log file. I know this works fine if I don't use a
credssp session as I run the same installation script without the session
and it works fine (different setup mode, same installation script, same
start and stop-transcript). Could the session be interacting with the
start-transcript and stop-transcript in the main?
If I comment out the invoke-command line it will continue just fine.
In the autoupdate.ps1 file there are a couple of errors generated during
the installation script, but they are ignored with erroractionpreference =
"silentlycontinue". After the installation is finished I output the errors
and then clear the error variable with $error.clear(). After my last
command in autoupdate.ps1 I check for errors and there are none. None of
the errors generated from the installation script report the error I get
in the catch block.
Which leads me to think the error is happening when trying to write that
output line in the main script that comes after the invoke-command line.
But why?
As my last test before posting this I moved the writing to the log file
after the remove-psssession command and got the same error.
Any help or troubleshooting steps are greatly appreciated. I will be
limiting the actions in autoupdate.ps1 just to see if it's one of the
actions in there, but it seems unlikely.
Scott
Here's the full sequence of events:
Main script starts.
start-transcript
<bunch of stuff that if removed doesn't make a difference>
try
session created
invoke-command
<stuff in autoupdate.ps1>
install ps1 file launched
start-transcript
<lots of stuff>
stop-transcript
<more stuff in autoupdate.ps1>
<no errors in $error variable when autoupdate.ps1 exits>
blank line output
comment output
remove session command
catch
errors out here and stops the transcript created in main script

No comments:

Post a Comment