Install Active Directory without needing RSAT - powershell

Installing Active Directory in Servers without RSAT
I am able to install Active Directory module in some servers but not
in others....
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-Module -AllowClobber -SkipPublisherCheck .\Microsoft.ActiveDirectory.Management.dll -ErrorAction Continue -Force
Install-Module -AllowClobber -SkipPublisherCheck .\Microsoft.ActiveDirectory.Management.resources.dll -ErrorAction Continue -Force
I keep getting this error:
PackageManagement\Install-Package : No match was found for the specified
search criteria and module name
'D:\install\ADPoSh\Microsoft.ActiveDirectory.Management.resources.dll'. Try
Get-PSRepository to see all available registered module repositories.
At C:\Program
Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1772
char:21
+ ... $null = PackageManagement\Install-Package #PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.Power....InstallPacka
ge:InstallPackage) [Install-Package], Exception
+ FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.Pac
kageManagement.Cmdlets.InstallPackage
I simply copied the DLLs from the WinSXS folder on a system that has the RSAT AD Module and the C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ActiveDirectory, all into the same folder.
What am I missing here?

Import-Module, not Install-Module
Wow, the answer was as simple as not using Install-Module, after the RSAT Feature install and merely running Import-Module instead! :)
So NOT THIS:
Get-WindowsFeature -Name RSAT-AD-PowerShell|Install-Windowsfeature
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-Module ActiveDirectory -Proxy proxy.verizon.com:80
But This:
Get-WindowsFeature -Name RSAT-AD-PowerShell|Install-Windowsfeature
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Import-Module ActiveDirectory -Proxy proxy.verizon.com:80
I mean, I would like to fix the repo, but, hey, AD Module works now so... yeah!

Related

How to fix powershell after installing VS Code

I already was asking a similar question here: https://superuser.com/questions/1613630/most-of-powershell-commands-not-working?noredirect=1
Basically, I wasn't able to fix the issue so I got a replacement computer. Brand new. Started setting it up and only did the following:
Installed VS Code
Turned on Powershell extension in VS Code
It prompted something about Package Manager needing an update or it will not work properly
Asked me to install NuGet to get the Package Manager
After that PowerShell 5.1 stopped working again.
Modules not autoloading, fresh instance of powershell does this:
PS C:\WINDOWS\system32> Write-Host
Write-Host : The term 'Write-Host' 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
+ Write-Host
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Write-Host:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Any ideas?
This fixed it for me:
(Get-Module -ListAvailable).Name | %{Import-Module -Name $_ -Force}
[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12
Install-Module -Name PackageManagement -Force -RequiredVersion 1.4.6 -Scope AllUsers -AllowClobber -Verbose
Then if the last command doesn't install NuGet do this:
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

Pester Provisioning PowerShell step fails on Import-DscResource in Windows Server 2016

I'm having an issue where PowerShell DSC resources are failing to import during a Packer job being run through Azure Pipelines.
Packer indicates an error saying:
==> amazon-ebs: Provisioning with Powershell...
==> amazon-ebs: Provisioning with powershell script: ./scripts/dsc-windows-powershell-policy.ps1
==> amazon-ebs: At C:\Windows\Temp\script-5e6ad1c0-dea6-f683-86ea-f173e577e85d.ps1:24 char:5
==> amazon-ebs: + Import-DscResource -ModuleName ComputerManagementDsc # -ModuleVer ...
==> amazon-ebs: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
==> amazon-ebs: Could not find the module 'ComputerManagementDsc'.
==> amazon-ebs: At C:\Windows\Temp\script-5e6ad1c0-dea6-f683-86ea-f173e577e85d.ps1:28 char:9
==> amazon-ebs: + PowerShellExecutionPolicy ExecutionPolicy
==> amazon-ebs: + ~~~~~~~~~~~~~~~~~~~~~~~~~
==> amazon-ebs: Undefined DSC resource 'PowerShellExecutionPolicy'. Use Import-DSCResource to import the resource.
==> amazon-ebs: + CategoryInfo : ParserError: (:) [], ParseException
==> amazon-ebs: + FullyQualifiedErrorId : ModuleNotFoundDuringParse
==> amazon-ebs:
The setup script for this one configuration starts with ensuring the modules are installed.
I removed the import statement from them due to other posts indicating it might perhaps cause some type of conflict.
$ErrorActionPreference = 'Stop'
#(
'PSDscResources'
'ComputerManagementDsc'
) | ForEach-Object {
$m = $_
if(-not (Get-InstalledModule $m))
{
Write-Host "Installing Module: $m"
Find-Module -Name $m -Repository PSGallery | Install-Module -Scope AllUsers -Force -AllowClobber
}
else
{
Write-Host "Bypassed install of $m per already installed"
}
}
Once this install is finished, I run in the same ps1 the invocation of DSC configuration. However, despite many different tests I can't get it to recognize the installed DSC Resource.
I'm suspicious that something with the built in resources in Windows 2016 is causing a conflict, but I'm not an expert in DSC yet to know what to do about it.
The configuration that is failing is:
Configuration DSC_PowerShellExecutionPolicy_config
{
Import-DscResource -ModuleName ComputerManagementDsc -ModuleVersion 8.0.0
node 'localhost'
{
PowerShellExecutionPolicy ExecutionPolicy
{
ExecutionPolicy = 'RemoteSigned'
ExecutionPolicyScope = 'LocalMachine'
}
}
}
try
{
Write-Host "⚙ Initiating DSC_PowerShellExecutionPolicy_config"
DSC_PowerShellExecutionPolicy_config
Start-DscConfiguration DSC_PowerShellExecutionPolicy_config -Verbose -force -Wait
}
catch
{
throw
exit 1
}
Note, this is an issue for me with other DSC installs I'm trying to convert to (instead of my homegrown scripts), and all of them keep having this issue.
Also, considering the Windows Server 2016 image I'm testing on doesn't have the latest nuget and PowerShellGet, I do ensure the latest version as of now:
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module PowerShellGet -MinimumVersion 2.2.3 -Force -Scope AllUsers -AllowClobber
Import-Module PowershellGet -MinimumVersion 2.2.3 -Force -verbose
Any guidance would be appreciated!
The problem you're seeing is that DSC configs are parsed before any code in the file is run, even before they are loaded into memory. If the modules aren't there before the config script is run then it'll fail. You'll be better off installing those modules before you run the config, probably in a separate Packer step.

