From 4411a2adc97e9ca2a400ea590b9ebaca2caf2d4a Mon Sep 17 00:00:00 2001 From: Mariah Breakey Date: Thu, 15 Dec 2016 09:39:08 -0800 Subject: [PATCH] adding user helpers back in for now and updating the manifest --- PSDscResources.psd1 | 2 +- Tests/TestHelpers/CommonTestHelper.psm1 | 406 ++++++++++++++++++++++++ 2 files changed, 407 insertions(+), 1 deletion(-) diff --git a/PSDscResources.psd1 b/PSDscResources.psd1 index 95fabab..93c55d4 100644 --- a/PSDscResources.psd1 +++ b/PSDscResources.psd1 @@ -73,7 +73,7 @@ VariablesToExport = '*' AliasesToExport = @() # DSC resources to export from this module -DscResourcesToExport = 'Group', 'Service', 'User', 'WindowsFeature', 'WindowsOptionalFeature', 'WindowsPackageCab' +DscResourcesToExport = 'Group', 'Service', 'User', 'WindowsFeature', 'WindowsOptionalFeature', 'WindowsPackageCab', 'WindowsProcess' # List of all modules packaged with this module # ModuleList = @() diff --git a/Tests/TestHelpers/CommonTestHelper.psm1 b/Tests/TestHelpers/CommonTestHelper.psm1 index b971a23..4177e30 100644 --- a/Tests/TestHelpers/CommonTestHelper.psm1 +++ b/Tests/TestHelpers/CommonTestHelper.psm1 @@ -105,6 +105,409 @@ function Test-IsLocalMachine return $false } +<# + .SYNOPSIS + Creates a user account. + + .DESCRIPTION + This function creates a user on the local or remote machine. + + .PARAMETER Credential + The credential containing the username and password to use to create the account. + + .PARAMETER Description + The optional description to set on the user account. + + .PARAMETER ComputerName + The optional name of the computer to update. Omit to create a user on the local machine. + + .NOTES + For remote machines, the currently logged on user must have rights to create a user. +#> +function New-User +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [System.Management.Automation.PSCredential] + [System.Management.Automation.Credential()] + $Credential, + + [String] + $Description, + + [String] + $ComputerName = $env:COMPUTERNAME + ) + + if (Test-IsNanoServer) + { + New-UserOnNanoServer @PSBoundParameters + } + else + { + New-UserOnFullSKU @PSBoundParameters + } +} + +<# + .SYNOPSIS + Creates a user account on a full server. + + .DESCRIPTION + This function creates a user on the local or remote machine running a full server. + + .PARAMETER Credential + The credential containing the username and password to use to create the account. + + .PARAMETER Description + The optional description to set on the user account. + + .PARAMETER ComputerName + The optional name of the computer to update. Omit to create a user on the local machine. + + .NOTES + For remote machines, the currently logged on user must have rights to create a user. +#> +function New-UserOnFullSKU +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [System.Management.Automation.PSCredential] + [System.Management.Automation.Credential()] + $Credential, + + [String] + $Description, + + [String] + $ComputerName = $env:COMPUTERNAME + ) + + Set-StrictMode -Version Latest + + $userName = $Credential.UserName + $password = $Credential.GetNetworkCredential().Password + + # Remove user if it already exists. + Remove-User $userName $ComputerName + + $adComputerEntry = [ADSI] "WinNT://$ComputerName" + $adUserEntry = $adComputerEntry.Create('User', $userName) + $null = $adUserEntry.SetPassword($password) + + if ($PSBoundParameters.ContainsKey('Description')) + { + $null = $adUserEntry.Put('Description', $Description) + } + + $null = $adUserEntry.SetInfo() +} + +<# + .SYNOPSIS + Creates a user account on a Nano server. + + .DESCRIPTION + This function creates a user on the local machine running a Nano server. + + .PARAMETER Credential + The credential containing the username and password to use to create the account. + + .PARAMETER Description + The optional description to set on the user account. + + .PARAMETER ComputerName + This parameter should not be used on NanoServer. +#> +function New-UserOnNanoServer +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [System.Management.Automation.PSCredential] + [System.Management.Automation.Credential()] + $Credential, + + [String] + $Description, + + [String] + $ComputerName = $env:COMPUTERNAME + ) + + Set-StrictMode -Version Latest + + if ($PSBoundParameters.ContainsKey('ComputerName')) + { + if (-not (Test-IsLocalMachine -Scope $ComputerName)) + { + throw 'Do not specify the ComputerName arguments when running on NanoServer unless it is a local machine.' + } + } + + $userName = $Credential.UserName + $securePassword = $Credential.GetNetworkCredential().SecurePassword + + # Remove user if it already exists. + Remove-LocalUser -Name $userName -ErrorAction SilentlyContinue + + New-LocalUser -Name $userName -Password $securePassword + + if ($PSBoundParameters.ContainsKey('Description')) + { + Set-LocalUser -Name $userName -Description $Description + } +} + +<# + .SYNOPSIS + Removes a user account. + + .DESCRIPTION + This function removes a local user from the local or remote machine. + + .PARAMETER UserName + The name of the user to remove. + + .PARAMETER ComputerName + The optional name of the computer to update. Omit to remove the user on the local machine. + + .NOTES + For remote machines, the currently logged on user must have rights to remove a user. +#> +function Remove-User +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String] + $UserName, + + [String] + $ComputerName = $env:COMPUTERNAME + ) + + if (Test-IsNanoServer) + { + Remove-UserOnNanoServer @PSBoundParameters + } + else + { + Remove-UserOnFullSKU @PSBoundParameters + } +} + +<# + .SYNOPSIS + Removes a user account on a full server. + + .DESCRIPTION + This function removes a local user from the local or remote machine running a full server. + + .PARAMETER UserName + The name of the user to remove. + + .PARAMETER ComputerName + The optional name of the computer to update. Omit to remove the user on the local machine. + + .NOTES + For remote machines, the currently logged on user must have rights to remove a user. +#> +function Remove-UserOnFullSKU +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String] + $UserName, + + [String] + $ComputerName = $env:COMPUTERNAME + ) + + Set-StrictMode -Version Latest + + $adComputerEntry = [ADSI] "WinNT://$ComputerName" + + if ($adComputerEntry.Children | Where-Object Path -like "WinNT://*$ComputerName/$UserName") + { + $null = $adComputerEntry.Delete('user', $UserName) + } +} + +<# + .SYNOPSIS + Removes a local user account on a Nano server. + + .DESCRIPTION + This function removes a local user from the local machine running a Nano Server. + + .PARAMETER UserName + The name of the user to remove. + + .PARAMETER ComputerName + This parameter should not be used on NanoServer. +#> +function Remove-UserOnNanoServer +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String] + $UserName, + + [String] + $ComputerName = $env:COMPUTERNAME + ) + + Set-StrictMode -Version Latest + + if ($PSBoundParameters.ContainsKey('ComputerName')) + { + if (-not (Test-IsLocalMachine -Scope $ComputerName)) + { + throw 'Do not specify the ComputerName arguments when running on NanoServer unless it is a local machine.' + } + } + + Remove-LocalUser -Name $UserName +} + +<# + .SYNOPSIS + Determines if a user exists. + + .DESCRIPTION + This function determines if a user exists on a local or remote machine. + + .PARAMETER UserName + The name of the user to test. + + .PARAMETER ComputerName + The optional name of the computer to update. +#> +function Test-User +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String] + $UserName, + + [String] + $ComputerName = $env:COMPUTERNAME + ) + + if (Test-IsNanoServer) + { + Test-UserOnNanoServer @PSBoundParameters + } + else + { + Test-UserOnFullSKU @PSBoundParameters + } +} + +<# + .SYNOPSIS + Determines if a user exists on a full server. + + .DESCRIPTION + This function determines if a user exists on a local or remote machine running a full server. + + .PARAMETER UserName + The name of the user to test. + + .PARAMETER ComputerName + The optional name of the computer to update. +#> +function Test-UserOnFullSKU +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String] + $UserName, + + [String] + $ComputerName = $env:COMPUTERNAME + ) + + Set-StrictMode -Version Latest + + $adComputerEntry = [ADSI] "WinNT://$ComputerName" + + if ($adComputerEntry.Children | Where-Object Path -like "WinNT://*$ComputerName/$UserName") + { + return $true + } + + return $false +} + +<# + .SYNOPSIS + Determines if a user exists on a Nano server. + + .DESCRIPTION + This function determines if a user exists on a local or remote machine running a Nano server. + + .PARAMETER UserName + The name of the user to test. + + .PARAMETER ComputerName + This parameter should not be used on NanoServer. +#> +function Test-UserOnNanoServer +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String] + $UserName, + + [String] + $ComputerName = $env:COMPUTERNAME + ) + + if ($PSBoundParameters.ContainsKey('ComputerName')) + { + if (-not (Test-IsLocalMachine -Scope $ComputerName)) + { + throw 'Do not specify the ComputerName arguments when running on NanoServer unless it is a local machine.' + } + } + + # Try to find a user by name. + try + { + $null = Get-LocalUser -Name $UserName -ErrorAction Stop + return $true + } + catch [System.Exception] + { + if ($_.CategoryInfo.ToString().Contains('UserNotFoundException')) + { + # A user with the provided name does not exist. + return $false + } + throw $_.Exception + } + + return $false +} + <# .SYNOPSIS Waits a certain amount of time for a script block to return true. @@ -411,6 +814,9 @@ function Exit-DscResourceTestEnvironment Export-ModuleMember -Function @( 'Test-GetTargetResourceResult', ` + 'New-User', ` + 'Remove-User', ` + 'Test-User', ` 'Wait-ScriptBlockReturnTrue', ` 'Test-IsFileLocked', ` 'Test-SetTargetResourceWithWhatIf', `