powershell output to .txt file wont print the for loop - excel

I have some audio, video & jpg files.
I am using ffmpeg to get all the duration
all thing works perfect, I have got all durations from a for loop and set it to a variable, when I echo it, it works perfectly, but when I output it to a .txt file it prints only the last loop
any suggestion
below is my code
$file = "D:\edl\input\aa.xlsx"
$sheetName = "Sheet1"
#Create an instance of Excel.Application and Open Excel file
$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open($file)
$sheet = $workbook.Worksheets.Item($sheetName)
$objExcel.Visible=$false
#Count max row
$rowMax = ($sheet.UsedRange.Rows).count
#Declare the starting positions
$rowName,$colName = 1,1
$rowAge,$colAge = 1,2
$rowCity,$colCity = 1,3
$rowMessage,$colMessage = 1,9
#loop to get values and store it
for ($i=0; $i -le $rowMax-1; $i++)
{
$id = $sheet.Cells.Item($rowName+$i,$colName).text
$name = $sheet.Cells.Item($rowAge+$i,$colAge).text
$subtitle = $sheet.Cells.Item($rowCity+$i,$colCity).text
$filename2 = "$id.jpg"
$filename = "$id.mp4"
$filename1 = "$id.m4a"
$duration2 = if ((ffmpeg -i $filename2 2>&1 | Out-String) -match 'Duration:\s+([\d:.]+)') { $matches[1] };
$duration1 = if ((ffmpeg -i $filename1 2>&1 | Out-String) -match 'Duration:\s+([\d:.]+)') { $matches[1] };
$duration = if ((ffmpeg -i $filename 2>&1 | Out-String) -match 'Duration:\s+([\d:.]+)') { $matches[1] };
if($duration2){
$duration2 = "02:00:11:15"
$bb = $duration2
$out = "$i" + " " + "AX" + " " + "V" + " " + "C" + " " + "00:00:00:00" + " " + $bb + " "+ "* FROM CLIP NAME: " + $id+".jpg"
}
if($duration1){
$out1 = "$i"+ " " + "AX" + " " + "AA" + " " + "C" + " " + "00:00:00:00" + " " + $duration1 + " " + "* FROM CLIP NAME: " + $id+".m4a"
}
if(!$duration){
}else{
$out2 = "$i" + " " + "AX" + " " + "AA/V" + " " + "C" + " " + "00:00:00:00" + " " + $duration + " "+ "* FROM CLIP NAME: " + $id+".mp4"
}
$gg = $out+"`n"+"`n"+$out1+"`n"+"`n"+$out2+"`n"
echo $gg
$gg | Out-File -FilePath d:\edl\input\output.txt
}
#close excel file
$objExcel.quit()
when i echo variable $gg
this is the output
0 AX V C 00:00:00:00 02:00:11:15 * FROM CLIP NAME: 1aef53e6-92ac-4d28-89f8-4cce28fa0f58.jpg
0 AX AA C 00:00:00:00 00:00:03.48 * FROM CLIP NAME: 1aef53e6-92ac-4d28-89f8-4cce28fa0f58.m4a
0 AX AA/V C 00:00:00:00 00:00:07.64 * FROM CLIP NAME: 1aef53e6-92ac-4d28-89f8-4cce28fa0f58.mp4
1 AX V C 00:00:00:00 02:00:11:15 * FROM CLIP NAME: 9a52a954-ffe9-4420-a701-68988923f81f.jpg
1 AX AA C 00:00:00:00 00:00:03.46 * FROM CLIP NAME: 9a52a954-ffe9-4420-a701-68988923f81f.m4a
0 AX AA/V C 00:00:00:00 00:00:07.64 * FROM CLIP NAME: 1aef53e6-92ac-4d28-89f8-4cce28fa0f58.mp4
2 AX V C 00:00:00:00 02:00:11:15 * FROM CLIP NAME: 20b92847-72eb-455d-98ce-6661ed52951e.jpg
2 AX AA C 00:00:00:00 00:00:03.52 * FROM CLIP NAME: 20b92847-72eb-455d-98ce-6661ed52951e.m4a
2 AX AA/V C 00:00:00:00 00:00:09.75 * FROM CLIP NAME: 20b92847-72eb-455d-98ce-6661ed52951e.mp4
3 AX V C 00:00:00:00 02:00:11:15 * FROM CLIP NAME: 73aa1f8e-1067-4efd-b2bd-6748a088f33a.jpg
3 AX AA C 00:00:00:00 00:00:03.21 * FROM CLIP NAME: 73aa1f8e-1067-4efd-b2bd-6748a088f33a.m4a
2 AX AA/V C 00:00:00:00 00:00:09.75 * FROM CLIP NAME: 20b92847-72eb-455d-98ce-6661ed52951e.mp4
4 AX V C 00:00:00:00 02:00:11:15 * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.jpg
4 AX AA C 00:00:00:00 00:00:03.05 * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.m4a
4 AX AA/V C 00:00:00:00 00:00:06.93 * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.mp4
but my txt file output
4 AX V C 00:00:00:00 02:00:11:15 * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.jpg
4 AX AA C 00:00:00:00 00:00:03.05 * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.m4a
4 AX AA/V C 00:00:00:00 00:00:06.93 * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.mp4

