title: Windows Docker Containers on AWS ECS - IAM Roles
body: <p>According to <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/windows_task_IAM_roles.html" rel="nofollow noreferrer">the docs</a>, I need to run the following PShell script when running a container so as to get access to AWS IAM Roles:</p>
  
  <pre><code>$gateway = (Get-WMIObject -Class Win32_IP4RouteTable | Where { $_.Destination -eq '0.0.0.0' -and $_.Mask -eq '0.0.0.0' } | Sort-Object Metric1 | Select NextHop).NextHop
  $ifIndex = (Get-NetAdapter -InterfaceDescription "Hyper-V Virtual Ethernet*" | Sort-Object | Select ifIndex).ifIndex
  New-NetRoute -DestinationPrefix 169.254.170.2/32 -InterfaceIndex $ifIndex -NextHop $gateway
  </code></pre>
  
  <p>However it throws an exception:</p>
  
  <blockquote>
    <p>Get-WMIObject : The term 'Get-WMIObject' is not recognized as the name
    of a cmdlet, function, script file, or operable program. Check the
    spelling of the name, or if a path was included, verify that the path
    is correct and try again.</p>
  </blockquote>
  
  <p>Some googling seems to suggest that the cmdlet has been replaced with <code>Get-CimInstance</code>, and this does indeed seem to at least progress to the 3rd line in the script, but then:</p>
  
  <blockquote>
    <p>New-NetRoute : Cannot process argument transformation on parameter 'InterfaceIndex'. Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.UInt32".</p>
  </blockquote>
  
  <p>The value of <code>$ifIndex</code> is an array <code>[7,19]</code>.</p>
  
  <p>My Dockerfile:</p>
  
  <pre><code>FROM microsoft/dotnet:2.0-sdk AS build-env
  WORKDIR /app
  
  COPY ./ ./
  RUN dotnet publish project/project.csproj -c Release -r win-x64 -o out
  
  FROM microsoft/nanoserver:sac2016
  WORKDIR /app
  COPY --from=build-env /app/project/out ./
  COPY --from=build-env /app/startProject.ps1 ./
  
  ENTRYPOINT ["powershell", ".\\startProject.ps1"]
  </code></pre>
  
  <p>The full contents of startProject.ps1:</p>
  
  <pre><code>$gateway = (Get-CimInstance -Class Win32_IP4RouteTable | Where-Object { $_.Destination -eq '0.0.0.0' -and $_.Mask -eq '0.0.0.0' } | Sort-Object Metric1 | Select-Object NextHop).NextHop
  $ifIndex = (Get-NetAdapter -InterfaceDescription "Hyper-V Virtual Ethernet*" | Sort-Object | Select-Object ifIndex).ifIndex
  New-NetRoute -DestinationPrefix 169.254.170.2/32 -InterfaceIndex $ifIndex -NextHop $gateway
  
  $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  Start-Process "$scriptDir\Project.exe" -Wait
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>Actually it seems I spoke too soon - using <code>Get-CimInstance</code> returns an error:</p>
  
  <blockquote>
    <p>Get-CimInstance : Invalid class</p>
  </blockquote>
  
  <p>Which seems to suggest that it can't find <code>Win32_IP4RouteTable</code></p>