Adding current object to array

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #5206

    by rob.irvin at 2012-10-17 18:30:27

    I am using a foreach loop with an "if" statement that reads each line in an array. If the command in the "if" statement succeeds I want to add the current working string to another array. If the command fails I want to delete the current working string from the second array.

    To add the string I tried this:
    $local = $local + {$_}
    all this does is adds the {$_} symbol to the array. Once I get that working I think I can figure out how to remove the item.

    I hope this makes sense. I can post some of the code if that helps.

    Thanks

    by DonJ at 2012-10-17 18:42:20

    Well it depends a bit. $_ only works in specific contexts, and {} makes it a script block. Not sure what the goal is but normally I don't use {}, and you gotta make sure $_ has meaning in your context. Wanna post a snippet?

    by rob.irvin at 2012-10-17 19:08:36

    Thanks for the quick reply. Here is some of the script which is by no means done so I'm probably missing braces, etc. The two blocks in bold print is the place I am stuck. The first one doesn't work, and I think the second one might but I can't test until I get to work on Friday. Thanks again for your help.

    ############################################################
    #Compare the two .csv files and assign differences to $diff #
    ############################################################

    if (Test-Path -Path "C:\Users\All Users\Gad\pa_cal.csv")
    {$Master = Get-content "c:\wcsv\remote.csv" | where{$_} | ConvertFrom-Csv
    $Local = Get-content "C:\wcsv\local.csv" |where{$_} |ConvertFrom-Csv
    $diff = Compare-Object $local $master -Property Name,slot,version
    foreach ($app in $diff | Where-Object {$_.SideIndicator -eq "< ="})
    ############################################################
    # Assign MSI filename to variable and attempt installation #
    ############################################################
    {$msi = Get-ChildItem \\$Server\$Dir\$App.slot\*.* -Include *.msi
    if (msiexec /i \\$Server\$Dir\$Appslot\$msi)
    {$local = $local + {$_}}
    {$local = $local + $app}
    }

    by nohandle at 2012-10-18 07:23:18

    basically like this, but are you really sure you do want to do it this way? what if the msiexec (removed in my example) will result in system reboot??
    if (Test-Path -Path "c:\temp")
    {
    $Master = Get-content "c:\temp\csv\Book1.csv" | where{$_} | ConvertFrom-csv -Delimiter ";"
    $Local = Get-content "C:\temp\csv\book2.csv" |where{$_} |ConvertFrom-Csv -Delimiter ";"
    $diff = Compare-Object -ReferenceObject $local -DifferenceObject $master -Property Name,slot,version

    foreach ($app in ($diff | Where-Object {$_.SideIndicator -eq "< ="}))
    {
    if ($true) #always true
    {$master += $app}
    else
    {< #afaik no easy way to remove the items#>}
    }
    }

    by rob.irvin at 2012-10-18 07:38:08

    Thank you. I will give it a try and see how it goes.

    As far as the reboot goes, think I may need to add "NORESTART" to avoid that. "MSIEXEC /i /quiet app.msi /norestart". I don't think any of the apps to install will require a reboot but I need to check. I'm glad you brought that to my attention. Do you know of a better way to do it?
    I'm concerned that I may need to use the return code from the msiexec command to make it work correctly. I will need to do some research on that.

    Thanks a lot for your reply. This forum has really helped me a lot. Hopefully I will be able to contribute once I am better at this.

    by nohandle at 2012-10-18 08:14:27

    the no restart is not always an option.

    msiexec saves the installed packages in registry. evaluate and compare them.

    not sure what exactly is the problem you are trying to solve, though.

You must be logged in to reply to this topic.