How do I install a module using a PSM1 file? - powershell

I can not install the module pointing to the PSM1 file
Is it possible to install a .PSM1 module? With the Install-Module command I can not do it.
If it is not possible, how do I not have to import the module with Import-Module in all new sections.
Install-Module "\\servers\folders\module.PSM1"
Error Example:
Install-Module "C:\INFRA\PsSinqia.psm1"
PackageManagement\Install-Package : No match was found for the specified search criteria and module name
'C:\INFRA\PsSinqia.psm1'. Try Get-PSRepository to see all available registered module repositories.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1809 char:21
+ ... $null = PackageManagement\Install-Package #PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Packag
e], Exception
+ FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage

The correct folders are:
C:\Windows\System32\WindowsPowerShell\v1.0\Modules\
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules\
The folder and module must have the same name.
Reference: MSOnline can't be imported on PowerShell (Connect-MsolService error)

Related

Install Module GroupPolicy in PowerShell

i try Install-Module GroupPolicy:
Open a PowerShell prompt (right-click on the PowerShell icon and choose "Run as Administrator").
Install-Module -Name GroupPolicy -Force
i get error
PackageManagement\Install-Package : No match was found for the specified search criteria and module name 'GroupPolicy'. Try Get-PSRepository to see all available
registered module repositories.
At C:\Program Files (x86)\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1809 char:21
+ ... $null = PackageManagement\Install-Package #PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exception
+ FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
succeed the command:
Import-Module GroupPolicy

Error during installing PSWindows Update module

I was going to automate some regular task on client computers using Powershell module. In this way, PSWINDOWSUPDATE is one of bests. But, I receive an error during install that:
PS C:\Users\stackoverflow> install-module -name pswindowsupdate -scope currentuser
PackageManagement\Install-Package : Package 'PSWindowsUpdate' failed to be installed because:
Access to the path 'C:\Users\stackoverflow\AppData\Local\Temp\ipufiq2h\PSWindowsUpdate.dll' is denied.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1809 char:21
+ ... $null = PackageManagement\Install-Package #PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidResult: (PSWindowsUpdate:String) [Install-Package], Exception
+ FullyQualifiedErrorId : Package '{0}' failed to be installed because: {1},Microsoft.PowerShell.PackageManagement
.Cmdlets.InstallPackage
running the command in administrative mode and without -scope switch didn't solve the problem.
Edit1: The problem is solved. But there is another problem. When I call import-module pswindowsupdate following error occurs:
import-module : Errors occurred while loading the format data file:
C:\Users\stackoverflow\Documents\WindowsPowerShell\Modules\pswindowsupdate\2.2.0.2\PSWindowsUpdate.Format.ps1xml, ,
C:\Users\stackoverflow\Documents\WindowsPowerShell\Modules\pswindowsupdate\2.2.0.2\PSWindowsUpdate.Format.ps1xml: The file was skipped because of the following validation exception: File
C:\Users\stackoverflow\Documents\WindowsPowerShell\Modules\pswindowsupdate\2.2.0.2\PSWindowsUpdate.Format.ps1xml cannot be loaded because running scripts is disabled on this system. For more
information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170..
At line:1 char:1
+ import-module pswindowsupdate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Import-Module], RuntimeException
+ FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand
Use Get-ExecutionPolicy to set it e.g. to RemoteSigned:
Get-ExecutionPolicy -List
Set-ExecutionPolicy RemoteSigned
also see here

Powershell Loading Binary Module

I have few powershell script which I am trying to put into module. In the same module I also intend to load a c# dll for token generation. DLL uses System.Management.Automation.
#content of asr.psm1
Import-Module ".\tokengenerator\PowershellTokenGenerator.dll"
Get-ChildItem $psscriptroot\*.ps1 -Recurse | ForEach-Object { . $_.FullName }
The folder tokengenerator includes dll to generate OAuth2.0 token. How can I load powershell module and C# cmdlet under the same module. However, when I am trying to load the module I get the below error.
Import-Module D:\repo\src\aadsr\setup\asr.psm1
Import-Module : The specified module '.\tokengenerator\PowershellTokenGenerator.dll' was not loaded because no valid module file was found in any module directory. At D:\repo\src\aadsr\setup\asr.psm1:1 char:1
+ Import-Module ".\tokengenerator\PowershellTokenGenerator.dll"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (.\tokengenerato...enGenerator.dll:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Use:
Import-Module "$PSScriptRoot\tokengenerator\PowershellTokenGenerator.dll"
to ensure that the DLL is found relative to the script module's (*.psm1) location, reflected in automatic variable $PSScriptRoot.
By contrast, if you use Import-Module ".\...", the DLL is looked for relative to the current location (.), whatever it may be.

'Set-AzureRmDataFactoryV2' is not recognized as the name of a cmdlet

I have installed Powershell 6.0.0 on Ubuntu 16.04. I am getting following error when trying to create Azure Data Factory using Powershell
Set-AzureRmDataFactoryV2 : The term 'Set-AzureRmDataFactoryV2' 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
+ $DataFactory = Set-AzureRmDataFactoryV2 -ResourceGroupName $ResGrp.Re ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-AzureRmDataFactoryV2:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Then I tried to install the module and getting the following error
Install-Module AzureRM -AllowClobber
Untrusted repository You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): Y
PackageManagement\Install-Package : The member 'TypesToProcess' in the module manifest is not valid: Cannot find path '/tmp/787167149/Azure.Storage/.\Microsoft.WindowsAzure.Commands.Storage.Types.ps1xml' because it does not exist.. Verify that a valid value is specified for this field in the '/tmp/787167149/Azure.Storage/Azure.Storage.psd1' file. At /opt/microsoft/powershell/6.0.0/Modules/PowerShellGet/1.6.0/PSModule.psm1:2057 char:21 + ... $null = PackageManagement\Install-Package #PSBoundParameters + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (/tmp/787167149/...re.Storage.psd1:String) [Install-Package], Exception + FullyQualifiedErrorId : Modules_InvalidManifest,Microsoft.PowerShell.Commands.TestModuleManifestCommand,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
Currently, Set-AzureRmDataFactoryV2 cmdlet is not supported on Linux. AzureRM.NetCore supports following services.
Virtual Machine
App Service (Websites)
SQL Database
Storage
Network
More information please refer to this official document.
If possible, I suggest you could use Azure CLI.
I have solved this issue in my machine by updating Az.DataFactory module.
Install/update latest AzureRM.DataFactoryV2 module.
Restart your powershell.
try the above command.
it will work successfully.

