Install nuget and DSC modules via script

I am trying to install DSC Module on a basic Windows 2019 Server. It just fails silently. This is a brand new servers that are failing

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if(!(get-packageprovider -name nuget)){
    Install-PackageProvider -Name NuGet -RequiredVersion -Force -Scope CurrentUser

Set-PSRepository -Name 'PsGallery' -InstallationPolicy Trusted
Install-Module -Name NetworkingDsc -RequiredVersion 8.1.0 -AllowClobber -Repository PSGallery -Verbose

What is the error you are getting ?

No error. It just fails silently

use -ErrorAction Stop for all the cmdlets. It should fail, else its wont be easy to find the reason.

I am getting these errors:

Exception calling "ShouldContinue" with "2" argument(s): "Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available." At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\\PSModule.psm1:7392 char:8 + if($Force -or $psCmdlet.ShouldContinue($shouldContinueQueryMessag ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : PSInvalidOperationException

Set-PSRepository : NuGet provider is required to interact with NuGet-based
repositories. Please ensure that ‘’ or newer version of NuGet
provider is installed.
At C:\ProgramData\Amazon\SSM\InstanceData\i-015ccaaf3fcfd076a\document\orchestr

  • Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidOperation: (:slight_smile: [Set-PSRepository], Invali
    • FullyQualifiedErrorId : CouldNotInstallNuGetProvider,Set-PSRepository

Get-PackageProvider : Unable to find package provider ‘Nuget’.
At C:\ProgramData\Amazon\SSM\InstanceData\i-015ccaaf3fcfd076a\document\orchestr

  • If ( (Get-PackageProvider -Name Nuget -ListAvailable).Version -le 2.8 …
  •   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : ObjectNotFound: (Microsoft.Power…PackageProvid
      er:GetPackageProvider) [Get-PackageProvider], Exception
    • FullyQualifiedErrorId : UnknownProvider,Microsoft.PowerShell.PackageMana

I am trying to run this script:

“Import-Module -Name PowerShellGet -Force\n”,
“Import-Module -Name PackageManagement -Force\n”,
“if ( (Get-PSRepository -Name ‘PSGallery’).InstallationPolicy -eq ‘Untrusted’ ) {\n”,
" Set-PSRepository -Name ‘PSGallery’ -InstallationPolicy Trusted \n",
“If ( (Get-PackageProvider -Name Nuget -ListAvailable).Version -le ) {\n”,
" Install-PackageProvider -Name NuGet -MinimumVersion -Force\n",
“if ( -not ( Get-Module -Name PSDscResources -ListAvailable ) ) {\n”,
" Install-Module PSDscResources -AllowClobber -SkipPublisherCheck -Confirm:$false -Force\n",
“if ( -not ( Get-Module -Name PSDscResources ) ) {\n”,
" Import-Module -Name PSDscResources -Global -Force -PassThru \n",

I think I got it:

                    "Import-Module -Name PowerShellGet -Force\n",
                    "Import-Module -Name PackageManagement -Force\n",
                    "try {\n",
                    "   If ( (Get-PackageProvider -Name Nuget -ListAvailable -ErrorAction Stop ).Version -le ) {\n",
                    "       Install-PackageProvider -Name NuGet -MinimumVersion -Force\n",
                    "   }\n",
                    "} catch {\n",
                    "       Install-PackageProvider -Name NuGet -MinimumVersion -Force\n",
                    "if ( (Get-PSRepository -Name 'PSGallery').InstallationPolicy -eq 'Untrusted' ) {\n",
                    "    Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted \n",
                    "if ( -not ( Get-Module -Name PSDscResources -ListAvailable ) ) {\n",
                    "    Install-Module PSDscResources -AllowClobber -SkipPublisherCheck -Confirm:$false -Force\n",
                    "if ( -not ( Get-Module -Name PSDscResources ) ) {\n",
                    "    Import-Module -Name PSDscResources -Global -Force -PassThru \n",

Could you please format your codse as code here in the forum? You can go back and edit your already existing posts to correct the formatting.

Thanks in advacne.

It is in " <\code>" blocks.

I don’t know how it looks for you but for me it does not look like code at all. :wink:

Do you see any difference between one of your code blocks and the code block @monterey610 posted in his initial question? :wink: :wink:

Yes, I see the difference, his is PowerShell and mine is JSON.


His looks like correctly formated code including syntax highlighting and indentation


And yours just does not.

1 Like

@dan-iverson I’ve formatted the json content. The best way to format is , once the code is pasted/typed while posting, select all the code and click on the prefrmatted text button (the sixth option in the tool bar on top while posting)