1

I am calling the below script in my post build configurations:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -file "\Sclddev8\tfs\Scripts\slack-notice.ps1" -Verb RunAs;

However, I keep getting this error:

Send-SlackMessage : The term 'Send-SlackMessage' is not recognized as the name

But I have installed this module in my environment and if I open a PowerShell console or run the file outside of this build process, works without issue.

Batman
  • 5,563
  • 18
  • 79
  • 155
  • I have the same issue: I run Powershell script from MSBuild as Exec task. I took the same command and then ran it in a CMD session, and it works. It works in every scenario aside being run from MSBuild. I am at lost here. – Gerino Mar 30 '19 at 23:55

2 Answers2

3

When you install a Powershell module, you are technically importing the module from your profile every time you open a new Powershell window. By running Powershell with the "-NoProfile" switch, you're preventing the module from being imported (even though it's "installed" and the files are present).

What may be your best option, if you want to keep the "-NoProfile" switch active, is to have a line at the top of your script to import the module before continuing. If you're using Warren Frame's "PSSlack" module, the command you need is:

> Import-Module PSSlack
Bryce McDonald
  • 1,760
  • 12
  • 22
  • tried and got this: "Import-Module : The specified module 'PSSlack' was not loaded because no valid module file was found in any module directory." – Batman Oct 24 '17 at 19:43
  • 2
    You may need to reference the actual .psm1 then. `Import-Module C:\Users\UserName\Documents\WindowsPowerShell\Modules\PSSlack\PSSlack.psm1` – Bryce McDonald Oct 24 '17 at 19:45
  • Thank you that worked. I'm not sure why it's not find these things without having to provide the full path, that's strange. – Batman Oct 24 '17 at 19:51
  • I'll be honest, I'm not exactly sure how out of bounds "-NoProfile" plays into things. I'd be willing to bet that it even runs as a dummy user's process, so it has no environment profile path to follow. As much as NoProfile is a best practice, I've run into more trouble with it than anything. When you're using it, it's best to be explicit on everything else you're calling. – Bryce McDonald Oct 24 '17 at 19:58
  • Any ideas on how to provide arguments to `%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy Unrestricted -file "\\Sclddev8\tfs\Scripts\slack-notice.ps1 -myParam $(TargetDatabase)" -Verb RunAs;"` – Batman Oct 24 '17 at 20:06
  • You may need to put another $ in front of TargetDatabase so it looks like this: `%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy Unrestricted -file "\\Sclddev8\tfs\Scripts\slack-notice.ps1 -myParam $($TargetDatabase)" -Verb RunAs;"` Then what I'd do is set $TargetDatabase to something, and pipe $TargetDatabase into the command. You could also play with splatting (I haven't had much luck there) or switch gears and try and use Invoke-Command. – Bryce McDonald Oct 24 '17 at 20:14
  • Hey yea I go it working, thanks a lot for your help. – Batman Oct 24 '17 at 20:15
  • This may also be helpful https://stackoverflow.com/questions/4225748/how-do-i-pass-named-parameters-with-invoke-command and I'm glad to hear it! – Bryce McDonald Oct 24 '17 at 20:16
0

I hit the same issue.

What helped was... copying the folder into C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules.

Yup, it makes a difference.

Gerino
  • 1,943
  • 1
  • 16
  • 21