Using VSTS as PowerShell Gallery in Deployments

I have a feed for PowerShell modules in my VSTS project and I am using it as a private gallery, based on this article:
https://roadtoalm.com/2017/05/02/using-vsts-package-management-as-a-private-powershell-gallery/
Everything works great when we run this on the machine.
I need these modules as part of the release process, so the first task is to register this repo if it doesn't exist and then download and install the latest version of each module.
The relevant part of my script is:
Install-PackageProvider -Name NuGet -Confirm:$false -RequiredVersion 2.8.5.208
Get-PSRepository | Where-Object {$_.Name -eq "MyPrivateFeed" -or $_.SourceLocation -eq "https://myproject.pkgs.visualstudio.com/_packaging/MyPrivateFeed/nuget/v2"} | Unregister-PSRepository
Register-PSRepository -Name "MyPrivateFeed" -SourceLocation "https://myproject.pkgs.visualstudio.com/_packaging/MyPrivateFeed/nuget/v2" -InstallationPolicy Trusted
$PAT = $(System.AccessToken) | ConvertTo-SecureString -AsPlainText -Force
$VSTSCredentials = New-Object -TypeName PScredential("dummy", $PAT)
Find-Module -Name * -Repository MyPrivateFeed -Credential $VSTSCredentials | Install-Module -Credential $VSTSCredentials
The PowerShell task in the release fails with:
WARNING: Unable to find module repositories.
PackageManagement\Register-PackageSource : The property 'Values' cannot be
found on this object. Verify that the property exists.
At C:\Program
Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:4173
char:17
+ ... $null = PackageManagement\Register-PackageSource #PSBoundParamete ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Microsoft.Power...erPackageSource
:RegisterPackageSource) [Register-PackageSource], Exception
+ FullyQualifiedErrorId : PropertyNotFoundStrict,Microsoft.PowerShell.Pack
ageManagement.Cmdlets.RegisterPackageSource
The same exact script works on the same machine when I run it on ISE/Console (with replacing the token with the actual PAT of course)
Anyone has any idea what is going on here?
I tried adding -PackageManagementProvider NuGet to Register-PSRepository but that didn't help
I occasionally see the "Register-PackageSource : The property 'Values' cannot be found on this object. Verify that the property exists." error when attempting to run a "Register-PSRepository" command from the PowerShellGet module (or a "Register-PackageSource" command from the dependent PackageManagement module) when there are no package sources currently registered for the "PowerShellGet" package provider (not even the default "PSGallery").
This appears to happen when an empty file exists under the current user's profile at "%USERPROFILE%\AppData\Local\Microsoft\Windows\PowerShell\PowerShellGet\PSRepositories.xml". I'm not sure what causes that empty file to be created, but until it's deleted, not even the "Register-PSRepository -Default" command will succeed.
Deleting the file does resolve the error and allows PowerShellGet package sources to be registered again.

