The specified module ActiveDirectory was not loaded - powershell

I have an SSIS job that executes a powershell module that's attempting to pull information from Active Directory. However, when I run the script I get this error:
The specified module ActiveDirectory was not loaded because no valid
module file was found in any module directory
I found this similar question, however they are using Windows Server 2008, whereas I am using Windows Server 2016. I tried to follow the instructions in the question, but I wasn't able to locate neither "Remote Server Administration Tools" nor "Active Directory module for Windows Powershell."

Can you check whether Active Directory Web Services service is running under services?
If not start it and run below command
Get-module -list
You can check whether Active Directory module is listed down.
Run below command to Enable Active Directory module
Add-WindowsFeature RSAT-AD-PowerShell

Related

"The Azure PowerShell session has not been properly initialized" error message in Octopus

I am trying to run the Get-AzureRmEventHubNamespaceKey cmdlet in an Azure Powershell step within Octopus.
I am getting the following error:
Get-AzureRmEventHubNamespaceKey : The Azure PowerShell session has not been properly
initialized. Please import the module and try again
The module is installed in the following directory on the Octopus server:
C:\Program Files (x86)\Microsoft
SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureRM.EventHub
I have tried importing the module first as part of the same step:
Import-Module –Name "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureRM.EventHub" -Verbose
And I can see in the output that it has been imported:
VERBOSE: Importing cmdlet 'Get-AzureRmEventHubNamespaceKey'.
But it is immediately followed by the above error. If I RDP to the octopus server and run directly from there it runs fine.
Any ideas on what might be causing this?
To use any Azure related commands from your machine, you need to log in first.
Note that there are several Azure modules, and each has a different login cmdlet, but the link above is specific to the module you're using.

'Connect-MsolService' is not recognized as the name of a cmdlet

