6

I want to configure high trusted app for app dev in SharePoint, and to do so, i need first to insert some commands in the powershell editor like :

$publicCertPath = "C:\Certs\HighTrustSampleCert.cer" 
$certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($publicCertPath)

I am using windows PowerShell on Windows Server 2012 R2 which includes Windows PowerShell 4 that includes by default the new-object cmd-let... I don't understand though, why doesn't my operating system recognize that command ... I don't stop having the following error : New-Object : The term 'New-Object' is not recognized as the name of a cmdlet.

When i open powerShell i get this :

*select :

The term 'Select-Object' 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:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\CONFIG\POWERSHELL\Registration\SharePoint.ps1:1 char:16 + $ver = $host | select version + ~~~~~~ + CategoryInfo : ObjectNotFound: (Select-Object:String) [], Comma ndNotFoundException + FullyQualifiedErrorId : CommandNotFoundException Set-location : The term 'Set-location' 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:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\CONFIG\POWERSHELL\Registration\SharePoint.ps1:4 char:1 + Set-location $home + ~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Set-location:String) [], Comman dNotFoundException + FullyQualifiedErrorId : CommandNotFoundException*

I thought that was normal until today... does it have any relation with the error? And here is the hole (new-object) exception stack:

New-Object : The term 'New-Object' 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 line:1 char:16 + $certificate = New-Object System.Security.Cryptography.X509Certificates.X509Cert ... + ~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (New-Object:String) [], CommandN otFoundException + FullyQualifiedErrorId : CommandNotFoundException

ps: i want to mention that when i used enter-psSession and worked remotely, the command new-object was recognized but sharepoint commands (like Get-SPAuthenticationRealm) were no more recognized ... And it's like there is a problem related to the operating system.

Haithem KAROUI
  • 1,533
  • 4
  • 18
  • 39
  • 2
    You've tried Get-Command and/or Get-Help to see whether it's recognized, right. Very strange to get that error, should be available by default. – Patrick87 Jul 04 '14 at 15:03
  • 1
    The `New-Object` cmdlet is available since PowerShell 1.0. It's also exported by the `Microsoft.PowerShell.Utility` module, so if you don't have that loaded in your PowerShell session, that could trigger an error like that. Does the result list of `Get-Module` contain this module? – PeterK Jul 05 '14 at 10:20
  • Are you connecting to a remote endpoint using enter-pssession? Sounds liked something that would occur with a constrained PowerShell endpoint. – boeprox Jul 05 '14 at 19:42
  • Please give the exact exception stack. – JPBlanc Jul 06 '14 at 07:18
  • @Patrick87 No, it does not recognize it. – Haithem KAROUI Jul 07 '14 at 07:29
  • @PeterK, can you explain more please? – Haithem KAROUI Jul 07 '14 at 07:30
  • @boeprox, i tried first with remote session and used enter-pssession, it does not work, then i switched to work directly on the sever machine and it does not work either. – Haithem KAROUI Jul 07 '14 at 07:30
  • @JPBlanc i edited the post to include the hole stakc – Haithem KAROUI Jul 07 '14 at 07:31
  • 1
    @KarouiHaythem: Yes, of course. PowerShell is modularized. Certain cmdlets like `New-Object` are only available if the module that implements them is loaded. `New-Object`, in particular is provided by the `Microsoft.PowerShell.Utility` module. You can verify if this module is loaded in your session by running the `Get-Module` cmdlet. This will give you the list of loaded modules and `Microsoft.PowerShell.Utility` should be in the list. This should confirm if you have `New-Object` available at all. – PeterK Jul 07 '14 at 09:00
  • @PeterK seems like i have serious problem... Get-Module get me blank ... nothing is loaded seems, what should i do ? – Haithem KAROUI Jul 07 '14 at 10:20
  • 2
    Run `Get-Module -ListAvailable' to get the list of PowerShell modules available for loading. This should include `Microsoft.PowerShell.Utility`. If it's in the list, you can just run `Import-Module Microsoft.PowerShell.Utility` to load it. By the way this module is among the Core Modules of PowerShell so the default session should include them. If this is a just a regular PowerShell session, you may want to try repairing your PowerShell installation. – PeterK Jul 07 '14 at 10:34
  • @PeterK i tried already to uninstall - reinstall powershell feature i got an error while unistalling it... :/ – Haithem KAROUI Jul 07 '14 at 11:27
  • @KarouiHaythem: It sounds like your PowerShell installation is corrupted. What was the error you got? – PeterK Jul 07 '14 at 11:33
  • @PeterK i finally fixed it i went to powerShell installation files on another server and copied the files inside the c:\ProgramFiles\WindowsPowerShell so it recognizes now the commands... thank you peter for you assistance and patience... you can post your comment as answer and i will mark it aswell. – Haithem KAROUI Jul 07 '14 at 11:55
  • 1
    @KarouiHaythem: Thank you. I'm not sure if I deserve the credit as you did the work, but I've posted my answer anyway for future reference. You may want to add the way you have fixed the problem eventually. Thanks. – PeterK Jul 07 '14 at 12:30

3 Answers3

4

It appears that your PowerShell installation is corrupted and needs to be repaired. The New-Object cmdlet is exported by the Microsoft.PowerShell.Utility module, which is one of the Core PowerShell modules and should be imported by default on all PowerShell installations.

PeterK
  • 3,667
  • 2
  • 17
  • 24
1

This can be because the Registry key entry for PSModulesPath is not pre-filled with the default PowerShell Modules path.

$PSModulePath = Get-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Control\Session Manager\Environment" -Name "PSModulePath"

$newPSModulePath = $PSModulePath.PSModulePath + ";C:\Windows\System32\WindowsPowerShell\v1.0\Modules"

Set-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Control\Session Manager\Environment" -Name "PSModulePath" -value $newPSModulePath
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
-2

Nothing verified here, but I am advancing the hypothesis that Powershell has run into a runtime error that caused it to corrupt its process.

Kevin Buchs
  • 2,520
  • 4
  • 36
  • 55
  • 3
    have you read the comments? The problem was in the installation and peter helped me figurin' it out... i don't see any reason for doubt – Haithem KAROUI Apr 05 '16 at 10:22