Skip to content

Commit

Permalink
Fix named parameters on New-Object
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju committed Jun 15, 2019
1 parent 2220fbf commit 65016dd
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 55 deletions.
8 changes: 4 additions & 4 deletions DSCResources/MSFT_xADDomainTrust/MSFT_xADDomainTrust.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ function Get-TargetResource
}

# Create the target object
$trgDirectoryContext = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext($DomainOrForest, $TargetDomainName, $TargetDomainAdministratorCredential.UserName, $TargetDomainAdministratorCredential.GetNetworkCredential().Password)
$trgDirectoryContext = New-Object -TypeName 'System.DirectoryServices.ActiveDirectory.DirectoryContext' -ArgumentList @($DomainOrForest, $TargetDomainName, $TargetDomainAdministratorCredential.UserName, $TargetDomainAdministratorCredential.GetNetworkCredential().Password)
$trgDomain = ([type]"System.DirectoryServices.ActiveDirectory.$DomainOrForest")::"Get$DomainOrForest"($trgDirectoryContext)

# Create the source object
$srcDirectoryContext = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext($DomainOrForest, $SourceDomainName)
$srcDirectoryContext = New-Object -TypeName 'System.DirectoryServices.ActiveDirectory.DirectoryContext' -ArgumentList @($DomainOrForest, $SourceDomainName)
$srcDomain = ([type]"System.DirectoryServices.ActiveDirectory.$DomainOrForest")::"Get$DomainOrForest"($srcDirectoryContext)

# Find trust between source & destination.
Expand Down Expand Up @@ -266,11 +266,11 @@ function Confirm-ResourceProperties
}

# Create the target object
$trgDirectoryContext = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext($DomainOrForest, $TargetDomainName, $TargetDomainAdministratorCredential.UserName, $TargetDomainAdministratorCredential.GetNetworkCredential().Password)
$trgDirectoryContext = New-Object -TypeName 'System.DirectoryServices.ActiveDirectory.DirectoryContext' -ArgumentList @($DomainOrForest, $TargetDomainName, $TargetDomainAdministratorCredential.UserName, $TargetDomainAdministratorCredential.GetNetworkCredential().Password)
$trgDomain = ([type]"System.DirectoryServices.ActiveDirectory.$DomainOrForest")::"Get$DomainOrForest"($trgDirectoryContext)

# Create the source object
$srcDirectoryContext = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext($DomainOrForest, $SourceDomainName)
$srcDirectoryContext = New-Object -TypeName 'System.DirectoryServices.ActiveDirectory.DirectoryContext' -ArgumentList @($DomainOrForest, $SourceDomainName)
$srcDomain = ([type]"System.DirectoryServices.ActiveDirectory.$DomainOrForest")::"Get$DomainOrForest"($srcDirectoryContext)

# Find trust
Expand Down
2 changes: 1 addition & 1 deletion DSCResources/MSFT_xADKDSKey/MSFT_xADKDSKey.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ function Assert-HasDomainAdminRights
$User
)

$windowsPrincipal = New-Object -TypeName System.Security.Principal.WindowsPrincipal($User)
$windowsPrincipal = New-Object -TypeName 'System.Security.Principal.WindowsPrincipal' -ArgumentList @($User)
$osInfo = Get-CimInstance -ClassName Win32_OperatingSystem

Write-Verbose -Message ($script:localizedData.CheckingDomainAdminUserRights -f $User.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,14 @@ function Set-TargetResource

$ntAccount = New-Object -TypeName 'System.Security.Principal.NTAccount' -ArgumentList $IdentityReference

$ace = New-Object -TypeName 'System.DirectoryServices.ActiveDirectoryAccessRule' -ArgumentList $ntAccount,
$ActiveDirectoryRights,
$AccessControlType,
$ObjectType,
$ActiveDirectorySecurityInheritance,
$InheritedObjectType
$ace = New-Object -TypeName 'System.DirectoryServices.ActiveDirectoryAccessRule' -ArgumentList @(
$ntAccount,
$ActiveDirectoryRights,
$AccessControlType,
$ObjectType,
$ActiveDirectorySecurityInheritance,
$InheritedObjectType
)

$acl.AddAccessRule($ace)
}
Expand Down
4 changes: 2 additions & 2 deletions DSCResources/MSFT_xADUser/MSFT_xADUser.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ function Test-Password