By default, if a file exists in the specified path, Out-File overwrites the file without warning. Use -Append parameter (Adds the output to the end of an existing file):
$gg | Out-File -FilePath d:\edl\input\output.txt -Append
BTW, as an alternative to
echo $gg
$gg | Out-File -FilePath d:\edl\input\output.txt -Append
use Tee-Object cmdlet (It stores the output in a file or variable and also sends it down the pipeline. If Tee-Object is the last command in the pipeline, the command output is displayed at the prompt):
$gg | Tee-Object -FilePath d:\edl\input\output.txt -Append

Related

POWERSHELL how to combine several csv into one excel

I am trying to download all CSV files from the \tmp\ directory with a PS script and convert them to one excel file as a report and place it in the \reports\ directory under the name LDAP.xlsx . My CSV files have varying amounts of saved data.
In the forum I found this how-to-export-a-csv-to-excel-using-powershell and my code looks like this:
Clear-Host
# SOURCE
##########
# config file
$conf_file = "C:\PS_LDAP_searchlight\config\searchlight_conf.conf"
$conf_values = Get-Content $conf_file | Out-String | ConvertFrom-StringData
# variables from config file
$main_path = $conf_values.main_path
$tmp_path = $conf_values.tmp_path
$reports_path = $conf_values.reports_path
# PROGRAM
##########
$workingdir = $main_path + $tmp_path + "*.csv"
$reportsdir = $main_path + $reports_path
$csv = dir -path $workingdir
foreach($inputCSV in $csv){
$outputXLSX = $reportsdir + "\" + $inputCSV.Basename + ".xlsx"
### Create a new Excel Workbook with one empty sheet
$excel = New-Object -ComObject excel.application
$excel.DisplayAlerts = $False
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
### Build the QueryTables.Add command
### QueryTables does the same as when clicking "Data » From Text" in Excel
$TxtConnector = ("TEXT;" + $inputCSV)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
### Set the delimiter (, or ;) according to your regional settings
### $Excel.Application.International(3) = ,
### $Excel.Application.International(5) = ;
$query.TextFileOtherDelimiter = $Excel.Application.International(5)
### Set the format to delimited and text for every column
### A trick to create an array of 2s is used with the preceding comma
$query.TextFileParseType = 1
$query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count
$query.AdjustColumnWidth = 1
### Execute & delete the import query
$query.Refresh()
$query.Delete()
### Save & close the Workbook as XLSX. Change the output extension for Excel 2003
$Workbook.SaveAs($outputXLSX,51)
$excel.Quit()
# Cleaner
$inputCSV = $null
$outputXLSX = $null
}
## To exclude an item, use the '-exclude' parameter (wildcards if needed)
#remove-item -path $workingdir -exclude *Crab4dq.csv
# CLEANER
###############################
# SOURCE
###############################
# config file
$conf_file = $null
$conf_values = $null
# variables from config file
$main_path = $null
$tmp_path = $null
$reports_path = $null
# PROGRAM
###############################
$workingdir = $null
$csv = $null
$reportsdir = $null
the code reads all files but writes one to one. I need help and explanation on how to make a many-to-one option. I would like each CSV file to be saved as a separate sheet under its own name like:
users_all_inf.csv in excel\sheet1 => users_all_inf
active_users_last_logon_year_ago.csv in excel\sheet2 => active_users_last_logon_year_ago
nextfilename.csv in excel\next_sheet => nextfilename
so that all data will be available in one excel report.xlsx file.
I will be grateful for any hint or help in converting the code.
Finally my code looks like:
Clear-Host
# config file
$conf_file = "C:\SEARCHLIGHT\config\searchlight_conf.conf"
$conf_values = Get-Content $conf_file | Out-String | ConvertFrom-StringData
# variables from config file
$main_path = $conf_values.main_path
$tmp_path = $conf_values.tmp_path
$reports_path = $conf_values.reports_path
$system_name = $conf_values.system_name
$tmp_dir = $main_path + $tmp_path + "*" # source file
$outputfilename = $(get-date -f yyyyMMdd) + "_" + $system_name + "_report.xlsx" # destination file with date
# get list of csvs files
$csvs = Get-ChildItem $tmp_dir -Include *.csv
$y = $csvs.Count
Write-Host "Detected the following CSV files: ($y)"
foreach ($csv in $csvs) {
Write-Host " "$csv.Name
}
Write-Host Creating: $outputfilename
# Create a new Excel workbook
$excelapp = new-object -comobject Excel.Application
$excelapp.sheetsInNewWorkbook = $csvs.Count
$xlsx = $excelapp.Workbooks.Add()
$sheet=1
$delimiter = ";" # delimiter used in the csv file
foreach ($csv in $csvs) {
#$row=1
#$column=1
$worksheet = $xlsx.Worksheets.Item($sheet)
$worksheet.Name = $csv.Name
# Build the QueryTables.Add command and reformat the data
$TxtConnector = ("TEXT;" + $csv)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
$query.TextFileOtherDelimiter = $delimiter
$query.TextFileParseType = 1
$query.TextFileColumnDataTypes = ,1 * $worksheet.Cells.Columns.Count
$query.AdjustColumnWidth = 1
# Execute & delete the import query
$query.Refresh()
$query.Delete()
$sheet++
} # end foreach ($csv in $csvs)
# Save & close the Workbook as XLSX
$output = $main_path + $reports_path + $outputfilename
$xlsx.SaveAs($output)
$excelapp.quit()
I found a solution that partially solves my problem:
# variables from config file
$main_path = $conf_values.main_path
$tmp_path = $conf_values.tmp_path
$reports_path = $conf_values.reports_path
$system_name = $conf_values.system_name
$tmp_dir = $main_path + $tmp_path + "*"
$csvs = Get-ChildItem $tmp_dir -Include *.csv
$y = $csvs.Count
Write-Host "Detected the following CSV files: ($y)"
foreach ($csv in $csvs)
{
Write-Host " "$csv.Name
}
$outputfilename = $(get-date -f yyyyMMdd) + "_" + $system_name + "_report.xlsx" #creates file name with date/username
Write-Host Creating: $outputfilename
$excelapp = new-object -comobject Excel.Application
$excelapp.sheetsInNewWorkbook = $csvs.Count
$xlsx = $excelapp.Workbooks.Add()
$sheet=1
foreach ($csv in $csvs) {
$row=1
$column=1
$worksheet = $xlsx.Worksheets.Item($sheet)
$worksheet.Name = $csv.Name
$file = (Get-Content $csv)
foreach($line in $file) {
$linecontents=$line -split ',(?!\s*\w+")'
foreach($cell in $linecontents) {
$worksheet.Cells.Item($row,$column) = $cell
$column++
}
$column=1
$row++
}
$sheet++
}
$output = $main_path + $reports_path + $outputfilename
$xlsx.SaveAs($output)
$excelapp.quit()
this solves the problem many to one but in the worksheet all the data is stored in one column. At this moment I have got:
csv files:
data1;data2;data3;...
data1;data2;data3;...
report like:
| A | B |
|-----------------------|---|
| data1;data2;data3;... | |
|-----------------------|---|
| data1;data2;data3;... | |
i need help to make the data split into columns like:
| A | B | C | ... |
|-------|-------|-------|-----|
| data1 | data2 | data3 | ... |
|-------|-------|-------|-----|
| data1 | data2 | data3 | ... |
Please find the github link on using ImportExcel module.
https://github.com/dfinke/ImportExcel/tree/master/Examples
$outputFile = "C:\Temp\OutputExcelFile.xlsx" #Output File
$csvFiles = Get-childItem -Filter *.csv # Filtering CSV file in my present working dir
foreach ($csvFile in $csvFiles) {
#Import csv file and export it contents to Output excel file and rename the sheet.
Import-csv $csvFile | Export-Excel $outputFile -WorksheetName $csvFile.BaseName
}
Hope it helps.

