none
Powershell: read multiple values from txt-file RRS feed

  • Question

  • Hi,

    I have this script which removes App-V packages from some servers (to do a cleanup).
    It works fine when using 1 package but I can't make it work using multiple packages.

    This is what I have:

    $servers = @("serverA","serverB")
    $packageData = Get-Content C:\packages.txt
    foreach ($server in $servers)
    {
        foreach ($data in $packageData) {
            $packagename = $data[0]
            $packageversion = $data[1]
            Invoke-Command -ScriptBlock { param($packagename,$packageversion) Remove-AppvClientPackage -Name $packagename -Version $packageversion } -ComputerName $server -argumentlist $packagename,$packageversion

        }
    }

    packages.txt contains

    "A Package" "0.0.0.1"
    "B Package" "0.0.0.1"

    When I check content and check for $packagename I get
    A

    So my question: howto read the data correctly from the txt-file?

    Please advise.
    J.

    Jan Hoedt

    Wednesday, January 20, 2016 1:11 PM

Answers

  • Ok, so this is what you mean:

    foreach ($server in $servers)
        {
            foreach ($package in $pacakges)
            {   
                Invoke-Command -ScriptBlock { param($package) Stop-AppvClientPackage -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package
                Invoke-Command -ScriptBlock { param($package) Remove-AppvClientPackage -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package
            }
        }


    Jan Hoedt

    Yep. I was just about to post an edit to my previous post:

    $serverList = 'Server1','Server2'
    $packageHash = @(
        @{Name = 'A Package' ; Version = '0.0.0.2'}
        @{Name = 'B Package' ; Version = '0.0.0.3'}
    )
    $package = $packageHash | % { New-Object PsObject -Property $_ }
    
    foreach ($server in $serverList) {
    
        foreach ($pkg in $package) {
    
            Write-Host "Server is $server, Package Name is $($pkg.Name), Package Version is $($pkg.Version)" -ForegroundColor Green
    
        }
    
    }


    • Marked as answer by janhoedt Wednesday, January 20, 2016 3:13 PM
    Wednesday, January 20, 2016 2:58 PM

All replies

  • Tried it in a different way but that won't work either:

    $servers = @("serverA","serverB")
    $package = @(
                   @{Name = 'A Package' ; Version = '0.0.0.2'}
                   @{Name = 'B Package' ; Version = '0.0.0.3'}
                )

    foreach ($server in $servers) {


        Invoke-Command -ScriptBlock { param($package) Stop-AppvClientPackage -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package.name


    }


    Jan Hoedt

    Wednesday, January 20, 2016 1:35 PM
  • If you convert your input file to a CSV, ie:

    Name, Value
    A Package, 0.0.0.1
    B Package, 0.0.0.1

    Then use Import-CSV then you get a nice structured way to access data, instead of what you're doing which is hoping that PowerShell can guess what format you want the data in.

    https://technet.microsoft.com/en-us/library/ee176874.aspx?f=255

    Wednesday, January 20, 2016 1:38 PM
  • Tried it in a different way but that won't work either:

    $servers = @("serverA","serverB")
    $package = @(
                   @{Name = 'A Package' ; Version = '0.0.0.2'}
                   @{Name = 'B Package' ; Version = '0.0.0.3'}
                )

    foreach ($server in $servers) {


        Invoke-Command -ScriptBlock { param($package) Stop-AppvClientPackage -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package.name


    }


    Jan Hoedt

    You're halfway there with this. You've got an array of hashtables, but no object.

    $packageHash = @(
        @{Name = 'A Package' ; Version = '0.0.0.2'}
        @{Name = 'B Package' ; Version = '0.0.0.3'}
    )
    
    $package = $packageHash | % { New-Object PsObject -Property $_ }
    
    $package


    Wednesday, January 20, 2016 1:55 PM
  • Thanks! So this is what I got now. Not fully working, debugging now.

    servers = @("serverA","serverB")
    $packagehash = @(
                   @{Name = 'A Package' ; Version = '0.0.0.2'}
                   @{Name = 'B Package' ; Version = '0.0.0.3'}
                )
    $package = $packageHash | % { New-Object PsObject -Property $_ }
    
    foreach ($server in $servers) 
        {
    
       
        Invoke-Command -ScriptBlock { param($package) Stop-AppvClientPackage -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package
        Invoke-Command -ScriptBlock { param($package) Remove-AppvClientPackage -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package
    
        }
    


    Jan Hoedt

    Wednesday, January 20, 2016 2:35 PM
  • Correct link is https://technet.microsoft.com/en-us/library/hh849891.aspx

    Jan Hoedt

    Wednesday, January 20, 2016 2:40 PM
  • Since you have multiple entries in the object, you should use an inner loop as well.

    Wednesday, January 20, 2016 2:47 PM
  • Ok, so this is what you mean:

    foreach ($server in $servers)
        {
            foreach ($package in $pacakges)
            {   
                Invoke-Command -ScriptBlock { param($package) Stop-AppvClientPackage -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package
                Invoke-Command -ScriptBlock { param($package) Remove-AppvClientPackage -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package
            }
        }
    


    Jan Hoedt

    Wednesday, January 20, 2016 2:58 PM
  • Ok, so this is what you mean:

    foreach ($server in $servers)
        {
            foreach ($package in $pacakges)
            {   
                Invoke-Command -ScriptBlock { param($package) Stop-AppvClientPackage -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package
                Invoke-Command -ScriptBlock { param($package) Remove-AppvClientPackage -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package
            }
        }


    Jan Hoedt

    Yep. I was just about to post an edit to my previous post:

    $serverList = 'Server1','Server2'
    $packageHash = @(
        @{Name = 'A Package' ; Version = '0.0.0.2'}
        @{Name = 'B Package' ; Version = '0.0.0.3'}
    )
    $package = $packageHash | % { New-Object PsObject -Property $_ }
    
    foreach ($server in $serverList) {
    
        foreach ($pkg in $package) {
    
            Write-Host "Server is $server, Package Name is $($pkg.Name), Package Version is $($pkg.Version)" -ForegroundColor Green
    
        }
    
    }


    • Marked as answer by janhoedt Wednesday, January 20, 2016 3:13 PM
    Wednesday, January 20, 2016 2:58 PM
  • Great! Thanks! Only thing I want to build in is that it only starts if it discovers the package.
    Will update this ticket once done.

    Jan Hoedt

    Wednesday, January 20, 2016 3:13 PM
  • Almost there, debugging since it still shows errors that it can't find the package.
    foreach ($server in $servers) 
        {
            foreach ($package in $packages)
            {   
                $PackageAvailable = Invoke-Command -ScriptBlock { param($package) Get-AppvClientPackage -All -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package
               
                if ($packageAvailable -eq $null)
                    {
                        Write-Host "$($package.Name), $($package.Version) is not installed on server $server" -ForegroundColor Green 
    
                    }
                Else { 
                
                       
                        Write-Host "Server is $server, Package Name is $($package.Name), Package Version is $($package.Version)" -ForegroundColor Green
                        Invoke-Command -ScriptBlock { param($package) Stop-AppvClientPackage -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package
                        Invoke-Command -ScriptBlock { param($package) Unpublish-AppvClientPackage -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package
                        Invoke-Command -ScriptBlock { param($package) Remove-AppvClientPackage -Name $package.name -Version $package.version } -ComputerName $server -argumentlist $package
                                    
                     }
                
            }
        }
    


    Jan Hoedt

    Wednesday, January 20, 2016 4:09 PM