if ($DomainAdministratorCredential)
{
$principalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext(
$principalContext = New-Object -TypeName 'System.DirectoryServices.AccountManagement.PrincipalContext' -ArgumentList @(
[System.DirectoryServices.AccountManagement.ContextType]::Domain,
$DomainName,
$DomainAdministratorCredential.UserName,
Expand All @@ -1534,7 +1534,7 @@ function Test-Password
}
else
{
$principalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext(
$principalContext = New-Object -TypeName 'System.DirectoryServices.AccountManagement.PrincipalContext' -ArgumentList @(
[System.DirectoryServices.AccountManagement.ContextType]::Domain,
$DomainName,
$null,
Expand Down
6 changes: 3 additions & 3 deletions DSCResources/MSFT_xWaitForADDomain/MSFT_xWaitForADDomain.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$script:resourceModulePath = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent
$script:resourceModulePath = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent
$script:modulesFolderPath = Join-Path -Path $script:resourceModulePath -ChildPath 'Modules'

$script:localizationModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'xActiveDirectory.Common'
Expand Down Expand Up @@ -209,11 +209,11 @@ function Get-Domain

if ($DomainUserCredential)
{
$context = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext('Domain', $DomainName, $DomainUserCredential.UserName, $DomainUserCredential.GetNetworkCredential().Password)
$context = New-Object -TypeName 'System.DirectoryServices.ActiveDirectory.DirectoryContext' -ArgumentList @('Domain', $DomainName, $DomainUserCredential.UserName, $DomainUserCredential.GetNetworkCredential().Password)
}
else
{
$context = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext('Domain',$DomainName)
$context = New-Object -TypeName 'System.DirectoryServices.ActiveDirectory.DirectoryContext' -ArgumentList @('Domain', $DomainName)
}

try
Expand Down
10 changes: 5 additions & 5 deletions Modules/xActiveDirectory.Common/xActiveDirectory.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ function Remove-DuplicateMembers
}

# Create the output array.
$destination = New-Object -TypeName System.String[] -ArgumentList $destIndex
$destination = New-Object -TypeName 'System.String[]' -ArgumentList $destIndex

# Copy only distinct elements from the original array to the destination array.
[System.Array]::Copy($Members, $destination, $destIndex)
Expand Down Expand Up @@ -1055,9 +1055,9 @@ function ThrowInvalidOperationError
$ErrorMessage
)

$exception = New-Object -TypeName System.InvalidOperationException -ArgumentList $ErrorMessage
$exception = New-Object -TypeName 'System.InvalidOperationException' -ArgumentList $ErrorMessage
$errorCategory = [System.Management.Automation.ErrorCategory]::InvalidOperation
$errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $exception, $ErrorId, $errorCategory, $null
$errorRecord = New-Object -TypeName 'System.Management.Automation.ErrorRecord' -ArgumentList @($exception, $ErrorId, $errorCategory, $null)
throw $errorRecord
}

Expand All @@ -1077,9 +1077,9 @@ function ThrowInvalidArgumentError
$ErrorMessage
)

$exception = New-Object -TypeName System.ArgumentException -ArgumentList $ErrorMessage
$exception = New-Object -TypeName 'System.ArgumentException' -ArgumentList $ErrorMessage
$errorCategory = [System.Management.Automation.ErrorCategory]::InvalidArgument
$errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $exception, $ErrorId, $errorCategory, $null
$errorRecord = New-Object -TypeName 'System.Management.Automation.ErrorRecord' -ArgumentList @($exception, $ErrorId, $errorCategory, $null)
throw $errorRecord

} #end function ThrowInvalidArgumentError
Expand Down
37 changes: 30 additions & 7 deletions Tests/Unit/MSFT_xADDomain.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,16 @@ try
$mgmtForestMode = [Microsoft.ActiveDirectory.Management.ADForestMode]::Windows2012R2Forest
$domainMode = [Microsoft.DirectoryServices.Deployment.Types.DomainMode]::Win2012R2
$mgmtDomainMode = [Microsoft.ActiveDirectory.Management.ADDomainMode]::Windows2012R2Domain
$testAdminCredential = New-Object System.Management.Automation.PSCredential 'DummyUser', (ConvertTo-SecureString 'DummyPassword' -AsPlainText -Force)
$invalidCredential = New-Object System.Management.Automation.PSCredential 'Invalid', (ConvertTo-SecureString 'InvalidPassword' -AsPlainText -Force)

$testAdminCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
'DummyUser',
(ConvertTo-SecureString -String 'DummyPassword' -AsPlainText -Force)
)

$invalidCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
'Invalid',
(ConvertTo-SecureString -String 'InvalidPassword' -AsPlainText -Force)
)

$testDefaultParams = @{
DomainAdministratorCredential = $testAdminCredential
Expand Down Expand Up @@ -185,7 +193,10 @@ try
$correctDomainNetBIOSName = 'PRESENT'
$incorrectDomainName = 'incorrect.com'
$parentDomainName = 'parent.com'
$testAdminCredential = New-Object System.Management.Automation.PSCredential 'DummyUser', (ConvertTo-SecureString 'DummyPassword' -AsPlainText -Force)
$testAdminCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
'DummyUser',
(ConvertTo-SecureString -String 'DummyPassword' -AsPlainText -Force)
)

$testDefaultParams = @{
DomainAdministratorCredential = $testAdminCredential
Expand Down Expand Up @@ -279,10 +290,22 @@ try
$testParentDomainName = 'parent.com'
$testDomainNetBIOSNameName = 'PRESENT'
$testDomainForestMode = 'WinThreshold'
$testAdminCredential = New-Object System.Management.Automation.PSCredential 'Admin', (ConvertTo-SecureString 'DummyPassword' -AsPlainText -Force)
$testSafemodePassword = (ConvertTo-SecureString 'DummyPassword' -AsPlainText -Force)
$testSafemodeCredential = New-Object System.Management.Automation.PSCredential 'Safemode', $testSafemodePassword
$testDelegationCredential = New-Object System.Management.Automation.PSCredential 'Delegation', (ConvertTo-SecureString 'DummyPassword' -AsPlainText -Force)

$testAdminCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
'Admin',
(ConvertTo-SecureString -String 'DummyPassword' -AsPlainText -Force)
)

$testSafemodePassword = (ConvertTo-SecureString -String 'DummyPassword' -AsPlainText -Force)
$testSafemodeCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
'Safemode',
$testSafemodePassword
)

$testDelegationCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
'Delegation',
(ConvertTo-SecureString -String 'DummyPassword' -AsPlainText -Force)
)

$newForestParams = @{
DomainName = $testDomainName
Expand Down
24 changes: 18 additions & 6 deletions Tests/Unit/MSFT_xADDomainDefaultPasswordPolicy.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ try
DomainName = $testDomainName
}
$testDomainController = 'testserver.contoso.com'
$testPassword = (ConvertTo-SecureString 'DummyPassword' -AsPlainText -Force)
$testCredential = New-Object System.Management.Automation.PSCredential 'Safemode', $testPassword

$testPassword = ConvertTo-SecureString -String 'DummyPassword' -AsPlainText -Force
$testCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
'Safemode',
$testPassword
)

$fakePasswordPolicy = @{
ComplexityEnabled = $true
Expand Down Expand Up @@ -121,8 +125,12 @@ try
DomainName = $testDomainName
}
$testDomainController = 'testserver.contoso.com'
$testPassword = (ConvertTo-SecureString 'DummyPassword' -AsPlainText -Force)
$testCredential = New-Object System.Management.Automation.PSCredential 'Safemode', $testPassword

$testPassword = ConvertTo-SecureString -String 'DummyPassword' -AsPlainText -Force
$testCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
'Safemode',
$testPassword
)

$stubPasswordPolicy = @{
ComplexityEnabled = $true
Expand Down Expand Up @@ -205,8 +213,12 @@ try
DomainName = $testDomainName
}
$testDomainController = 'testserver.contoso.com'
$testPassword = (ConvertTo-SecureString 'DummyPassword' -AsPlainText -Force)
$testCredential = New-Object System.Management.Automation.PSCredential 'Safemode', $testPassword