How do I export a to an excel spreadsheet using powershell

I have this PowerShell script that strips html tags and just leaves the text and have it display the word count for that html file when the script is executed. My question is when I execute:
function Html-ToText {
param([System.String] $html)
# remove line breaks, replace with spaces
$html = $html -replace "(`r|`n|`t)", " "
# write-verbose "removed line breaks: `n`n$html`n"
# remove invisible content
#('head', 'style', 'script', 'object', 'embed', 'applet', 'noframes', 'noscript', 'noembed') | % {
$html = $html -replace "<$_[^>]*?>.*?</$_>", ""
}
# write-verbose "removed invisible blocks: `n`n$html`n"
# Condense extra whitespace
$html = $html -replace "( )+", " "
# write-verbose "condensed whitespace: `n`n$html`n"
# Add line breaks
#('div','p','blockquote','h[1-9]') | % { $html = $html -replace "</?$_[^>]*?>.*?</$_>", ("`n" + '$0' )}
# Add line breaks for self-closing tags
#('div','p','blockquote','h[1-9]','br') | % { $html = $html -replace "<$_[^>]*?/>", ('$0' + "`n")}
# write-verbose "added line breaks: `n`n$html`n"
#strip tags
$html = $html -replace "<[^>]*?>", ""
# write-verbose "removed tags: `n`n$html`n"
# replace common entities
#(
#("&bull;", " * "),
#("&lsaquo;", "<"),
#("&rsaquo;", ">"),
#("&(rsquo|lsquo);", "'"),
#("&(quot|ldquo|rdquo);", '"'),
#("&trade;", "(tm)"),
#("&frasl;", "/"),
#("&(quot|#34|#034|#x22);", '"'),
#('&(amp|#38|#038|#x26);', "&"),
#("&(lt|#60|#060|#x3c);", "<"),
#("&(gt|#62|#062|#x3e);", ">"),
#('&(copy|#169);', "(c)"),
#("&(reg|#174);", "(r)"),
#("&nbsp;", " "),
#("&(.{2,6});", "")
) | % { $html = $html -replace $_[0], $_[1] }
# write-verbose "replaced entities: `n`n$html`n"
return $html + $a | Measure-Object -word
}
And then run:
Html-ToText (new-object net.webclient).DownloadString("test.html")
it displays 4 words that are displayed in the output in PowerShell. How do I export that output from the PowerShell window into a an excel spreadsheet with the column words and the count 4?
The CSV you want just looks like this:
Words
4
it's easy enough to just write that to a text file, Excel will read it. But you're in luck, the output of Measure-Object is already an object with 'Words' as a property and '4' as a value, and you can feed that straight into Export-Csv. Use select-object to pick just the property you want:
$x = Html-ToText (new-object net.webclient).DownloadString("test.html")
# drop the Lines/Characters/etc fields, just export words
$x | select-Object Words | Export-Csv out.csv -NoTypeInformation
I'd be tempted to see if I could use
$x = Invoke-WebResponse http://www.google.com
$x.AllElements.InnerText
to get the words out of the HTML, before I tried stripping the content with replaces.
I figured it out. What I did was added
+ $a | Measure-Object -Word after the #html variable in the script and then ran:
Html-ToText (new-object net.webclient).DownloadString("test.html") + select-Object Words | Export-Csv out.csv -NoTypeInformation and it exported the word count – josh s 1 min ago

