【问题标题】:PowerShell import module in docker fails with error"module is not currently supported on the Core edition of PowerShell"docker 中的 PowerShell 导入模块失败并出现错误“PowerShell 核心版当前不支持模块”
【发布时间】:2020-11-23 02:22:31
【问题描述】:

我正在尝试在 php docker 映像中安装 powercli,以便可以使用 laravel 将报告发布到网页。已安装 Powershell。但导入模块时 PowerCLI 安装失败并出现以下错误。只有导入模块才能连接服务器。

Exception: VMware.VimAutomation.HorizonView module is not currently supported on the Core edition of PowerShell.

Dockerfile

FROM php:7.4-fpm
.
.
.

# Download the Microsoft repository GPG keys
RUN wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb

# Register the Microsoft repository GPG keys
RUN dpkg -i packages-microsoft-prod.deb

# Update the list of products
RUN apt-get update

# Install PowerShell
RUN apt-get install -y powershell

# Start PowerShell
#RUN pwsh

# Allow installation from PSGallery
RUN pwsh -Command 'Set-PSRepository -Name PSGallery -InstallationPolicy Trusted'

# Install PowerCLI, PowervRA, Vester
RUN pwsh -Command 'Install-Module -Name VMware.PowerCLI -Scope CurrentUser'
RUN pwsh -Command 'Install-Module -Name PowervRA -Confirm:$false'
RUN pwsh -Command 'Install-Module -Name Vester -Confirm:$false'

# Disable CEIP warning
RUN pwsh -Command 'Set-PowerCLIConfiguration -ParticipateInCEIP $false -Confirm:$false'

# Set InvalidCertificateAction
RUN pwsh -Command 'Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false'
RUN pwsh -c 'Import-Module VMware.PowerCLI'
.
.
.

docker-compose

version: '3'
services:

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: vapp
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./powershell/Modules/:/usr/local/share/powershell/Modules/
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network
.
.
.

在容器上手动测试时它可以工作。 请查看 dockerfile 是否有问题。

更新 1 现在我更新了 docker 文件如下:

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
RUN pwd
# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    libonig-dev \
    locales \
    libzip-dev \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl \
    wget \
    apt-utils


RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl mysqli
RUN docker-php-ext-configure gd --enable-gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd
RUN docker-php-ext-enable mysqli

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www


# Copy existing application directory permissions
COPY --chown=www:www . /var/www

#########################
RUN pwd
# Download the Microsoft repository GPG keys
RUN wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb

# Register the Microsoft repository GPG keys
RUN dpkg -i packages-microsoft-prod.deb

# Update the list of products
RUN apt-get update

# Install PowerShell
RUN apt-get install -y powershell

# Start PowerShell
#RUN pwsh

# Allow installation from PSGallery
SHELL ["pwsh", "-command"]

RUN Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
#RUN Install-Module VMware.VimAutomation.Core -Confirm:$false
#RUN Import-Module VMware.VimAutomation.Core; Get-Module

#RUN Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
##RUN connect-viserver 10.21.24.19

RUN mkdir -p ./powershell/Modules
RUN wget https://download3.vmware.com/software/vmw-tools/powerclicore/PowerCLI_Core.zip ~/.local/share/powershell/Modules
RUN unzip -o PowerCLI_Core.zip && unzip -o 'PowerCLI.*.zip' -d ./powershell/Modules
RUN cd ./powershell/Modules
RUN pwd
RUN ls -la
RUN Get-Module -ListAvailable VMware.VimAutomation.Core | Import-Module
RUN $env:PSModulePath = $env:PSModulePath + ":powershell/Modules"
RUN mkdir -p /root/.config/powershell
RUN touch /root/.config/powershell/Microsoft.PowerShell_profile.ps1
RUN echo "Get-Module -ListAvailable PowerCLI* | Import-Module" >> /root/.config/powershell/Microsoft.PowerShell_profile.ps1

SHELL ["/bin/sh", "-c"]
# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

【问题讨论】:

  • PowerCLI 通常也需要核心元素,而不仅仅是 Horizo​​n 的特定模块。如果你只运行 Import-Module VMware.PowerCLI 会发生什么?
  • 它失败并出现同样的错误。我不需要 Horizo​​nView。我正在检查RUN pwsh -c Import-Module VMware.PowerCLI -Function VMware.VimAutomation.HorizonView 是否跳过它。
  • RUN pwsh -c 'Import-Module VMware.PowerCLI -ErrorAction Continue' 也失败了
  • 如果我跳过RUN pwsh -c 'Import-Module VMware.PowerCLI这一行,构建成功。但是找不到命令。我觉得这是因为模块没有加载。有没有办法解决如何导入模块?
  • 模块已安装,错误在于导入不受支持的子模块。您可以编辑 .psd 以将其从加载列表中删除。在 Powershell 的安装路径中查找 VMware.PowerCLI.psd1 并编辑掉模块。

标签: docker powershell


【解决方案1】:

响应您的查询 - 要使用 Powershell 以编程方式执行此操作,您可以在 Import-Module 之前尝试类似这样的操作:

RUN pwsh -c '(Get-Module VMware.PowerCLI -ListAvailable).RequiredModules | Where-Object { $_.Name -eq 'VMware.VimAutomation.HorizonView' } | Uninstall-Module'

编辑:好的,这行不通。

你可以做的只是导入你需要的模块:

# Generally most stuff needs the core module:
RUN pwsh -Command 'Import-Module -Name VMware.VimAutomation.Core -Scope CurrentUser'

# If for example you need to manipulate storage, also import that module
RUN pwsh -Command 'Import-Module -Name VMware.VimAutomation.Storage -Scope CurrentUser'

可用模块的完整列表位于 VMware.PowerCLI.psd1 文件中。这样做的好处还在于它会更快;不会导入所有命令,只导入您需要使用的命令。

【讨论】:

  • 嗯,好主意。但它失败并出现错误The module 'VMware.VimAutomation.HorizonView' of version | '7.13.0.16985899' in module base folder | '/root/.local/share/powershell/Modules/VMware.VimAutomation.HorizonView/7.13.0.16985899' cannot be uninstalled, because one or more other modules 'VMware.PowerCLI' are dependent on this module. Uninstall the modules that depend on this module before uninstalling module 'VMware.VimAutomation.HorizonView'
  • 不幸的是,在导入模块之前,最困难的方法是复制/编辑 .psd 文件并重写它以删除依赖项。使用Get-ContentOut-File 查找
  • 实际上,可能还有另一种方式——你不必导入模块所有东西,你可以只导入你需要的模块。请参阅上面我编辑的答案
  • 是的..谢谢。昨天我尝试了与RUN Get-Module -ListAvailable VMware.VimAutomation.Core | Import-Module 相同的方法。这回答了这个问题。但是要补充一点,安装和导入不会持久化,这意味着 connect-viserver 仅在构建容器时才有效,但是当我在 PHP 代码中调用它时它会失败。我还尝试添加到 $profile SHELL ["pwsh", "-command"] RUN echo "Get-Module -ListAvailable PowerCLI* | Import-Module" >> /root/.config/powershell/Microsoft.PowerShell_profile.ps1。虽然它不起作用。有什么想法吗?
  • 你试过`Install;模块 -Scope AllUsers?无论如何,要使 PS 模块工作,您唯一需要的就是模块位于模块路径中 - 有关详细信息,请参阅 $PSModulePath 变量。您不必每次都安装模块。您可以只安装;- 模块一次以获取文件,然后将文件夹/文件复制到已知的持久位置并将该位置添加到 PSModulePath。
猜你喜欢
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 2021-11-30
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多