Private/Get-specPSCredential.ps1
Function Get-specPSCredential { <# .SYNOPSIS Retrieves a PSCredential object based on provided username and password. .DESCRIPTION This function takes a username and password as parameters and returns a PSCredential object. The username and password can be provided as separate string parameters or as properties of a [pscustomobject] when using the pipeline. .PARAMETER User Specifies the username for which to create a PSCredential object. .PARAMETER Password Specifies the password for which to create a PSCredential object. .EXAMPLE Get-specPSCredential -User 'john' -Password 'pass123' Creates a PSCredential object for the user 'john' with the specified password 'pass123'. The input is provided using inline parameters. .EXAMPLE $mysecrets = @( [pscustomobject]@{User = [string]'owen'; password = [string]'123'}, [pscustomobject]@{User = [string]'gary'; password = [string]'321'}, [pscustomobject]@{User = [string]'marg'; password = [string]'abc'} ) $mysecrets | Get-specPSCredential Creates PSCredential objects for each user/password pair in the $mysecrets array. The input is provided as [pscustomobject] with 'User' and 'Password' properties. .NOTES Author : owen.heaume Version : 1.0 #> [CmdletBinding()] param ( [Parameter( Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0)] [Alias('UserName')] [string]$User, [Parameter( Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)] [Alias('PW')] [string]$Password ) Begin {} Process { try { $usern = $user $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force $psCredential = New-Object System.Management.Automation.PSCredential($usern, $securePassword) $psCredential } catch { Write-Warning "Error: $($_.Exception.Message)" } } End { } } |