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 2.8.5.201 -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\1.0.0.1\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 ‘2.8.5.201’ or newer version of NuGet
provider is installed.
At C:\ProgramData\Amazon\SSM\InstanceData\i-015ccaaf3fcfd076a\document\orchestr
ation\800b245a-c7c4-4340-943a-bdea231b4760\runPowerShellScript_script.ps1:7
char:5

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

Get-PackageProvider : Unable to find package provider ‘Nuget’.
At C:\ProgramData\Amazon\SSM\InstanceData\i-015ccaaf3fcfd076a\document\orchestr
ation\800b245a-c7c4-4340-943a-bdea231b4760\runPowerShellScript_script.ps1:11
char:7

  • 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
      gement.Cmdlets.GetPackageProvider

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",
“}\n”,
“If ( (Get-PackageProvider -Name Nuget -ListAvailable).Version -le 2.8.5.208 ) {\n”,
" Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.208 -Force\n",
“}\n”,
“if ( -not ( Get-Module -Name PSDscResources -ListAvailable ) ) {\n”,
" Install-Module PSDscResources -AllowClobber -SkipPublisherCheck -Confirm:$false -Force\n",
“}\n”,
“if ( -not ( Get-Module -Name PSDscResources ) ) {\n”,
" Import-Module -Name PSDscResources -Global -Force -PassThru \n",
“}\n\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 2.8.5.208 ) {\n",
                    "       Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.208 -Force\n",
                    "   }\n",
                    "} catch {\n",
                    "       Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.208 -Force\n",
                    "}\n",
                    "if ( (Get-PSRepository -Name 'PSGallery').InstallationPolicy -eq 'Untrusted' ) {\n",
                    "    Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted \n",
                    "}\n",
                    "if ( -not ( Get-Module -Name PSDscResources -ListAvailable ) ) {\n",
                    "    Install-Module PSDscResources -AllowClobber -SkipPublisherCheck -Confirm:$false -Force\n",
                    "}\n",
                    "if ( -not ( Get-Module -Name PSDscResources ) ) {\n",
                    "    Import-Module -Name PSDscResources -Global -Force -PassThru \n",
                    "}\n\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.

No.

His looks like correctly formated code including syntax highlighting and indentation

image

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)

2 Likes