Skip to content

Instantly share code, notes, and snippets.

@lazywinadmin
Last active August 29, 2015 14:14
Show Gist options
  • Save lazywinadmin/360072f36ac0d3abb3c9 to your computer and use it in GitHub Desktop.
Save lazywinadmin/360072f36ac0d3abb3c9 to your computer and use it in GitHub Desktop.
Create perforce user using powershell
function Connect-PerforceServer
{
<#
.SYNOPSIS
Connect to a Perforce Server
.DESCRIPTION
Connect to a Perforce Server
#>
[CmdletBinding()]
PARAM(
[Parameter(Mandatory=$True)]
$Server,
[Parameter(Mandatory=$True)]
$Port,
[Parameter(Mandatory=$True)]
$UserName,
[Parameter(Mandatory=$True)]
$Password
)
BEGIN
{
#Check if p4.exe is present
}
PROCESS
{
TRY
{
IF (Test-Connection -ComputerName $Server -Count 1 -Quiet -ErrorAction SilentlyContinue)
{
# Set the Environment Variables
$env:p4port = "$($Server):$($Port)"
$env:p4user = $UserName
# Connect to p4 as Admin
# The environment variables previously set will be used by p4
$Password |p4 login
}
ELSE
{
Write-Warning -Message "[PROCESS] Can't ping to $Server on port: $port"
}
}
CATCH
{
Write-Warning -Message "[PROCESS] Issue while connecting to perforce server: $Server on port: $port"
}
}
}
function New-PerforceUserTemplate
{
<#
.SYNOPSIS
Create a tmp file which contains the User, Email and FullName
.DESCRIPTION
Create a tmp file which contains the User, Email and FullName
#>
[CmdletBinding()]
PARAM(
[Parameter(Mandatory=$True)]
$UserName,
[Parameter(Mandatory=$True)]
$EmailAddress,
[Parameter(Mandatory=$True)]
$FullName,
$TempDirectory = "c:\"
)
PROCESS{
# Create Temp file
$p4TemplateFile = join-path -path $TempDirectory -childpath "NewPerforceUser_$($UserName)_$(Get-Date -Format 'yyyyMMdd_HHmmss').tmp"
# Define user information
$tempp4NewUserName = "User:" + $UserName
$tempp4NewUserEmail = "email:" + $EmailAddress
$tempp4NewUserFullName = "fullname:" + $FullName
# Add the information to a template file
$tempp4NewUserName | Out-File -FilePath $p4TemplateFile | Out-Null
$tempp4NewUserEmail | Out-File -FilePath $p4TemplateFile -Append | Out-Null
$tempp4NewUserFullName | Out-File -FilePath $p4TemplateFile -Append | Out-Null
}
END{
#Output FilePath
Write-Output $p4TemplateFile
}
}
function Create-PerforceUser
{
<#
.SYNOPSIS
Create a user in perforce based on tmp file which contains the User, Email and FullName
.DESCRIPTION
Create a user in perforce based on tmp file which contains the User, Email and FullName
#>
[CmdletBinding()]
PARAM($UserTemplate)
PROCESS{
# Create perforce user based on template
Get-Content $UserTemplate|p4 user -f -i
}
}
Function Check-PerforceUserExist
{
PARAM($UserName)
PROCESS
{
$CheckAccount = p4 users $UserName
if ($CheckAccount -like "*accessed*")
{
$CheckAccount = $CheckAccount -split '\s'
$Properties = @{
"UserName" = $CheckAccount[0]
"Email" = $CheckAccount[1] -replace "<|>",""
"PerforceAccount" = $CheckAccount[2] -replace "\(|\)",""
}
New-Object -TypeName PSObject -Property $Properties
}#IF
}
}
# Connect to Perforce
Connect-PerforceServer -Server "PerforceServer.Contoso.com" -Port 1666 -UserName SuperAccount -Password Sup3rP@ssw0rd
IF (-not(Check-PerforceUserExist))
{
# Create Template
$Template = New-PerforceUserTemplate -UserName "TestUser" -EmailAddress "test.user@contoso.com" -FullName "Test User" -TempDirectory "C:\Scorch\Perforce\newaccount"
# Create the new user
Create-PerforceUser -UserTemplate $Template
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment