0

I have the following script that will deploy an Octopus Deploy release based on the parameters I provide it.

    param(
   [string] $releaseVersion,
   [array] $future
   
)

foreach($line in Get-Content C:\admin\customers.txt) {

$ErrorActionPreference = "Stop";

# Define working variables
$octopusURL = "http://10.2.2.62:8022/api"
$octopusAPIKey = "API-Key"
$headers = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$spaceName = "Default"
$projectName = "C9-Deployment"
$environmentName = "LabFarm2"
$tenantNames = $line
$date = get-date -Format yyyy-MM-dd
$expiredate = $(date).AddDays(1).ToString("yyyy-MM-dd")

# Get space id
$spaces = Invoke-WebRequest -Uri "$octopusURL/spaces/all" -Headers $headers -ErrorVariable octoError | ConvertFrom-Json
$space = $spaces | Where-Object { $_.Name -eq $spaceName }
Write-Host "Using Space named $($space.Name) with id $($space.Id)"

# Get project by name
$projects = Invoke-WebRequest -Uri "$octopusURL/projects/all" -Headers $headers -ErrorVariable octoError | ConvertFrom-Json
$project = $projects | Where-Object { $_.Name -eq $projectName }
Write-Host "Using Project named $($project.Name) with id $($project.Id)"

# Create space specific url
$octopusSpaceUrl = "$octopusURL/$($space.Id)"

# Get release by version
$releases = Invoke-RestMethod -Uri "$octopusSpaceUrl/projects/$($project.Id)/releases" -Headers $headers -ErrorVariable octoError
$release = $releases.Items | Where-Object { $_.Version -eq $releaseVersion }
Write-Host "Using Release version $($release.Version) with id $($release.Id)"

# Get environment by name
$environments = Invoke-RestMethod -Uri "$octopusSpaceUrl/environments?partialName=$([uri]::EscapeDataString($environmentName))&skip=0&take=100" -Headers $headers -ErrorVariable octoError
$environment = $environments.Items | Where-Object { $_.Name -eq $environmentName }
Write-Host "Using Environment named $($environment.Name) with id $($environment.Id)"

$tenants = Invoke-WebRequest -Uri "$octopusSpaceUrl/tenants/all" -Headers $headers -ErrorVariable octoError | ConvertFrom-Json

$tenantNames | ForEach-Object {
    $name = $_
    $tenant = $tenants | Where-Object { $_.Name -eq $name }

if ($future -eq $null) {   
    write-host "This deployment is for tonight"
    $deploymentBody = @{
        ReleaseId     = $release.Id
        EnvironmentId = $environment.Id
        TenantId      = $tenant.Id
        QueueTime     = "${date}T23:00:00"
        QueueTimeExpiry = "${expiredate}T05:00:00"
    } | ConvertTo-Json
}

if ($future -ne $null) {
    write-host "This deployment will take place on $future"
#Problem Line 64 below
    $expirefuturedate = (get-date $future).Adddays(1).ToString("yyyy-MM-dd")
    $deploymentBody = @{
        ReleaseId     = $release.Id
        EnvironmentId = $environment.Id
        TenantId      = $tenant.Id
        QueueTime     = "${future}T23:00:00"
#problem line 70 below
        QueueTimeExpiry = "${expirefuturedate}T05:00:00"
    } | ConvertTo-Json
}

    Write-Host "Creating deployment with these values: $deploymentBody"
    $deployment = Invoke-WebRequest -Uri $octopusSpaceUrl/deployments -Method POST -Headers $headers -Body $deploymentBody -ErrorVariable octoError
}
}

So the problem is on line 64 and 70 where I try to take add one day to the Future parameter. If I run this with only the ReleaseVersion parameter set it will run fine without issues. But if I add a parameter for future like "-Future 2021-03-11" I get the following error:

PS C:\Users\bbelden.CLOUD9\Documents\powershell\Octopus> .\Deploycustom_parm.ps1 -releaseversion 8.1.2103.193 -future 20
21-03-11
Using Space named Default with id Spaces-1
Using Project named C9-Deployment with id Projects-101
Using Release version 8.1.2103.193 with id Releases-12243
Using Environment named LabFarm2 with id Environments-161
This deployment will take place on 2021-03-11
ForEach-Object : Cannot convert 'System.Object[]' to the type 'System.DateTime' required by parameter 'Date'.
Specified method is not supported.
At C:\Users\bbelden.CLOUD9\Documents\powershell\Octopus\Deploycustom_parm.ps1:47 char:16
+ $tenantNames | ForEach-Object {
+                ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ForEachObjectCommand

Now if I just remove the line 64 completely and manually set a time date stamp in line 70 like the following:

 if ($future -ne $null) {
    write-host "This deployment will take place on $future"
    #$expirefuturedate = (get-date $future).Adddays(1).ToString("yyyy-MM-dd")
    $deploymentBody = @{
        ReleaseId     = $release.Id
        EnvironmentId = $environment.Id
        TenantId      = $tenant.Id
        QueueTime     = "${future}T23:00:00"
        QueueTimeExpiry = "2021-03-11T05:00:00"
    } | ConvertTo-Json
}

It will work fine. So I am not really sure what I am missing here. Please let me know if there is something I am doing wrong. I believe it has to do with the Array, because if I comment that line out the issue will go away, but I need a way to transform the $future variable to adding one day to it.

Thanks

2
  • 3
    It would be a help to readers to specify which lines you are referring to using comments in your code (e.g., # below line has the problem) rather than referring to line numbers. (With line numbers, you are assuming that readers have the time to copy and paste all of your code and that line breaks will match exactly. A comment identifying the problem line(s) is preferable.) Commented Mar 8, 2021 at 18:54
  • I strongly urge you to reformat your code to properly show the scope of the various scriptblocks - the error message had me looking in a completely different place than you indicate. Commented Mar 8, 2021 at 19:32

1 Answer 1

2

This should explain your error:

PS /home/> $future=[array]'2021-03-11'
PS /home/> (get-date $future).Adddays(1).ToString("yyyy-MM-dd")
Get-Date: Cannot convert 'System.Object[]' to the type 'System.DateTime' required by parameter 'Date'. Specified method is not supported.
PS /home/> $future=[datetime]'2021-03-11'                      
PS /home/> (get-date $future).Adddays(1).ToString("yyyy-MM-dd")
2021-03-12
Sign up to request clarification or add additional context in comments.

1 Comment

Np, glad to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.