$testPassword = ConvertTo-SecureString -String 'DummyPassword' -AsPlainText -Force
$testCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
'Safemode',
$testPassword
)

$stubPasswordPolicy = @{
ComplexityEnabled = $true
Expand Down
6 changes: 5 additions & 1 deletion Tests/Unit/MSFT_xADGroup.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ try
}

$testDomainController = 'TESTDC'
$testCredentials = New-Object System.Management.Automation.PSCredential 'DummyUser', (ConvertTo-SecureString 'DummyPassword' -AsPlainText -Force)

$testCredentials = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
'DummyUser',
(ConvertTo-SecureString -String 'DummyPassword' -AsPlainText -Force)
)

#region Function Get-TargetResource
Describe 'xADGroup\Get-TargetResource' {
Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/MSFT_xADKDSKey.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ try
$DeepCopyObject
)

$memStream = New-Object IO.MemoryStream
$formatter = New-Object Runtime.Serialization.Formatters.Binary.BinaryFormatter
$memStream = New-Object -TypeName 'IO.MemoryStream'
$formatter = New-Object -TypeName 'Runtime.Serialization.Formatters.Binary.BinaryFormatter'
$formatter.Serialize($memStream,$DeepCopyObject)
$memStream.Position = 0
$formatter.Deserialize($memStream)
Expand Down
10 changes: 7 additions & 3 deletions Tests/Unit/MSFT_xADManagedServiceAccount.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@ try
$DeepCopyObject
)

$memStream = New-Object IO.MemoryStream
$formatter = New-Object Runtime.Serialization.Formatters.Binary.BinaryFormatter
$memStream = New-Object -TypeName 'IO.MemoryStream'
$formatter = New-Object -TypeName 'Runtime.Serialization.Formatters.Binary.BinaryFormatter'
$formatter.Serialize($memStream,$DeepCopyObject)
$memStream.Position=0
$formatter.Deserialize($memStream)
}

$mockPath = 'OU=Fake,DC=contoso,DC=com'
$mockDomainController = 'MockDC'
$mockCredentials = New-Object System.Management.Automation.PSCredential 'DummyUser', (ConvertTo-SecureString 'DummyPassword' -AsPlainText -Force)

$mockCredentials = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
'DummyUser',
(ConvertTo-SecureString -String 'DummyPassword' -AsPlainText -Force)
)

$mockADUSer = @{
SamAccountName = 'User1'
Expand Down
5 changes: 4 additions & 1 deletion Tests/Unit/MSFT_xADOrganizationalUnit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ try
function Remove-ADOrganizationalUnit { param ($Name, $Credential) }
function New-ADOrganizationalUnit { param ($Name, $Credential) }

$testCredential = New-Object System.Management.Automation.PSCredential 'DummyUser', (ConvertTo-SecureString 'DummyPassword' -AsPlainText -Force)
$testCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
'DummyUser',
(ConvertTo-SecureString -String 'DummyPassword' -AsPlainText -Force)
)

$testPresentParams = @{
Name = 'TestOU'
Expand Down
11 changes: 7 additions & 4 deletions Tests/Unit/MSFT_xWaitForADDomain.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,22 @@ try
Invoke-TestSetup

InModuleScope $script:dscResourceName {
$password = 'Password' | ConvertTo-SecureString -AsPlainText -Force
$DomainUserCredential = New-Object pscredential('Username', $password)
$domainUserCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
'Username',
$(ConvertTo-SecureString -String 'Password' -AsPlainText -Force)
)

$domainName = 'example.com'
$testParams = @{
DomainName = $domainName
DomainUserCredential = $DomainUserCredential
DomainUserCredential = $domainUserCredential
RetryIntervalSec = 10
RetryCount = 5
}

$rebootTestParams = @{
DomainName = $domainName
DomainUserCredential = $DomainUserCredential
DomainUserCredential = $domainUserCredential
RetryIntervalSec = 10
RetryCount = 5
RebootRetryCount = 3
Expand Down
Loading

0 comments on commit 65016dd

Please sign in to comment.