PSCommand commandToRun = new PSCommand();
commandToRun.AddCommand("Connect-MsolService");
commandToRun.AddParameter("Credential", new PSCredential(msolUsername, msolPassword));
powershell.Streams.ClearStreams();
powershell.Commands = commandToRun;
powershell.Invoke();
I am trying to run above code in visual studio and getting the following error :
The term 'Connect-MsolService' 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.
But I am able to Connect to Msol service from Microsoft Azure Active Directory Module for Windows PowerShell. Please help.
I had to do this in that order:
Install-Module MSOnline
Install-Module AzureAD
Import-Module AzureAD
All links to the Azure Active Directory Connection page now seem to be invalid.
I had an older version of Azure AD installed too, this is what worked for me.
Install this.
Run these in an elevated PS session:
uninstall-module AzureAD # this may or may not be needed
install-module AzureAD
install-module AzureADPreview
install-module MSOnline
I was then able to log in and run what I needed.
This issue can occur if the Azure Active Directory Module for Windows PowerShell isn't loaded correctly.
To resolve this issue, follow these steps.
1.Install the Azure Active Directory Module for Windows PowerShell on the computer (if it isn't already installed). To install the Azure Active Directory Module for Windows PowerShell, go to the following Microsoft website:
Manage Azure AD using Windows PowerShell
2.If the MSOnline module isn't present, use Windows PowerShell to import the MSOnline module.
Import-Module MSOnline
After it complete, we can use this command to check it.
PS C:\Users> Get-Module -ListAvailable -Name MSOnline*
Directory: C:\windows\system32\WindowsPowerShell\v1.0\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 1.1.166.0 MSOnline {Get-MsolDevice, Remove-MsolDevice, Enable-MsolDevice, Disable-MsolDevice...}
Manifest 1.1.166.0 MSOnlineExtended {Get-MsolDevice, Remove-MsolDevice, Enable-MsolDevice, Disable-MsolDevice...}
More information about this issue, please refer to it.
Update:
We should import azure AD powershell to VS 2015, we can add tool and select Azure AD powershell.
Following worked for me:
Uninstall the previously installed ‘Microsoft Online Service Sign-in Assistant’ and ‘Windows Azure Active Directory Module for Windows PowerShell’.
Install 64-bit versions of ‘Microsoft Online Service Sign-in Assistant’ and ‘Windows Azure Active Directory Module for Windows PowerShell’.
https://littletalk.wordpress.com/2013/09/23/install-and-configure-the-office-365-powershell-cmdlets/
If you get the following error In order to install Windows Azure Active Directory Module for Windows PowerShell, you must have Microsoft Online Services Sign-In Assistant version 7.0 or greater installed on this computer, then install the Microsoft Online Services Sign-In Assistant for IT Professionals BETA:
http://www.microsoft.com/en-us/download/details.aspx?id=39267
Copy the folders called MSOnline and MSOnline Extended from the source
C:\Windows\System32\WindowsPowerShell\v1.0\Modules\
to the folder
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules\
https://stackoverflow.com/a/16018733/5810078.
(But I have actually copied all the possible files from
C:\Windows\System32\WindowsPowerShell\v1.0\
to
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\
(For copying you need to alter the security permissions of that folder))

Import-Module WebAdministration wont load from script but does from command line

I'm coming onto a project that uses PowerShell to script the build. The build makes use of the WebAdministration module to manage the local IIS instance. When I run the build script the following error is thrown when trying to import WebAdministration.
Error: 06/29/2016 17:28:35: At
C:\dev\src\nib-ravendb\build\ConfigureIis.ps1:10 char:1 +
Import-Module WebAdministration + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~
[<<==>>] Exception: The specified module 'WebAdministration' was not
loaded because no valid module file was fo und in any module
directory. ERROR: 1
How ever when I run Import-Module WebAdministration at the PowerShell command line the module is imported and I can use features from it. Subsequently running the build script still fails.
I have IIS 7.5 and PowerShell 4
Does anyone have an idea why this import would be failing in the script but not at the command line, and how to fix it?
For servers you need to install the role Management Tools under Web Server (IIS) to use the WebAdministration module. To see if you have the module available use Get-Module -ListAvailable.
For Windows 7 to 10 you will need to install the feature IIS Management Scripts and tools under Internet Information Services >> Web Management Tools.
You could try manually locating the WebAdministration .psd1 file and then import it. Use $env:psmodulepath to help locate where your modules are stored then run:
Import-Module -Name 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration\WebAdministration.psd1'
If Server 2008 you could try the following but this may not work on 2012 and upwards:
Add-PSSnapin WebAdministration
Note You will need to run the script with administrator rights to be able to load the WebAdministration module with Import-Module or Add-PSSnapin.
Also check that you have PowerShell's execution Policy set to Unrestricted:
Set-ExecutionPolicy unrestricted
You might want to see this Question.
I had the same situation, i've fixed it installing the Windows Feature Web-Scripting-Tools on W2016 Server:
Add-WindowsFeature Web-Scripting-Tools
In the end there was a problem something, possibly chocolatey?, was truncating $env:PSModulePath to the first entry, this is why the script was working if I typed it in but not in the script.
I found it by logging $env:PSModulePath at different points in the scripts that I was running.
I worked around it by reordering the entries in $env:PSModulePath.
Have a look at #Richard's answer for some other good suggestions.
In my case (Windows 10) I was using Powershell 7 and this simply refused to install the WebAdministration module, despite it being present in Windows Features.
Using a previous version of PS: e.g. Developer PowerShell for VS worked.

Jenkins slave is not able to access powershell modules

There is a Jenkins node running at Windows Server 2012 R2 host.
There is a build job with "Windows PowerShell" step and command
Import-Module -Name:'WebAdministration'
Job is failing with error
Import-Module : The specified module 'WebAdministration' was not loaded because no valid module file was found in any module directory.
If I'm logging in as user which is running that node then manual execution of
Import-Module -Name:'WebAdministration' is fine.
The same job after manual module import will work for some time and then it starts failing again.
Could anyone give a tip what could be wrong?
The error indicates the running instance could not find the module file. I'd recommend try Import-Module with absolute path to the slave
e.g, you module is placed on C:\PSModule\Webadmin folder on the slave
Import-Module "C:\PSModule\Webadmin"
This should point to the desired module file and resolve the problem.
Alternatively, you can give add-pssnapin WebAdministration a shot, might land different result.

trying to import hyperv not working

I'm writing a powershell script that will start/stop hyper-v VM's but it gave this message:
The term 'Get-VM' is not recognized as the name of a cmdlet, function,
script file, or operable program
I found this helpful link:
http://social.technet.microsoft.com/Forums/windowsserver/en-US/07decd82-270e-4dd0-b8e8-789d693099dd/powershell-getvm-error?forum=winserverpowershell enter link description here
Then I added import-module HyperV but get this message:
Import-Module: The specified module 'HyperV' was not loaded because no
valid module file was found in any module directory at
c:\scripts\startStopVMShell.ps1
I found a link that was helpful here: Power shell: Import-Module enter link description here
I tried this at the powershell cmd line:
Get-Module -listavailable
But it's not listing HyperV with any spelling.
I'm using Server 2008 R2. How do I get the HyperV module on my server? Or maybe I have it on my server but powershell is looking in the current directory instead of where the module is loaded?
The Hyper-V Module for Windows PowerShell was introduced as part of the Windows Server 2012 OS. There is a CodePlex project under the "same" name that you might be able to port to 2008 R2.
PowerShell Management Library for Hyper-V
Let me know how that goes for you.
Related blog post.