将 Powershell 模块导入为 TeamCity 上的 LocalSystem 帐户

Importing a Powershell Module as LocalSystem Account on TeamCity

我 运行 在我的个人资料下使用命令,而 TeamCity 运行 使用相同的命令。

> Import-Module $root\packages\fsm.buildrelease.*\tools\modules\BuildDeployModules -Force

如果我手动 运行 脚本,构建会按预期启动。如果我让 TeamCity 执行脚本,它会吐出以下内容。

Import-Module : The specified module 'C:\BuildAgent\workeb7c2fdfcfc0af\packages\fsm.buildrelease.*\tools\modules\BuildDeployModules' was not loaded because no valid module file was found in any module directory.

我已经双重和三次验证该模块存在于该位置。我已经检查了我的模块并在 param 之前添加了 [cmdletbinding()],但它似乎并没有解决这个问题。

这很令人沮丧,因为它没有说 "which" 模块正在获取传入的无效参数。


$fsmbrVersion = "1.1.1" # contains the current version of fsm.buildrelease

Write-Host "`nfsm.buildrelease version $fsmbrVersion `nCopyright ($([char]0x00A9)) Future State Mobile Inc. & Contributors`n"

Push-Location $psScriptRoot

. .\Add-HostsFileEntry.ps1
. .\Add-IISHttpVerb.ps1
. .\Add-IISMimeType.ps1
. .\Add-LoopbackFix.ps1
. .\ApplicationAdministration.ps1
. .\AppPoolAdministration.ps1
. .\Approve-Permissions.ps1
. .\Assert-PSVersion.ps1
. .\EntityFramework.ps1
. .\Expand-NugetPackage.ps1
. .\Expand-ZipFile.ps1
. .\Format-TaskNameToHost.ps1
. .\Get-EnvironmentSettings.ps1
. .\Grunt.ps1
. .\Helpers.ps1
. .\Install-WebApplication.ps1
. .\Invoke-Deployment.ps1
. .\Invoke-DeployOctopusNugetPackage.ps1
. .\Invoke-ElevatedCommand.ps1
. .\Invoke-ExternalCommand.ps1
. .\Invoke-Using.ps1
. .\MSBuild.ps1
. .\Nuget.ps1
. .\nUnit.ps1
. .\Set-IISAuthentication.ps1
. .\Set-IISCustomHeader.ps1
. .\SiteAdministration.ps1
. .\Specflow.ps1
. .\SqlHelpers.ps1
. .\Test-PathExtended.ps1
. .\Test-RunAsAdmin.ps1
. .\TextUtils.ps1
. .\Update-AssemblyVersions.ps1
. .\Update-JsonConfigFile.ps1
. .\Update-XmlConfigFile.ps1
. .\WindowsFeatures.ps1
. .\xUnit.ps1

Pop-Location

Export-ModuleMember `
    -Alias @(
        '*') `
    -Function @(
          'Add-HostsFileEntry',
          'Add-IISHttpVerb',
          'Add-IISMimeType',
          'Add-LoopbackFix',
          'Approve-Permissions',
          'Assert-That',
          'Assert-PSVersion',
          'Confirm-ApplicationExists',
          'Confirm-AppPoolExists',
          'Confirm-SiteExists',
          'Exec',
          'Expand-NugetPackage',
          'Expand-ZipFile',
          'Format-TaskNameToHost',
          'Get-Application',
          'Get-Applications',
          'Get-AppPool',
          'Get-AppPools',
          'Get-DatabaseConnection',
          'Get-EnvironmentSettings',
          'Get-Site',
          'Get-Sites',
          'Get-TestFileName',
          'Get-WarningsFromMSBuildLog', 
          'Get-WindowsFeatures',
          'Install-WebApplication',
          'Install-WindowsFeatures',
          'Invoke-BulkCopy',
          'Invoke-DBMigration',
          'Invoke-Deployment',
          'Invoke-DeployOctopusNugetPackage',
          'Invoke-ElevatedCommand',
          'Invoke-EntityFrameworkMigrations',
          'Invoke-ExternalCommand',
          'Invoke-FromBase64', 
          'Invoke-GruntMinification',
          'Invoke-HtmlDecode', 
          'Invoke-HtmlEncode',
          'Invoke-KarmaTests',
          'Invoke-MSBuild',
          'Invoke-Nunit',
          'Invoke-NUnitWithCoverage'
          'Invoke-SpecFlow',
          'Invoke-SqlFile',
          'Invoke-SqlStatement',
          'Invoke-ToBase64', 
          'Invoke-UrlDecode', 
          'Invoke-UrlEncode', 
          'Invoke-Using',
          'Invoke-XUnit',
          'Invoke-XUnitWithCoverage',
          'New-Application',
          'New-AppPool',
          'New-NugetPackage',
          'New-Site',
          'Remove-Application',
          'Remove-AppPool',
          'Remove-Site',
          'RequiredFeatures',
          'Set-IISAuthentication',
          'Set-IISCustomHeader',
          'Start-Application',
          'Start-AppPool',
          'Start-Site',
          'Step',
          'Stop-Application',
          'Stop-AppPool',
          'Stop-Site',
          'Test-PathExtended',
          'Test-RunAsAdmin',
          'Update-Application',
          'Update-AppPool',
          'Update-AssemblyVersions',
          'Update-JsonConfigValues',
          'Update-Site',
          'Update-XmlConfigValues'
          )