Loop through and replace backslash in filename (string)

I am trying to replace a \ contained in the filenames I have queried from the database. My script below loops through a CSV containing project codes. For each project code the database is queried and the project name is retrieved.
However, the projectname contains \ which I am trying to replace.
$startRow = 2
$col = 3
$excel = New-Object -COM Excel.Application
$wb = $excel.Workbooks.Open("\myprojectfolder\projectcodes.csv")
$excel.Visible = $false
for ($i = 1; $i -le $wb.Sheets.Count; $i++){
$sh = $wb.Sheets.Item($i)
$endRow = $sh.UsedRange.Rows.Count
$rangeAddress = $sh.Cells.Item($startRow,$col).Address() + ":" +
$sh.Cells.Item($endRow,$col).Address()
$sh.Range($rangeAddress).Value2 | foreach {
#GET PROJECT NAME TO APPEND TO FOLDER NAME
$projectCode = $_
$Server= "MYSERVER"
$Database = "MYDATABASE"
$SQLQuery = $("SELECT [description] FROM [dbo].[projects] WHERE [project] = '$projectCode'")
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $SQLQuery
$Reader = $Command.ExecuteReader()
while ($Reader.Read()) {
$projectName = $Reader.GetValue($1)
#CHECK AND REPLACE '\' CHARACTER IN PROJECTNAME
if ($projectName -like '*\\*') {
Write-Debug "PROJECT NAME CONTAINS \"
$projectName.Replace('\\', '_')
}
$folderPath = "\\myfolder\"
$pathTogether = $folderPath + $projectCode + "_" + $projectName + "\"
New-Item -Path $pathTogether -Type Directory -force
#CHECK IF FILE EXISTS IN APPROPRIATE DIRECTORY
$testFile = $pathTogether + $projectCode + "_" + $projectName + ".xlsm"
$fileExist = Test-Path $testFile
if ($fileExist -eq $false) {
$templateFile = $folderPath + "my_template\my_template.xlsm"
Copy-Item $templateFile $pathTogether
$newPath = $pathTogether + "\my_template.xlsm"
$saveFile = $projectCode + "_" + $projectName + ".xlsm"
$renameToOLD = $projectCode + "_" + $projectName + "_RENAMED" + ".xlsm"
#RENAME PROJECT FILE TO HAVE OLD IN FILENAME
Rename-Item $newPath $saveFile
$projectxlFile = New-Object -COM Excel.Application
$projectxlFile.workbooks.open($pathTogether + "\" + $saveFile)
$queryWS = $projectxlFile.worksheets.Item("Query")
$queryWS.Cells.Item(8,2) = $projectCode
$projectxlFile.DisplayAlerts = $False
$projectxlFile.Visible = $False
$savePath = $pathTogether + $saveFile
#Add-Type -AssemblyName Microsoft.Office.Interop.Excel
#$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbookMacroEnabled
$projectxlFile.ActiveWorkbook.Save()
$projectxlFile.Workbooks.Close()
$projectxlFile.Quit()
$ProcID = Get-Process |
Where-Object {$_.MainWindowHandle -eq $projectxlFile.HWND} |
Select -ExpandProperty ID
Get-Process -Id $ProcID | Stop-Process -Force
##[System.Runtime.Interopservices.Marshal]::ReleaseComObject($projectxlFile)
}
}
$Connection.Close()
}
}
$excel.Workbooks.Close()
$excel.Quit()
$ProcID2 = Get-Process |
Where-Object {$_.MainWindowHandle -eq $excel.HWND} |
Select -ExpandProperty ID
Get-Process -Id $ProcID2 | Stop-Process -Force
###[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
$projectName.Replace('\\', '_')
By default the Replace() method does regular string replacements, so the above would only replace double backslashes with an underscore. Also, it doesn't replace in-place, so you need to assign the modified string back to the variable:
$projectName = $projectName.Replace('\', '_')
The -like operator does wildcard matches, so you mustn't escape the backslash in that expression either, otherwise you don't even get to the replacement operation:
if ($projectName -like '*\*') {
Write-Debug 'PROJECT NAME CONTAINS \'
$projectName = $projectName.Replace('\', '_')
}

Powershell - Get Last 3 char from AD attribute in PsObject

I got a script which export to csv some AD attributes
I want to have the last 3 characters of the 'initials' attribute in a PsObject but i have an error and i spent Hours on this...
Could you help me ?
The Error:
*Method invocation failed because [Microsoft.ActiveDirectory.Management.ADUser] doesn't contain a method named 'substring'.
At C:\scripts\ExtractDWH\Untitled2.ps1:15 char:26
+ "Test" = $_.substring <<<< ($_.initials.length - 3, 3)
+ CategoryInfo : InvalidOperation: (substring:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound*
The Code :
$output = 'c:\scripts\ExtractDWH\consultants_test.csv'
$users = Get-ADUser -Filter * -SearchBase "ou=Rennes,ou=Consultants,ou=Utilisateurs,ou=FedFinance,dc=dfedinterim,dc=fr" - Properties * | ? { ($_.initials -notlike 'IC*') -and ($_.initials -notlike 'IM*') -and ($_.initials -ne $null) }
fileIn | % {
$users | % {
New-Object psobject -Property #{
"ID" = $_.initials
"Last 3 strings ID" = $_.substring($_.initials.length - 3, 3)
"Centre Imputation" = $_.extensionAttribute10
"Date Entrée" = $_.extensionAttribute9
# The line below does not work
"Test" = $_.substring($_.initials.length - 3, 3)
}
}
} | Select-Object ID, 'Centre Imputation', 'Date Entrée', 'Test'
| Export-CSV $output -Delimiter ';' -Encoding "UTF8" -NoTypeInformation `
Thank you !
Change this:
"Last 3 strings ID" = $_.substring($_.initials.length - 3, 3)
To This:
"Last 3 strings ID" = $_.initials.substring($_.initials.length -3, 3)
Avshalom showed you the error. You can also use the following to access the last 3 characters:
"Test" = $_.initials[-3 .. -1] -join ''

PowerShell string concatenation gives newlines

Note: The actual error from Java/Closure Compiler came from the missing t in --js-outputfile!
I have this PowerShell script:
cls
$jsFiles = #();
Get-ChildItem | Where {$_.PsIsContainer} | Foreach {
$dir = $_.FullName;
$jsFile = $dir + "\" + $_.Name + ".js";
if (Test-Path ($jsFile)) {
$jsFiles += $jsFile;
}
}
$wd = [System.IO.Directory]::GetCurrentDirectory();
# Build Closure Compiler command line call
$cmd = #("-jar $wd\..\ClosureCompiler\compiler.jar");
Foreach ($file in $jsFiles) {
# Both insert a newline!
$cmd += "--js $file";
#$cmd = "$cmd --js $file";
}
$cmd = "$cmd --js_ouput_file $wd\all.js";
Invoke-Expression "java.exe $cmd"
The problem is that newlines are inserted on each += or $cmd = "$cmd str" call!
Echoargs gives me this output:
Arg 0 is <-jar>
Arg 1 is <S:\ome\Path\compiler.jar>
Arg 2 is <--js>
Arg 3 is <S:\ome\Path\script1.js>
Arg 4 is <--js>
Arg 5 is <S:\ome\Path\script2.js>
...
Arg 98 is <--js_ouput_file>
Arg 99 is <S:\ome\Path\all.js>
(Possibly) therefore, I get some errors from java.exe:
java.exe : "--js_ouput_file" is not a valid option
At line:1 char:1
+ java.exe -jar ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: ("--js_ouput_file" is not a valid option:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Try to rewrite to much simple version:
cls
$wd = [System.IO.Directory]::GetCurrentDirectory();
# Build Closure Compiler command line call
$cmd = "-jar $wd\..\ClosureCompiler\compiler.jar";
$arrayOfJs = Get-ChildItem -Recurse -Include "*.js" | % { "--js $_.FullName" };
$cmd += [string]::Join(" ", $arrayOfJs);
Invoke-Expression "java $cmd --js_ouput_file $wd\all.js"
When you do
$cmd = #(...);
you are creating an array and hence the subsequent += on it is appending elements to the array and is not string concatenation. Just have it as a string, or before using $cmd. Do something like:
$cmd -join " "
Which will join together the elements into a single space-separated string. By default when the array is coerced into a string, you will see new lines between the elements.

Resources