deploy powershell script

Rising Flight 5,216 Reputation points
2024-03-28T00:29:02.8933333+00:00

Hi All

i want to deploy the below PowerShell script on a collection. When i execute below syntax manually on a VM, it will give me the prompt to install NuGet. I want to add YES in the PowerShell script so that it will not prompt and script will not fail. How can i add it please guide me.

(PowershellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositores.Do you want PowerShellGet to install and import the NuGet provider now?)(Yes/No/Suspend)

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Install PSWindowsUpdate module if not already installed
if (-not (Get-Module -ListAvailable -Name PSWindowsUpdate)) {
    Write-Host "Installing PSWindowsUpdate module..."
    Install-Module PSWindowsUpdate -Force
}
else {
    Write-Host "PSWindowsUpdate module is already installed."
}
# Import PSWindowsUpdate module
Import-Module PSWindowsUpdate
Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Intune | Configuration Manager | Deployment
Microsoft Security | Intune | Configuration Manager | Application
Microsoft Security | Intune | Configuration Manager | Other
0 comments No comments
{count} votes

Accepted answer
  1. AllenLiu-MSFT 49,311 Reputation points Microsoft External Staff
    2024-03-28T02:10:26.1333333+00:00

    Hi, @Rising Flight

    Thank you for posting in Microsoft Q&A forum.

    To install the NuGet provider without being prompted, you can use the -Force parameter with the Install-PackageProvider cmdlet. Here's an updated version of your script that includes this change:

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    # Install NuGet provider if not already installed
    if (-not (Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue)) {
        Write-Host "Installing NuGet provider..."
        Install-PackageProvider -Name NuGet -Force
    }
    else {
        Write-Host "NuGet provider is already installed."
    }
    # Install PSWindowsUpdate module if not already installed
    if (-not (Get-Module -ListAvailable -Name PSWindowsUpdate)) {
        Write-Host "Installing PSWindowsUpdate module..."
        Install-Module PSWindowsUpdate -Force
    }
    else {
        Write-Host "PSWindowsUpdate module is already installed."
    }
    # Import PSWindowsUpdate module
    Import-Module PSWindowsUpdate
    

    This script checks if the NuGet provider is already installed and installs it if it's not. The -ErrorAction SilentlyContinue parameter prevents any error messages from being displayed if the provider is not found. The -Force parameter with Install-PackageProvider installs the provider without prompting.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Add comment".

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Gary Blok 1,756 Reputation points
    2024-03-28T03:40:35.3766667+00:00

    What are you trying to accomplish with the PSWindowsUpdate on a collection of endpoints?
    If you're trying to deploy Windows Updates, I'd recommend looking into

    1. Using Software Updates in ConfigMgr to maintain device compliance
    2. Using Co-management to use Windows Update for Business to manage your updates
    3. Using Native PowerShell commands, removing the reliance on the PSWIndowsUpdate Module
      1. https://github.com/OSDeploy/OSD/blob/master/Public/OSDCloudTS/Start-WindowsUpdate.ps1
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.