Private/New-TMProjectWebService.ps1
function New-TMProjectWebService { [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Name, [Parameter(Mandatory = $false)] [String]$Company, [Parameter(Mandatory = $false)] [String]$Code, [Parameter(Mandatory = $false)] [PSObject]$TMSession, [Parameter(Mandatory = $false)] [DateTime]$StartDate, [Parameter(Mandatory = $false)] [DateTime]$EndDate, [Parameter(Mandatory = $false)] [Switch]$Passthru ) begin { ## Get Session Configuration $TMSession = Get-TMSession $TMSession #Honor SSL Settings $TMCertSettings = @{ SkipCertificateCheck = $TMSession.AllowInsecureSSL } } process { # Check if the project already exists $ExistingProject = Get-TMProject -Name $Name -TMSession $TMSession if ($ExistingProject) { if ($Passthru) { return $ExistingProject } else { return } } # Make sure the Provider exists $Client = Get-TMCompany -Name $Company -TMSession $TMSession if (!$Client) { throw "The company '$Company' does not exist." } $instance = $TMSession.TMServer.Replace('/tdstm', '').Replace('https://', '').Replace('http://', '') $uri = "https://$instance/tdstm/ws/project/saveProject/" Set-TMHeaderContentType -ContentType 'JSON' -TMSession $TMSession if (!$StartDate) { $StartDate = (Get-Date -AsUTC).ToString('yyyy-MM-ddTHH:mm:ssZ') } if (!$EndDate) { $EndDate = (Get-Date -AsUTC).AddDays(365).ToString('yyyy-MM-ddTHH:mm:ssZ') } if (!$Code) { $FormattedCode = $Name.Trim().Replace(' - ', '-').Replace('- ', '-').Replace(' ', '-') $Code = $FormattedCode.Length -gt 20 ? $FormattedCode.Substring(0, 20) : $FormattedCode } # Format the body of the request $Project = [PSCustomObject]@{ clientId = $Client.id startDate = $StartDate completionDate = $EndDate projectCode = $Code defaultBundleName = "TBD" description = "" collectMetrics = $true timeZone = "GMT" projectManagerId = 0 comment = "" projectType = "Standard" } # Format the name property based on the TM version $NameKey = $TMSession.TMVersion -like '4.*' ? 'projectName' : 'name' $Project | Add-Member -Name $NameKey -Value $Name -MemberType NoteProperty $PostJson = $Project | ConvertTo-Json try { $response = Invoke-WebRequest -Method Post -Uri $uri -WebSession $TMSession.TMWebSession -Body $PostJson @TMCertSettings } catch { return $_ } if ($response.StatusCode -in @(200, 204)) { if ($Passthru) { return (Get-TMProject -Name $Name -TMSession $TMSession) } } else { throw "Unable to create Project." } } end { } } |