header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Combining two arrays of string from get-content text files strips carriage returns
Last Post 16 Aug 2010 10:35 PM by beuy. 1 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
beuyUser is Offline
New Member
New Member
Posts:12
Avatar

--
16 Aug 2010 08:20 PM
    Hi there I'm working on a script that can automate some jobs, nothing fancy just feed a list of commands in and report back depending on the options passed via named parameters. I'm currently working on improving it by adding the ability to process a number of jobs at once (job definitions are in a few text files in a separate folder called "jobs") and having an odd problem. The code snippet is as follows: # if $commands is a folder if(test-path $c -pathtype container) { # get each textfile in the root level directory of the folder passed $files = @(Get-ChildItem "$c\*.*" -include *.txt -errorAction "SilentlyContinue") foreach($file in $files) { $tempcommands = @(Get-Content $file) foreach($tc in $tempcommands) { $commands += $tc } } } else { # Read in list of commands from passed named parameter $commands = @(Get-Content $c) } # Run commands foreach ($command in $commands) { Write-Output "+--------------------------------+" Write-Output "Running Command: $command" Write-Output "+--------------------------------+" try { $scriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock($command) $out = Invoke-Command -computer $computer -scriptblock $scriptblock }... The problem is that every line in all the txt files get put into a single $command variable so the invoke-command fails. I've tested this within a shell by doing the following $a = get-content @(jobs\a.txt) $b = get-content @(jobs\b.txt) $c = $a + $b (note i've also tried $a += $b here!) foreach($i in $c) { "new line $i" } Which outputs the expected one command per line. Any help tips or ideas is greatly appreciated, please let me know if anything requires more explanation. I've tried to only include the relevant scope.
    beuyUser is Offline
    New Member
    New Member
    Posts:12
    Avatar

    --
    16 Aug 2010 10:35 PM
    Talk about over complicating an issue just needed to declare $commands as an array via

    $commands = @()
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer