PowerShell 2.0 cannot handle space in $Env:PSModulePath? - powershell

It appears that PowerShell 2.0 cannot handle a space in the Env:PSModulePath variable when looking for a module. I have the function in a .psm1 file in a directory under the "\ORGHOST\USRSHR\USERS\lit\My Documents\WindowsPowerShell\Modules" directory.
My organization requires that my Windows document directory be on a network share. This causes the $Env:PSModulePath variable to have "My Documents" as part of the path in the first entry. No, I cannot change the policies set by the organization.
It sounds like I should iterate over the paths in $Env:PSModulePath and find the short (8.3) name for each, then set $Env:PSModulePath using those in my profile script. I would welcome a better suggestion. Any ideas?
If anyone could confirm that this is a problem in PowerShell 2.0, that would be helpful in pushing the organization to upgrade to PowerShell 5.0.
PS 5.0
PS C:\src\powershell> $Env:PSModulePath -split ";"
\\ORGHOST\ORGSHR\Users\lit\My Documents\WindowsPowerShell
C:\Program Files\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules
PS 2.0
PS C:\Users\lit> $Env:PSModulePath -split ";"
\\ORGHOST\ORGSHR\USERS\lit\My Documents\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\
The term 'Phs-RunBat' 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 \\ORGHOST\USRSHR\Users\lit\Scripts\PSAutomation\Phs-RunRemoteBat.ps1:17 char:23
+ [int32]$r = Phs-RunBat <<<< -ComputerName $ComputerName -ScriptPath $ScriptPath -LogFile $LogFile -Verbose
+ CategoryInfo : ObjectNotFound: (Phs-RunBat:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Related

Not able to execute the "Import-module"

I get below error when i try to execute the command "Import-module script.ps1"
any idea why is that shows up? what need to be done to resolve the issue ?
PS C:\File> Import-module script.ps1
Import-module : The specified module 'script.ps1' was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ Import-module script.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (script.ps1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
You need to use .\ to execute scripts, commands from current directory. i.e. ipmo .\script.ps1
As a security feature, PowerShell does not run executable (native)
commands, including PowerShell scripts, unless the command is located
in a path that is listed in the Path environment variable $env:path or
unless you specify the path to the script file.
To run a script that is in the current directory, specify the full
path, or type a dot .\ to represent the current directory.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_command_precedence?view=powershell-7

The term 'Set-AzureKeyVaultKeyAttributes' is not recognized as the name of a cmdlet,

On my Windows 10 I've Powershell 5.0 installed. But when I run the following command on it, I get the above error.
PS command:
Set-AzureKeyVaultKeyAttributes -Name Key2 -VaultName KayVault-abctest -Enable $false
Error
Set-AzureKeyVaultKeyAttributes : The term 'Set-AzureKeyVaultKeyAttributes' 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:1
+ Set-AzureKeyVaultKeyAttributes -Name Key2 -VaultName KayVault-abctest ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-AzureKeyVaultKeyAttributes:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Note
I'm using the topic Create a Key (Portal) in this official Azure lab tutorial.
I believe the name of the cmdlet should be the singular Set-AzureKeyVaultKeyAttribute, rather than the plural Set-AzureKeyVaultKeyAttributes. Note that this is one of the Strongly Encouraged Development Guidelines for Powershell by Microsoft:
To enhance the user experience, the noun that you choose for a cmdlet name should be singular. For example, use the name Get-Process instead of Get-Processes. It is best to follow this rule for all cmdlet names, even when a cmdlet is likely to act upon more than one item.
If you are still seeing the same problem while using a singular name, ensure that the AzureRM.KeyVault module is installed and loaded:
Install-Module AzureRM.KeyVault
Import-Module AzureRM.KeyVault

Get-QADUser' is not recognized as the name of a cmdlet

I am trying to take users from AD with some filter criteria but while executing Get-QADUser' command I am getting following error.
Get-QADUser : The term 'Get-QADUser' 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:1
+ Get-QADUser
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-QADUser:String) [], CommandNotFoundExce
ption
+ FullyQualifiedErrorId : CommandNotFoundException
Can Anybody tel me how to resolve this?
I using following reference link
http://wiki.powergui.org/index.php/Get-QADUser
QAD cmdlets FAQ
To load QAD cmdlets for one particular script:
Add this line in the beginning of the script:
Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction Stop
To make QAD cmdlets available for all your scripts on a particular computer:
Open your PowerShell profile (for example, by starting PowerGUI Script Editor and selecting File / Open Current User Profile). Add this line to the profile code:
Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction Stop
PS. Original Quest article was recommending -ErrorAction SilentlyContinue but I cannot agree with such.

New-Object : The term New-Object is not recognized as the name of a cmdlet

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.
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.
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
Nothing verified here, but I am advancing the hypothesis that Powershell has run into a runtime error that caused it to corrupt its process.

Windows sharepoint services (WSS 3.0) stsadm not working

I am new to powershell. I am trying to backup a sharepoint site using a stsadm command but for some reason I keeps on giving me some error.
Here is what I am doing:
PS C:\Users\Administrator> Stsadm –o backup –url >http://server< name:port -filename path\filename to be used for the backup file
Here is the error I am getting:
The term 'stsadm' is not recognized as the name of the cmdlet, function, script file, or operable program. Check the spelling of the name, or path was included, verify that the path is correct and try again,
At line:1 char:7
+ stsadm <<<< stsadm –o backup –url >http://server< name:port -filename path\filename to be used for the backup file
+CategoryInfo : ObjectNotFound: [], CommandNotFoundException
+FullyQualifiedErrorID : CommandNotFoundException
Stsadm is not a powershell script, it is an executable located in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12 or 14 'hive'.
Normally you would refrain from calling stsadm when running powershell as it is effectively deprecated in SharePoint 2010.
You can however change directory to locate the executable and call it if you wish.
Here is a list of stsadm equivalents in powershell.
check this blog post => Backup and Restore SharePoint 2010 Site Collection with PowerShell: http://www.bradleyschacht.com/backup-and-restore-sharepoint-2010-site-collection-with-powershell/
Perhaps, it is what you are looking for