title: rundeck unable to execute powershell script with import-module
body: <p>I am running several python scripts via rundeck (in-line) on windows 2012 target node. These scripts used to run locally but now in the process of moving to rundeck.</p>
  
  <p>One of the python scripts opens subprocess to invoke a powershell script and read the output.</p>
  
  <pre><code>import subprocess
  CMD = [r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe ', '-File', r'C:\Users\Osman\Code\mop.ps1']
  cmd = CMD[:]
  cmd.append('arg1')
  cmd.append('arg2')
  cmd.append('arg3')
  p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  r = p.communicate()
  print(r)
  </code></pre>
  
  <p>The mop.ps1 is</p>
  
  <pre><code>Import-Module MSOnline
  $domain = $args[0]
  $login = $args[1]
  $pass = $args[2]
  $EPass = $pass | ConvertTo-SecureString -AsPlainText -Force
  $Cred = New-Object System.Management.Automation.PsCredential($login, $EPass)
  Connect-MsolService -Credential $Cred
  $TenantId = Get-MsolPartnerContract -Domain $domain | Select-Object -ExpandProperty TenantId
  Get-MsolAccountSKU -TenantId $TenantId | select SkuPartNumber,ActiveUnits,ConsumedUnits | ConvertTo-Csv -NoTypeInformation
  </code></pre>
  
  <p>This part of the code always fails to execute and if I check stderr it says:</p>
  
  <pre><code>        Connect-MsolService : Exception of type 'Microsoft.Online.Administration.Automation.MicrosoftOnlineException' was thrown.
  At C:\Users\Osman\Code\mop.ps1:7 char:1 
  + Connect-MsolService -Credential $Cred
  </code></pre>
  
  <p>I'm not sure why it fails. I tried </p>
  
  <pre><code>Import-Module MSOnline -Verbose
  </code></pre>
  
  <p>And I can see Cmdlets being loaded. I tried creating profile.ps1 file in <code>C:\WINDOWS\system32\WindowsPowerShell\v1.0\</code> location. </p>
  
  <p>Everything works fine if I execute the code locally. I tried running a regular test .ps1 file 'disk.ps1' instead of my code, and that works fine because it doesn't load any modules: </p>
  
  <pre><code>get-WmiObject win32_logicaldisk -Computername $env:computername
  </code></pre>
  
  <p>What's the workaround to get a script with module to run properly? stdout is always empty. </p>
  
  <p>Node is registered as 64 bit so I tried changing the cmd to </p>
  
  <pre><code>C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe 
  </code></pre>
  
  <p>I tried to copy profile.ps1 there, copied the Module there but still doesn't work via rundeck. </p>