根据文档,我需要在运行容器时运行以下 PShell 脚本才能访问 AWS IAM 角色:
$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
但是它会引发异常:
Get-WMIObject :术语“Get-WMIObject”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。
一些谷歌搜索似乎表明该 cmdlet 已替换为Get-CimInstance
,这确实似乎至少进展到脚本中的第 3 行,但随后:
New-NetRoute:无法处理参数“InterfaceIndex”的参数转换。无法将“System.Object[]”类型的“System.Object[]”值转换为“System.UInt32”类型。
的值$ifIndex
是一个数组[7,19]
。
我的 Dockerfile:
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"]
startProject.ps1的完整内容:
$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
编辑
实际上,我似乎说得太早了 - usingGet-CimInstance
返回错误:
Get-CimInstance:无效的类
这似乎表明它找不到Win32_IP4RouteTable