Powershell script execution error

Hi,

I am getting an error when the octopus tentacle is executing a Deploy.ps1 script. The Deploy script call a number of custom methods in a custom powershell module.
I can execute the script via powershell running as admin on the server where the tentacle is installed and the script executes fine.
I have tried the following each time the scripts errors.

Running the tentacle interactively as my domain account (I am a domain admin and an admin on the server).
Running the tentacle as the default LocalSystem account (default).
Running the tentacle as a service account with local admin and domain admin privileges.

See below error.

ERROR: Test-Account : Exception calling “FindByIdentity” with “3” argument(s): “Could
2013-12-11 00:11:33 INFO [Deploy Script] ERROR: not load file or assembly ‘Microsoft.PowerShell.Commands.Management’ or one of
2013-12-11 00:11:33 INFO [Deploy Script] ERROR: its dependencies. The system cannot find the file specified.”
2013-12-11 00:11:33 INFO [Deploy Script] ERROR: At C:\Octopus\Applications\Dev\AMS.Core.Host\1.0.48_8\Deploy.ps1:16 char:1
2013-12-11 00:11:33 INFO [Deploy Script] ERROR: + Test-Account -Account $ServiceAccount -Password $ServiceAccountPassword
2013-12-11 00:11:33 INFO [Deploy Script] ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2013-12-11 00:11:33 INFO [Deploy Script] ERROR: + CategoryInfo : NotSpecified: (:slight_smile: [Test-Account], MethodInvocati
2013-12-11 00:11:33 INFO [Deploy Script] ERROR: onException
2013-12-11 00:11:33 INFO [Deploy Script] ERROR: + FullyQualifiedErrorId : FileNotFoundException,Test-Account
2013-12-11 00:11:33 INFO [Deploy Script] ERROR:

Here is the method that is being called.

@@@
Function Test-Account() {
Param (
[Parameter(Mandatory=$true)] [String] $Account,
[Parameter(Mandatory=$true)] [String] $Password
)
Add-Type -AssemblyName System.DirectoryServices.AccountManagement

 $domainContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Domain)
$foundUser = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($domainContext, [System.DirectoryServices.AccountManagement.IdentityType]::SamAccountName, $Account)

if ($foundUser -eq $Null) {
	Write-Error "User $Account not exist in AD"
}

$contextType = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$principalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($contextType)
if (!$principalContext.ValidateCredentials($Account, $Password)) 
{
	Write-Error "Service Account Password is invalid for $Account"
}

}
@@@

This is the line of script that is throwing the error.
@@@
$foundUser = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($domainContext, [System.DirectoryServices.AccountManagement.IdentityType]::SamAccountName, $Account)’
@@@

Environment information

Server: Windows Server 2008 R2.

I have tried this with PS v3 and PS v4.

$psversiontable output

Name Value


PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.18052
BuildVersion 6.3.9600.16406
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2

Cheers
Adam

Hi Adam,

If you dump $psversiontable when
running the script under Tentacle, what version does it list? I think
Tentacle may be running an older version of PowerShell.

Regards,

Paul Stovell
Octopus Deploy
W: octopusdeploy.com | T: @octopusdeploy http://twitter.com/octopusdeploy

Hi Paul,

Please see initial post the $psversiontable output is included for the tentacle. It was output from the module method throwing the error when run by the tentacle. I have pasted in below again.

$psversiontable output

Name Value

PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.18052
BuildVersion 6.3.9600.16406
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2

Cheers
Adam

Hi Adam,

Thanks and sorry, I thought that output was from running PowerShell interactively. Interesting that they are the same.

Do you get the same error if you run it from an admin command prompt using tentacle.exe run-script? Instructions on this can be found at: http://octopusdeploy.com/blog/testing-powershell-scripts

Can you also try it with/without putting this at the top of the script:

Write-Host "test"
Get-Module -ListAvailable | Import-Module

Paul

Hi Paul,

Thanks for your reply. Sorry it has taken awhile to get back to you but I am only on this client site part time.

I added the powershell code you suggested to the three scripts that call the module that was failing.
@@@
Write-Host "test"
Get-Module -ListAvailable | Import-Module
@@@

Script A deploys a website to Server A
Script B deploys a windows service to Server B
Script C also deploys a windows service to Server B

The scripts for Script B and C are identical except for some inline variables that name the windows service etc

I deployed the project using the Octopus Deploy web interface and all the scripts executed without error. I was tinkering with the scripts and Script C started to fail with the that I started the thread with. I rolled back the script to the last working version but it continued to fail. Note Script B which runs on the same server and is almost identical was still working.

then ran script B and C using the tentacle.exe run-script with an admin cmd prompt. Both scripts failed with the error that I started the thread with.

I then deployed the project using the Octopus Deploy web interface with the last version of the project (where script A and B were successful). This time only script A was successful.

I restarted the tentacle on Server B where Script B and C run on and reran the last deployment again using the web interface. Same result as before only Script A on Server A was successful.

I rebooted Server B and reran the last deployment again using the web interface. This time all scripts executed successfully .

I then I then ran script B using the tentacle.exe run-script with an admin cmd prompt. Script B failed with the error that I started the thread with.

I then deployed the project using the Octopus Deploy web interface with the last version of the project (where script A ,B , C were successful). This time script A and C were successful with script B failing the error that I started the thread with. I rebooted the server again and all scripts run without error.

Note: The tentacles ATM arerunning as a service account with local admin and domain admin privileges.

Any way hope that provides some insight for you as to what might be causing the error.

Cheers
Adam

It sounds like this might be the classic “Directory Services LASTERROR” bug - in a nutshell, System.DirectoryServices throws when an error has been recorded previously, even if it is unrelated to whatever DS is doing (and may have been handled elsewhere).

The issue’s specific to Win 7 and 2k8 R2, Microsoft provides a hotfix here:

http://support.microsoft.com/kb/2683913

Can you give this a shot and let us know if there are any further issues?

Cheers
Nick

Hi Nick,

Thanks for you reply.

Applying the hotfix seems to have fixed the error.

Thanks again for helping out.

Cheers
Adam

Almost the same issue running v3.3.8 and PoSHv4:

$serviceSite = Get-Item “IIS:\sites$siteName” -ErrorAction “SilentlyContinue”

Get-Item : Could not load file or assembly
15:43:40 ‘Microsoft.PowerShell.Commands.Management’ or one of its dependencies. The system cannot find the file specified.

Workaround:

try
{
	$serviceSite = Get-Item "IIS:\sites\$siteName" -ErrorAction "SilentlyContinue"
}
catch [System.IO.FileNotFoundException]
{
	# Try again (workaround for System.IO.FileNotFoundException issue)
	$serviceSite = Get-Item "IIS:\sites\$siteName" -ErrorAction "SilentlyContinue"
}