Reputation: 3626
When I run a file through the Build Events in Visual Studio I get the same error for EVERY in-built powershell command I try.
The build events window in Visual Studio
The error
------ Build started: Project: XAzure, Configuration: Debug Any CPU ------ XAzure-> C:\visual studio 2013\Projects\test\bin\XAzure.dll
Get-Date : The term 'Get-Date' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\visual studio 2013\Projects\XAzure\WaitForFiles.ps1:3 char:15 + $dateTime = $(Get-Date) + ~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-Date:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
The file only contains this one line $dateTime = $(Get-Date) and fails.
It does not matter what command I run
But if I switch out Get-Date for e.g
$start =[System.DateTime]::Now.ToString("yyyy-MM-dd-HH-mm-ss.fff")
it works just fine. But that is no solution.
I´m firing this up in the Build Events with
Powershell -ExecutionPolicy bypass -File "$(ProjectDir)WaitForFiles.ps1" "$(ProjectDir)\"
But I have also tried number of other variations like (can´t remeber them all)
Powershell -ExecutionPolicy Unrestricted -File "$(ProjectDir)WaitForFiles.ps1" "$(ProjectDir)\"
Runing this file in a powershell window works just fine so I must have the commands but not in the build events.
I´m running Visual Studio 2013 update 4 in admin mode on a win32 Windows 8.1 i7 Dell Laptop.
Running $psversiontable gives me
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.34014
BuildVersion 6.3.9600.17090
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
I have tried to give powershell more access and elevated privileges but nothing has worked.
I can´t think of anything more to add to this question or to try. I googled this for 3 hours this weekend and tried everything that I could find on this error with out success.
The strange thing is that there are lots of this "The term 'x' is not recognized.." error out there with seemingly totally different causes.
Update 1
I have tried to explicitly "install" Microsoft.PowerShell.Utiltiy (in a admin Powershell window ) with this command Import-Module Microsoft.PowerShell.Utility
but I get this error
Import-Module : The specified module 'Microsoft.PowerShell.Utility' was not loaded because no valid module file was found in any module directory.
If I run $env:PSModulePath I get
c:\Program Files\AppFabric 1.1 for Windows Server\PowershellModules;
C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement
BUT if I run %windir%\system32\WindowsPowerShell\v1.0\Modules I find C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
Does that mean that the this set of modules are not connected somehow? I see the Microsoft.PowerShell.Utiltiy folder there.
Upvotes: 2
Views: 5432
Reputation: 435
I had more luck with this answer:
PowerShell script result different when ran from Visual Studio
I believe running powershell from visual studio launches the x86 version. Hardcoding the path to...
%WINDIR%\SysNative\WindowsPowerShell\v1.0\PowerShell.exe
... made it work, especially since my script had V4 commmands.
Upvotes: 1
Reputation: 3626
I found out what was the problem. The commands that were failing were all from Microsoft.PowerShell.Utility. Just as Jisaak pointed out (give you 1 up for that thanks)
After I found that ps1 file ( Microsoft.PowerShell.Utility.ps1) and looked into it I saw PowerShellVersion="3.0".
Since Im running PowerShell version 4.0 I needed to downgrade the version Im running.
First I thought that Version -3 would do the trick but for some reason that was not an option. But by adding -Version 2 switch everything works fine.
So the following works
Powershell -Version 2 -ExecutionPolicy bypass -File "$(ProjectDir)WaitForFiles.ps1" "$(ProjectDir)\"
Upvotes: 3
Reputation: 58931
Try this one:
cmd /c powershell -executionpolicy bypass -file "$(ProjectDir)WaitForFiles.ps1"
Get-Date cmdlet is in the Microsoft.Powershell.Utility module. Maybe you could try to load it explicit:
Import-Module Microsoft.PowerShell.Utility
Upvotes: 3