# Messages
DATA msgs {
convertfrom-stringdata @"
    error_duplicate_step_name = Step {0} has already been defined.
    error_must_supply_a_feature = You must supply at least one Windows Feature.
    error_feature_set_invalid = The argument `"{0}`" does not belong to the set `"{1}`".
    error_admin_required = You are required to 'Run as Administrator' when running this deployment.
    error_loading_sql_file = Error loading '{0}'. {1}.
    error_octopus_deploy_failed = Failed to deploy: {0}.
    error_specflow_failed = Publishing specflow results  for '{0}' failed.
    error_coverage_failed = Running code coverage for '{0}' failed.
    error_tests_failed = Running tests '{0}' failed.
    error_msbuild_compile = Error compiling '{0}'.
    wrn_full_permission = You have applied FULL permission to '{0}' for '{1}'. THIS IS DANGEROUS!
    wrn_cant_find = Could not find {0} with the name: {0}.
    msg_grant_permission = Granting {0} permissions to {1} for {2}.
    msg_enabling_windows_feature = Enabling Windows Feature: `"{0}`".
    msg_wasnt_found = `"{0}`" wasn't found.
    msg_updated_to = Updated `"{0}`" to `"{1}`".
    msg_updating_to = Updating `"{0}`" to `"{1}`".
    msg_changing_to = Changing `"{0}`" to `"{1}`".
    msg_overriding_to = Overriding node `"{0}`" with value `"{1}`".
    msg_updating_assembly = Updating AssemblyVersion to '{0}'. Updating AssemblyFileVersion to '{1}'. Updating AssemblyInformationalVersion to '{2}'.
    msg_not_updating = Not updating {0}, you must specify the '-updateIfFound' if you wish to update the {0} settings.
    msg_custom_header = Setting custom header '{0}' on site '{1}' to value '{2}'.
    msg_disable_anon_auth = Disabling Anonymous Authentication for '{0}'.
    msg_web_app_success = Successfully deploy Web Application '{0}'.
    msg_copying_content = Copying {0} content to {1}.
    msg_use_machine_environment = Using config for machine {0} instead of the {1} environment.
    msg_octopus_overrides = Checking for Octopus Overrides for environment '{0}'.
    msg_teamcity_importdata = ##teamcity[importData type='{0}' tool='{1}' path='{2}']
    msg_teamcity_buildstatus = ##teamcity[buildStatus text='{0}']
    msg_teamcity_buildstatisticvalue = ##teamcity[buildStatisticValue key='{0}' value='{1}']
    msg_add_loopback_fix = Adding loopback fix for '{0}'.
    msg_add_mime_type = Adding mime type '{0}' for extension '{1}' to IIS site '{2}'.
    msg_add_verb = Adding IIS Http Verb '{0}' to site '{1}'.
    msg_add_host_entry = Adding host entry for '{0}' into the hosts file.
    msg_validate_host_entry = Validating host entry for '{0} in the hosts file'
    msg_loopback_note = note: we're not disabling the loopback check all together, we are simply adding '{0}' to an allowed list.
"@
}

问题与导入模块无关; powershell 在引导您解决问题方面不是很有帮助。在我将 -VERBOSE 开关放在导入上之前,问题的根源并没有显露出来。一旦我这样做了,我在旧错误之上得到了一个新错误。

: Parameter attributes need to be a constant or a script block.
FullyQualifiedErrorId: ParameterAttributeArgumentNeedsToBeConstandOrScriptBlock

本质上,我使用的是 ValidatePattern 双引号而不是单引号。检查并确保在使用 ValidatePattern

时正确编写正则表达式模式
# Bad
[ValidatePattern("^[a-z]$")]

# Good
[ValidatePattern('^[a-z]$')]