0

I installed powershell in the php debian docker image and now I am installing PowerCLI module in the docker image to access the vsphere info and display using laravel. The issue is with the PowerCLI installation. Powershell doesn't seem to persist the modules installed and imported. When I import module and use RUN pwsh -c connect-viserver, it seems working in the docker-image. But when I call the cmdlet in laravel container as $process = new Process(['pwsh', '-c','Connect-VIServer', 'SERVERNAME']); it fails. I check to see if it is imported iin the powershell by accessing container docker exec -it app bash But the module is not installed. I am manually keeping the modules in a folder powershell/Modules and adding it to $env:$PSModulePath

I do not understand what I'm missing.

Here's is my docker file.

FROM php:7.4-fpm

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

# 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

# Clear cache
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

#########################
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 -ParticipateInCEIP $false -Confirm:$false
#RUN Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
##RUN connect-viserver 10.21.24.9


RUN Get-Module -ListAvailable VMware.VimAutomation.Core | Import-Module
RUN $env:PSModulePath = $env:PSModulePath + ":/var/www/powershell/Modules"

RUN mkdir -p /home/www/.config/powershell
RUN touch /home/www/.config/powershell/Microsoft.PowerShell_profile.ps1
RUN echo "Get-Module -ListAvailable VMware.VimAutomation.Core | Import-Module" >> /home/www/.config/powershell/Microsoft.PowerShell_profile.ps1

RUN Get-Module
SHELL ["/bin/sh", "-c"]
# 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
# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
RUN pwd
RUN ls -la && ls -la /var/www/powershell/Modules/
Vamshi
  • 103
  • 6
  • The `Import-Module` cmdlet adds one or more modules to **the current session** but `new Process(['pwsh', …` creates a new instance. Either add `Import-Module …` to a profile, or use it in the `-c` explicitly. – JosefZ Nov 23 '20 at 10:15
  • Actually I am adding it to profile as ```RUN echo "Get-Module -ListAvailable VMware.VimAutomation.Core | Import-Module" >> /home/www/.config/powershell/Microsoft.PowerShell_profile.ps1```. But not working. – Vamshi Nov 23 '20 at 10:35

1 Answers1

0

This may be related to PowerCLI on Debian Stretch: The type initializer for 'VMware.VimAutomation.ViCore.Util10.SettingsManager' threw an exception.

I had issues calling pwsh using salt cmd.run until setting HOME env to /tmp

Example:

salt redacted.server cmd.run 'pwsh -nol -noni -c Import-Module VMware.VimAutomation.Core'
redacted.server:
Import-Module: The type initializer for 'VMware.VimAutomation.ViCore.Util10.SettingsManager' threw an exception.
Andrew Schulman
  • 8,561
  • 21
  • 31
  • 47