简体   繁体   English

Powershell,纠缠和启用-ADAccount

[英]Powershell, Pester and Enable-ADAccount

In my module PSCHSGActiveDirectory, I have a function Set-CHSGActiveUserActiveOn which call 2 functions from ActiveDirectory module: Get-ADUser and Enable-ADAccount.在我的模块 PSCHSGActiveDirectory 中,我有一个 function Set-CHSGActiveUserActiveOn,它从 ActiveDirectory 模块调用 2 个函数:Get-ADUser 和 Enable-ADAccount。

Function Set-CHSGActiveUserActiveOn() {
    Param(
        [Parameter(
            Mandatory = $True,
            ValueFromPipeline = $True
        )]
        [String]$samAccountName
    )
    ...
    $user = Get-ADUser -Identity $SamAccountName
    ...
    Enable-ADAccount -Identity $user
    ...
}

Here, my pester test file:在这里,我的纠缠测试文件:

BeforeAll {
    Import-Module PSCHSGActiveDirectory -Force
}

Describe 'Set-CHSGActiveUserActiveOn' {
    Context 'Without Reset Password' {
        BeforeAll {
            #Mock Get-ADUser -ModuleName PSCHSGActiveDirectory { }
            #Mock Enable-ADAccount -ModuleName PSCHSGActiveDirectory { }
        }

        It 'Tests Enable User' {
            PSCHSGActiveDirectory\Set-CHSGActiveUserActiveOn -SamAccountName 'johndoe' | Should -BeNullOrEmpty
        }
    }
}

When i didn't mock anything, test failed on Get-ADUser.当我没有模拟任何东西时,Get-ADUser 上的测试失败了。

When i mock only Get-ADUser, test failed on Enable-ADAccount with the message:当我只模拟 Get-ADUser 时,Enable-ADAccount 上的测试失败并显示以下消息:

[-] Set-CHSGActiveUserActiveOn.Without Reset Password.Tests Enable User 42ms (40ms|2ms)
 ValidationMetadataException: L’argument est Null. Spécifiez une valeur valide pour l’argument, puis réessayez.
 ParameterBindingValidationException: Impossible de valider l'argument sur le paramètre «*Identity*». L’argument est Null. Spécifiez une valeur valide pour l’argument, puis réessayez.

When i mock both, test still failed on Enable-ADAccount with the same message当我同时模拟两者时,Enable-ADAccount 上的测试仍然失败,并显示相同的消息

I try to change my function Set-CHSGActiveUserActiveOn with the use of a param Microsoft.ActiveDirectory.Management.ADUser instead of a string (and delete the Get-ADuser of course) but the problem still here.我尝试使用参数 Microsoft.ActiveDirectory.Management.ADUser 而不是字符串(当然删除 Get-ADuser)来更改我的 function Set-CHSGActiveUserActiveOn,但问题仍然存在。

I use these versions:我使用这些版本:

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   1.0.1.0    ActiveDirectory                     {Add-ADCentralAccessPolicyMember, Add-ADComputerServiceAccount, Add-ADDomainControllerPasswordReplicationPolicy, Add-ADFineGrainedPasswordPolicySubject...}
Binary     1.0.0.0    CimCmdlets                          {Export-BinaryMiLog, Get-CimAssociatedInstance, Get-CimClass, Get-CimInstance...}
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}
Manifest   3.0.0.0    Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl, Get-AuthenticodeSignature...}
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Manifest   3.0.0.0    Microsoft.WSMan.Management          {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, Enable-WSManCredSSP...}
Script     5.3.1      Pester                              {Add-ShouldOperator, AfterAll, AfterEach, Assert-MockCalled...}
Script     3.4.0      Pester                              {AfterAll, AfterEach, Assert-MockCalled, Assert-VerifiableMocks...}
Script     2.0.0      PSReadline                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Set-PSReadLineKeyHandler...}

I suppose it is just a syntax problem to mock Enable-ADAccount but i can't find any ways.我想这只是模拟 Enable-ADAccount 的语法问题,但我找不到任何方法。

Thank you in advance,先感谢您,

If the flow of the tested function expects a result from a called cmdlet, you should always return (a mocked) result from that function.如果测试的 function 的流程期望来自调用的 cmdlet 的结果,则应始终从该 function 返回(模拟)结果。

Change the Mocks to something like this:将 Mocks 更改为如下所示:

Mock Get-ADUser -ModuleName PSCHSGActiveDirectory { 
   return $Identity
}
Mock Enable-ADAccount -ModuleName PSCHSGActiveDirectory { }

$Identity is the value of the -Identity parameter as made available within the Mock by Pester. $Identity是 Pester 在 Mock 中提供的-Identity参数的值。 You could also return a fixed value, eg.您还可以返回一个固定值,例如。 "user" or another (script) variable "user"或另一个(脚本)变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM