<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: PowerCLI script runs fine normally, fails when called by apcupsd (linux) in VMware PowerCLI Discussions</title>
    <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891669#M105141</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.vmware.com/t5/user/viewprofilepage/user-id/256147"&gt;@LucD&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;If Install-Module installs in a User directory, that means the Scope is the default, namely CurrentUser.&lt;BR /&gt;When you do with the Scope AllUsers, it should be in a folder accessible to all users.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;That was one of the first things I tried to resolve the modules not being accessible, but I can't recall if I did it with a fresh powershell install or not. Will give that a try.&lt;/P&gt;</description>
    <pubDate>Wed, 02 Feb 2022 21:59:47 GMT</pubDate>
    <dc:creator>snktech</dc:creator>
    <dc:date>2022-02-02T21:59:47Z</dc:date>
    <item>
      <title>PowerCLI script runs fine normally, fails when called by apcupsd (linux)</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891483#M105119</link>
      <description>&lt;P&gt;So I've written a PowerCLI script to use with apcupsd to shutdown any VMs running on NAS datastores and shut them down first (so the NAS can then shut down).&lt;/P&gt;&lt;P&gt;Here's a cut down version of the script - ignore the variables that aren't initialised, gets pulled from a file and works fine but clutters things up so I took it out. When I run this from the command line, it works perfectly. Does exactly what I want it to do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;#!/usr/bin/powershell/pwsh

Write-Output "PS Environment Variables: "
Write-Output $Env:PSModulePath

$server = $($item.SERVER)
$user = $($item.USER)
$pass = ConvertTo-SecureString $($item.PASSWORD) -AsPlainText -Force

# Create credential object
$pscreds = New-Object System.Management.Automation.PSCredential ($user, $pass)

# Connect to server
Connect-VIServer $server -Credential $pscreds

# Find any datastore with NAS as part of the name, grab any running VM's and issue a guest shutdown
Get-Datastore | Where{$_.name -like '*NAS*'} | Get-VM | Where{$_.PowerState -eq 'PoweredOn'} | Shutdown-VMGuest -Confirm:$false&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But when I then add it to the APCUPSD config it gets called when it's supposed to.. and fails with the following output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;PS Environment Variables:
/tmp/9d12ecbf-5362-4faa-98a3-d45f001a03c5/.local/share/powershell/Modules:/usr/local/share/powershell/Modules:/usr/bin/powershell/Modules

    Directory: /usr/bin/powershell/Modules

ModuleType Version    PreRelease Name                                PSEdition
---------- -------    ---------- ----                                ---------
Script     12.5.0.19…            VMware.CloudServices                Desk
&amp;lt;snip - lists all modules&amp;gt;
Script     12.1.0.16…            VMware.VumAutomation                Desk

Connect-VIServer: /root/scripts/VMShutDown/test.ps1:16
Line |
  16 |      Connect-VIServer $server -Credential $pscreds
     |      ~~~~~~~~~~~~~~~~
     | The 'Connect-VIServer' command was found in the module
     | 'VMware.VimAutomation.Core', but the module could not be
     | loaded. For more information, run 'Import-Module
     | VMware.VimAutomation.Core'.