Issues with Registering PSRepository and Installing VMWare PowerCLI Module

My Setup: Windows 10.17134, PowerShell 5.1.17134.407
When trying to Register a new PSRepository using the Code below….
Register-PSRepository -Name "PSGallery" –SourceLocation "https://www.powershellgallery.com/api/v2/" -InstallationPolicy Trusted
….it gives me the following error:
FullyQualifiedErrorId : UseDefaultParameterSetOnRegisterPSRepository,Register-PSRepository
I am not able to install the VMWare PowerCLI Module with this command:
Save-module -Name vmware.powercli -path c:\temp
….it gives me this error:
FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.SavePackage
Here are the results of a days googling
I'm running
Register-PSRepository
-Name "PSGallery"
–SourceLocation "https://www.powershellgallery.com/api/v2/"
-InstallationPolicy Trusted
(multiline for clarity)
and I kept getting
Register-PSRepository : Use 'Register-PSRepository -Default' to
register the PSGallery repository. At line:1 char:1
+ Register-PSRepository -Name "PSGallery" –SourceLocation "https://www. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (PSGallery:String) [Register-PSRepository], ArgumentException
+ FullyQualifiedErrorId : UseDefaultParameterSetOnRegisterPSRepository,Register-PSRepository
I assumed this was to do with proxy settings so I tried all of the various methods of setting the proxy in powershell to no avail.
Finally I ran
Get-PSRepository
and discovered that the repository was already registered, it was just untrusted
So I ran
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
The original reason I had for doIng this is because when I run
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
I get
Install-PackageProvider : No match was found for the specified search
criteria for the provider 'NuGet'. The package provider requires
'PackageManagement' and 'Provider' tags. Please check if the specified
package has the tags. At line:1 char:1
+ Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.PowerShel\u2026tallPackageProvider:InstallPackageProvider)
[Install-PackageProvider], Exception
+ FullyQualifiedErrorId : NoMatchFoundForProvider,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackageProvider
Unfortunately, making the repository trusted did no help with my issue
... and the original reason for doing this was so I could run
Install-Module -Name SqlServer -Force -Verbose -Scope CurrentUser
Which was failing (sorry no error message, it was so long ago)
After all my fiddling however, this statement now works without error.
Not sure if it was all the proxy config, or making NuGet trusted or both
I've been chasing this problem for days. I isolated it to Install-PackageProvider not being able to download:
Microsoft.PackageManagement.NuGetProvider-2.8.5.208.dll
In my case, my company does not allow us to download DLLs from the internet without due process. I engaged due process and got an approved copy of the DLL. Then it needs to be placed here:
$env:ProgramFiles\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll
Note the version becomes a folder and is stripped from the name.
Manually doing so eliminates the need to run Install-PackageProvider. Some articles state that Import-PackageProvider is necessary to get PowerShell to recognise it. But, I did not need to do the Import. Just try Install-Module -Name SqlServer -Force again.

Import-module: Could not find a part of the path

I am trying to connect to Exchange Online using PowerShell (As per this documentation) to add distribution group members.
However I am getting the following error:
Import-Module : Could not find a part of the path
'C:\Users\[my user account]\AppData\Local\Temp\tmp_hhw3s30w.xwu\tmp_hhw3s30w.xwu.format.ps1xml'.
At line:3 char:17
+ ... Import-Module -Name $name -Alias * -Function * -Prefix $p ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:String) [Import-Module], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.ImportModuleCommand
My code is as follows:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $adCreds -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking -AllowClobber
Connect-MsolService -Credential $adCreds
My code has worked in the past on my machine, so I am struggling to work out why it is failing now.
Any help would be massively appreciated.
Another thing to try is closing and reopening PowerShell.
For me, I had deleted everything in my local temp folder earlier in the session. It seems PowerShell was using some of what was deleted.
When I reopened PowerShell, it apparently recreated the temp files/directories it needed, and Import-Module worked again.
I know this is an old thread but I had the same issue today. The way I fixed it was to update the module that imports modules, PowerShellGet
To check your current module version, enter: Get-Module -Name PowerShellGet
If you're still on version 1.x.x.x, update it with: Install-Module -Name PowerShellGet -Force -AllowClobber
You may need to close and reopen PowerShell. Be sure to check the version again and compare it with what's available at https://www.powershellgallery.com/packages/PowerShellGet/2.2.5