An empty pipe element is not allowed

Glenn Maxwell 10,351 Reputation points
2024-04-01T06:51:47.3433333+00:00

Hi All, can anyone help me correct the syntax. i am unable to export the output to a text file

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
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."
}
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-Module PSWindowsUpdate
Write-Host "Checking for available Windows Updates..."
Get-WindowsUpdate
Write-Host "Installing available Windows Updates..."
Install-WindowsUpdate -AcceptAll -AutoReboot:$false | Out-File -FilePath "C:\output.txt"
At line:29 char:1
+ | Out-File -FilePath "C:\output.txt"
+ ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement


Windows Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,520 questions
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,408 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,418 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,185 questions
{count} votes

Accepted answer
  1. MotoX80 32,331 Reputation points
    2024-04-02T16:48:14.7866667+00:00

    I didn't have any problem with the brackets on Win10/PS5.1, but -AutoReboot:$false did not work. I get a prompt.

    User's image

    -IgnoreReboot appears to work. I need to verify on another VM.

    User's image

    After a reboot, when I rerun the script I get this.

    User's image

    Here is the script that I used. Updated on 4/3..

    $LogFolder = "C:\Temp"                                # Where to put log files 
    $ts = $LogFolder + "\PatchInstallTranscript.log"
    $op = $LogFolder + "\PatchInstallOutput.log"
    
    Start-Transcript -Path $ts
    
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    if (-not (Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue)) {
        Write-Host "NOT Installing NuGet provider..."
        Install-PackageProvider -Name NuGet -Force -confirm:$false
    }
    else {
        Write-Host "NuGet provider is already installed."
    }
    if (-not (Get-Module -ListAvailable -Name PSWindowsUpdate)) {
        Write-Host "Installing PSWindowsUpdate module..."
        Install-Module PSWindowsUpdate -Force -confirm:$false
    }
    else {
        Write-Host "PSWindowsUpdate module is already installed."
    }
    Import-Module PSWindowsUpdate
    Write-Host "Checking for available Windows Updates..."
    Get-WindowsUpdate -verbose -IgnoreReboot
    Write-Host "Installing available Windows Updates..."
    $Update = Install-WindowsUpdate -AcceptAll -IgnoreReboot -verbose
    if (!$Update) {
       $Update = "Install-WindowsUpdate did not return any data."
    }
                                                
    $Update | format-table -wrap | Out-File -FilePath $op 
    get-content $op                     # record in transcript   
    Stop-Transcript
    
    

    I would recommend that you do a reboot in case you have updates installed and waiting to to be implemented and/or other powershell.exe processes waiting for someone to type in y/n.


8 additional answers

Sort by: Most helpful
  1. Glenn Maxwell 10,351 Reputation points
    2024-04-01T21:18:29.4266667+00:00
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    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."
    }
    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-Module PSWindowsUpdate
    Write-Host "Checking for available Windows Updates..."
    Get-WindowsUpdate
    Write-Host "Installing available Windows Updates..."
    $x = Install-WindowsUpdate -AcceptAll -AutoReboot:$false -verbose
    $x.gettype()
    $x | Out-File -FilePath "C:\windows\temp\output.txt"
    
    
    

    You cannot call a method on a null-valued expression.

    At line:21 char:1

    • $x.gettype()
    • 
          + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      
          + FullyQualifiedErrorId : InvokeMethodOnNull
      
      

  2. Glenn Maxwell 10,351 Reputation points
    2024-04-02T05:41:55.6566667+00:00

    The whole script is the above. not sure what mistake i am doing


  3. Rich Matheisen 45,186 Reputation points
    2024-04-02T15:33:51.2566667+00:00

    I ran that script interactively and got this weird error about "else" not be recognized! That shook loose a remembrance of a PowerShell parsing problem from long ago.

    Try this version of the script:

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    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."
    }
    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-Module PSWindowsUpdate
    Write-Host "Checking for available Windows Updates..."
    Get-WindowsUpdate
    Write-Host "Installing available Windows Updates..."
    Install-WindowsUpdate -AcceptAll -AutoReboot:$false | Out-File -FilePath "C:\output.txt"
    

    The only thing different is the removal of the space between the else and the "{" in two places. Just to safe I also removed the spaces between the last ")" and the following "{" in the two "if" statements (I don't think that's a problem but I didn't want to test twice). I do remember that there was also a problem (at least I think I remember that) with the trailing space after the "{" in an "if" statement).

    0 comments No comments

  4. Glenn Maxwell 10,351 Reputation points
    2024-04-02T18:33:34.6466667+00:00

    at stop-transcript i am getting error.

    Stop-Transcript : An error occurred stopping transcription: The host is not currently transcribing.

    At line:28 char:1

    • Stop-Transcript
    • 
          + CategoryInfo          : InvalidOperation: (:) [Stop-Transcript], PSInvalidOperationException
      
          + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.StopTranscriptCommand