Get-Datastore: /root/scripts/VMShutDown/test.ps1:19
Line |
  19 |      Get-Datastore | Where{$_.name -like '*NAS*'} | Get-VM | Where{$_. …
     |      ~~~~~~~~~~~~~
     | The type initializer for
     | 'VMware.VimAutomation.Sdk.Interop.V1.CoreServiceFactory' threw
     | an exception.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;No difference other than it's being run by apcupsd so my thought was some kind of environment variable missing, but I specifically installed the module into the /usr/bin/powershell/Modules directory with the following, and it works just fine when called via the command line&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Find-Module -Name 'VMware.PowerCLI' -Repository 'PSGallery' | Save-Module -Path /usr/bin/powershell/Modules
Import-Module -FullyQualifiedName '/usr/bin/powershell/Modules/VMware.PowerCLI'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So pretty stumped. Any ideas welcome.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 04:10:42 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891483#M105119</guid>
      <dc:creator>snktech</dc:creator>
      <dc:date>2022-02-02T04:10:42Z</dc:date>
    </item>
    <item>
      <title>Re: PowerCLI script runs fine normally, fails when called by apcupsd (linux)</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891493#M105121</link>
      <description>&lt;P&gt;Why do you hae the /tmp folder in the PSModulePath variable?&lt;BR /&gt;I would suggest to remove all PowerCLI related directories in all folders mentioned in PSModuePath.&lt;BR /&gt;Then do a fresh install with Install-Module.&lt;BR /&gt;&lt;BR /&gt;Not sure why you are doing the Save-Module part?&lt;BR /&gt;That should only be required when installing on a station that has no connection to the PSGallery.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 07:27:23 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891493#M105121</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2022-02-02T07:27:23Z</dc:date>
    </item>
    <item>
      <title>Re: PowerCLI script runs fine normally, fails when called by apcupsd (linux)</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891517#M105126</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.vmware.com/t5/user/viewprofilepage/user-id/256147"&gt;@LucD&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Why do you hae the /tmp folder in the PSModulePath variable?&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;That's something that only shows up when the script is called by the system, presumably it creates its own temporary space for that session in case it's needed.&lt;/P&gt;&lt;P&gt;The path when run as root is:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;PS /root&amp;gt; Write-Output $Env:PSModulePath
/root/.local/share/powershell/Modules:/usr/local/share/powershell/Modules:/usr/bin/powershell/Modules&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.vmware.com/t5/user/viewprofilepage/user-id/256147"&gt;@LucD&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Not sure why you are doing the Save-Module part?&lt;BR /&gt;That should only be required when installing on a station that has no connection to the PSGallery.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Initially the modules were being installed in /root/.local/share/powershell/Modules and only available to root when run from the command line (or whatever other user installed the module). When run by the system even as root the same environment variable isn't used.&lt;/P&gt;&lt;P&gt;Pulling the module down with Save-Module then forcing it to install to /usr/bin/powershell/Modules made it available. Prior to this I installed it as per normal with just Install-Module however the PowerCLI commands were not available at all.&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.vmware.com/t5/user/viewprofilepage/user-id/256147"&gt;@LucD&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;I would suggest to remove all PowerCLI related directories in all folders mentioned in PSModuePath.&lt;BR /&gt;Then do a fresh install with Install-Module.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Yep was how I initially had it but the module was not available to the system, hence the Save-Module. I've tried multiple fresh installs - they all work just fine when running the script directly or using the commands from a session, but they won't work when called by the system.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 10:01:50 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891517#M105126</guid>
      <dc:creator>snktech</dc:creator>
      <dc:date>2022-02-02T10:01:50Z</dc:date>
    </item>
    <item>
      <title>Re: PowerCLI script runs fine normally, fails when called by apcupsd (linux)</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891555#M105128</link>
      <description>&lt;P&gt;If Install-Module installs in a User directory, that means the Scope is the default, namely CurrentUser.&lt;BR /&gt;When you do with the Scope AllUsers, it should be in a folder accessible to all users.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 13:21:54 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891555#M105128</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2022-02-02T13:21:54Z</dc:date>
    </item>
    <item>
      <title>Re: PowerCLI script runs fine normally, fails when called by apcupsd (linux)</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891669#M105141</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.vmware.com/t5/user/viewprofilepage/user-id/256147"&gt;@LucD&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;If Install-Module installs in a User directory, that means the Scope is the default, namely CurrentUser.&lt;BR /&gt;When you do with the Scope AllUsers, it should be in a folder accessible to all users.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;That was one of the first things I tried to resolve the modules not being accessible, but I can't recall if I did it with a fresh powershell install or not. Will give that a try.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 21:59:47 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891669#M105141</guid>
      <dc:creator>snktech</dc:creator>
      <dc:date>2022-02-02T21:59:47Z</dc:date>
    </item>
    <item>
      <title>Re: PowerCLI script runs fine normally, fails when called by apcupsd (linux)</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891676#M105143</link>
      <description>&lt;P&gt;Make sure to first remove the installation in all user directories.&lt;BR /&gt;Unfortunately Install-Module allows you to install in both scopes, without even requiring a confirmation.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 22:12:47 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891676#M105143</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2022-02-02T22:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: PowerCLI script runs fine normally, fails when called by apcupsd (linux)</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891700#M105151</link>
      <description>&lt;P&gt;Still no luck. Completely removed Powershell then went through the PS Environment variables and checked every single directory to make sure there was nothing there, did a new install, then added the module with:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Install-Module -Name 'VMware.PowerCLI' -Scope AllUsers&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Installed just fine, this time to /usr/local/share/powershell/Modules which the log of the script shows is where the module is being pulled from. But still getting the same result:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;PS Environment Variables:
/tmp/cd37f0ff-da59-43ac-84e1-c8e2c8e4b78b/.local/share/powershell/Modules:/usr/local/share/powershell/Modules:/usr/bin/powershell/Modules

    Directory: /usr/local/share/powershell/Modules

ModuleType Version    PreRelease Name                                PSEdition
---------- -------    ---------- ----                                ---------
Script     12.5.0.19…            VMware.CloudServices                Desk
etc.

Connect-VIServer: /root/scripts/VMShutDown/test.ps1:16
Line |
  16 |      Connect-VIServer $server -Credential $pscreds
     |      ~~~~~~~~~~~~~~~~
     | The 'Connect-VIServer' command was found in the module
     | 'VMware.VimAutomation.Core', but the module could not be
     | loaded. For more information, run 'Import-Module
     | VMware.VimAutomation.Core'.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Only thing I can think of is to redeploy the server entirely but somewhat at a loss as to what that would achieve given powershell is just an extracted directory.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 23:46:58 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891700#M105151</guid>
      <dc:creator>snktech</dc:creator>
      <dc:date>2022-02-02T23:46:58Z</dc:date>
    </item>
    <item>
      <title>Re: PowerCLI script runs fine normally, fails when called by apcupsd (linux)</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891728#M105154</link>
      <description>&lt;P&gt;OK... solved it!&lt;/P&gt;&lt;P&gt;Started just printing everything I could for debugging and saw there was no HOME var (linux var, not powershell). Apparently if PowerCLI doesn't have a HOME environment variable set, it won't work no matter where you put the modules. Added the following to the script that calls the ps1, problem solved (obviously if using a different service account would need to change accordingly).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;export HOME=/root&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I assume it's due to PowerCli storing its settings in ~/.local/share/VMware/PowerCLI&amp;nbsp; and not able to pick them up without a HOME var.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: just a suggestion if anyone wants it, but if practical I'd very much recommend moving to a "check if config exists in ~ if not use the default from /etc" or similar way of doing things.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Feb 2022 05:13:14 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-runs-fine-normally-fails-when-called-by-apcupsd/m-p/2891728#M105154</guid>
      <dc:creator>snktech</dc:creator>
      <dc:date>2022-02-03T05:13:14Z</dc:date>
    </item>
  </channel>
</rss>