Does #Requires work in module scripts?

Consider the following module script:
MyWebApp.psm1:
#Requires -Version 4
#Requires -Modules WebAdministration
function Test-MyWebApp() {
return ((Get-WebApplication 'myapp') -ne $null)
}
(Export-ModuleMember omitted for simplicity. The script still works as a module without it.)
If this were a ps1 script, the #Requires comments at the top would force PowerShell to throw an error if
The version were lower than 4
The WebAdministration module could not be loaded (imported)
But if I try to import this using Import-Module, do these have any effect? The #Requires documentation just says "scripts", but it doesn't clarify whether script modules count as "scripts" or not here. What can I do instead, if not?
No, it's treated as a normal comment
No, #Requires comments are not processed when a psm1 script is executed by calling Import-Module.
If we save the script in the question as both MyWebApp.psm1 and MyWebApp.ps1 on a machine that lacks the WebAdministration module, we get the following result:
PS> .\MyWebApp.ps1
.\MyWebApp.ps1 : The script 'MyWebApp.ps1' cannot be run because the following modules that are specified by the
"#requires" statements of the script are missing: WebAdministration.
At line:1 char:1
+ .\MyWebApp.ps1
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (MyWebApp.ps1:String) [], ScriptRequiresException
+ FullyQualifiedErrorId : ScriptRequiresMissingModules
PS> Import-Module .\MyWebApp.psm1
PS>
Importing the module succeeds, and the function now exists in the current scope. But the function will fail:
PS> Test-MyWebApp
Get-WebApplication : The term 'Get-WebApplication' 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 .\MyWebApp.psm1:5 char:14
+ return ((Get-WebApplication 'myapp') -Eq $null)
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WebApplication:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Even the -Version check is ignored. If we bump it up to 5 on a machine with only PowerShell 4:
PS> .\MyWebApp.ps1
.\MyWebApp.ps1 : The script 'MyWebApp.ps1' cannot be run because it contained a "#requires" statement for Windows
PowerShell 5.0. The version of Windows PowerShell that is required by the script does not match the currently running
version of Windows PowerShell 4.0.
At line:1 char:1
+ .\MyWebApp.ps1
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (MyWebApp.ps1:String) [], ScriptRequiresException
+ FullyQualifiedErrorId : ScriptRequiresUnmatchedPSVersion
PS> Import-Module .\MyWebApp.psm1
PS>
Use a Module Manifest
The only way to get the requirements validated properly is to use a module manifest. Unfortunately, this must be a separate file alongside the psm1 file. The following manifest will achieve what the #Requires comments are intended to do:
MyWebApp.psd1:
#
# Module manifest for module 'MyWebApp'
#
#{
ModuleVersion = '1.0'
PowerShellVersion = '4.0'
RequiredModules = #('WebAdministration')
RootModule = #('.\MyWebApp.psm1')
}
Importing this file gives the error we want:
PS> Import-Module .\MyWebApp.psd1
Import-Module : The required module 'WebAdministration' is not loaded. Load the module or remove the module from 'RequiredModules' in the file
'.\MyWebApp.psd1'.
At line:1 char:1
+ Import-Module .\MyWebApp.psd1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (.\MyWebApp.psd1:String) [Import-Module], MissingMemberException
+ FullyQualifiedErrorId : Modules_InvalidManifest,Microsoft.PowerShell.Commands.ImportModuleCommand
Unfortunately, you cannot declare the function in the same file. You must use a separate psd1 file and then explicitly declare the original psm1 script as the "root module." The root module is indicated as a path relative to the psd1 file.
Other attributes:
The ModuleVersion is required. It must be present.
PowerShellVersion accomplishes what #Requires -Version 4 intends.
RequiredModules accomplishes what #Requires -Modules WebAdministration intends.
Note that Test-MyWebApp is exported implicitly in both the psm1 and the psd1 file. This is normally controlled by Export-ModuleMember -Function in a psm1 file; the equivalent in a module manifest is FunctionsToExport. I find it simpler to just omit FunctionsToExport from the manifest and control what's exported using Export-ModuleMember in the psm1 script.