Shell script - set autofilter on a existing xls-file - powershell

I want to set the filters on an existing .xls-file by running a shell script from the command line.
powershell -c "$excelObj = New-Object -ComObject Excel.Application;$excelWorkBook = $excelObj.Workbooks.Open(\"C:\Users\Desktop\Papierkorb\Test\test2.xlsx\");$excelWorkSheet = $excelObj.WorkSheets.item(\"Sheet1\");$excelWorkSheet.activate();$headerRange = $excelWorkSheet.Range(\"A1\",\"A1\").AutoFilter() | Out-Null;$excelWorkBook.Save();$excelWorkBook.Close();$excelObj.Quit()"
I am getting an error message:
Unable to get the AutoFilter property of the Range class
At line:1 char:231
I tried several adaptions with the Range, but could not fix it.
Thanks for your help,

It is not possible to set AutoFilter on a range without data.
Try to put some text into cell "A1" in the test2.xlsx file (either using Excel, or problematically with PowerShell, example below). You can even put empty string ''.
The following works for me.
$excelObj = New-Object -ComObject Excel.Application;$excelWorkBook = $excelObj.Workbooks.Open("d:\temp\test2.xlsx");
$excelWorkSheet = $excelObj.WorkSheets.item("Sheet1");
$excelWorkSheet.activate();
$headerRange = $excelWorkSheet.Range("A5","A5") ;
$headerRange.Item(1,1) = 'Something'
$headerRange.AutoFilter() ;
#$headerRange.AutoFilter() | Out-Null;
$excelWorkBook.Save();
$excelWorkBook.Close();
$excelObj.Quit()

Related

PowerShell - How can I select the last row of the last table in a Word document, copy it, and insert as a new row at the bottom of the table?

I am trying to increase the level of automation within my day-to-day work, part of which involves adding a line to the end of a table within a report that contains largely the same information, with a few cells changed (new dates).
I have a little experience with VB and C++, but I am very much an amateur when it comes to PowerShell, which seems to be the go-to for task automation.
I have a couple of PowerShell scripts that search through the body of the report and change text, but the last part of the report is a record, and needs appending as opposed to amending.
How would I go about this?
I have tried mangling a few bits of PowerShell code I've found online, to no avail. I have gotten as far as selecting a row in the correct table, but I have no idea how I might then select the last row, copy this and insert it beneath as a new row at the bottom of the table:
$objWord = New-Object -ComObject word.application
$objWord.Visible = $True
$objWord.Documents.Open("FILEPATH")
$FindText = "KEYTEXT"
$objWord.Selection.Find.Execute($FindText)
$objWord.Selection.SelectRow()
Here's a lengthy example with explanations that does what you're looking for:
# open document
$objWord = New-Object -ComObject word.application
$objWord.Visible = $True
$doc = $objWord.Documents.Open("C:\temp\temp.docx")
# search for row
$FindText = "Value2"
$result = $objWord.Selection.Find.Execute($FindText)
$objWord.Selection.SelectRow()
# copy the ranges from searched row
$table = $objWord.Selection.Tables[1]
$copiedCells = $objWord.Selection.Cells | select columnindex,rowindex
# add row at the end of table
$table.Rows[$table.Rows.Count].Select()
$objWord.Selection.InsertRowsBelow(1)
# insert copied text into each column of last row
foreach ($cell in $copiedCells) {
# copy value from cell in copied row
$copiedText = $table.Cell($cell.rowindex, $cell.columnindex).Range.Text
# remove last 2 characters (paragraph and end-of-cell)
$TrimmedText = $copiedText.Remove($copiedText.Length - 2)
# set value for cell in last row
$table.Cell($table.Rows.Count,$cell.columnindex).Range.Text = $TrimmedText
}

Powershell GUI Imported text file missing newline/carriage return

I have a powershell GUI which imports a text file and displays it in a textbox when a button is clicked
But even though the text file contains one entry per line when it gets displayed in the textbox it is all on one line...
The text file looks like this-
But when I import it it looks like this-
This is the code I am using-
$button_hosts = New-Object system.windows.Forms.Button
$button_hosts.Text = "Hosts"
$button_hosts.Width = 60
$button_hosts.Height = 25
$button_hosts.location = new-object system.drawing.point(20,55)
$button_hosts.Font = "Microsoft Sans Serif,10"
$mydocs = [Environment]::GetFolderPath('MyDocuments')
$button_hosts.Add_Click({
$textBox_hosts.Text = Get-Filename "$mydocs" txt
$textBox_hostlist.Text = Get-Content $textBox_hosts.Text
})
$GUI.controls.Add($button_hosts)
Any idea how to get it to display the same? I cant add any extra data to the txt file as it is an output from another program
Set the lines property, not the text property.
$textBox_hostlist.Lines = Get-Content $textBox_hosts.Text
Get-Content reads the content one line at a time and returns a collection of objects, each of which represents a line of content.
The means that you have to join the collection with carriage returns and linefeeds:
(Get-Content $textBox_hosts) -Join "`r`n"
For your WinForms TextBox, do you have the multiline property set to true?
https://msdn.microsoft.com/en-us/library/12w624ff(v=vs.110).aspx
If not, it defaults to single line.

How to avoid a int value output when I use powershell script to query database?

my recent project need to use powershell to query data from mysql database, and I find a good script to do it, the only issue is that the script will always output a int value (which is exactly the number of the return item). How to avoid this output? The only thing I want to get is the query data.
The following is the script:
$mysqlDllFilePath = "C:\temp\powerShellScript\MySQL.Data.dll"
[void][system.reflection.Assembly]::LoadFrom($mysqlDllFilePath)
$myconnection = New-Object MySql.Data.MySqlClient.MySqlConnection
$myconnection.ConnectionString = "server=localhost;userid=root;password=admin;database=temp;pooling=false"
$myconnection.Open()
$mycommand = New-Object MySql.Data.MySqlClient.MySqlCommand
$mycommand.Connection = $myconnection
$mycommand.CommandText = "SELECT SiteGuid,SiteName,ProductEdition,ProductVersion from site"
$adapter = new-object MySql.Data.MySqlClient.MySqlDataAdapter
$adapter.SelectCommand = $mycommand
$ds = new-object System.Data.DataSet
$adapter.Fill($ds)
$myconnection.close()
$ds.Tables[0]
And the output of the script is:
PS C:\Users\jason> C:\temp\powerShellScript\Get-ConfigSite.ps1
2
SiteGuid SiteName ProductEdition ProductVersion
-------- -------- -------------- --------------
123f7267-757c-4864-b0... simulatedSite PLT 7.1
526f7267-757c-4864-b0... simulatedSite PLT 7.1
How to avoid output value "2"??
Ok, I found the reason of the output, which is the output of "$adapter.Fill($ds)" statement, and if I declare a variable to hold this value, it will not output to the console. Maybe that's the grammar of powershell.
The solution is change that statement to "$count=$adapter.Fill($ds)".
Anyway, thanks.

How to create a powerpoint table using powershell

I'm currently trying to create a calendar in powerpoint using powershell. All I want to do is insert a table into a powerpoint slide. This table is representing the month of January, it contains the days of the week etc.
I did some research and came across this.
This is VB script, so i tried to "create its equivalent" in powershell:
EDIT3: I was finally able to copy my table from Excel and paste it into my powerpoint slide using this code:
#Create an instance of Excel.
$xl=New-Object -ComObject "Excel.Application"
$xl.Visible = $True
#Open the Excel file containing the table.
$wb = $xl.workbooks.open("C:\January.xls")
$ws = $wb.ActiveSheet
#Select the table.
$range = $ws.range("A1:G7")
$range.select()
#Copy the table to the clipboard.
$range.copyPicture()
#Create an instance of Powerpoint.
$objPPT = New-Object -ComObject "Powerpoint.Application"
$objPPT.Visible ='Msotrue'
#Add a slide to the presentation.
$project = $objPPT.Presentations.Add()
$slide = $project.Slides.Add(1, 1)
#Paste the table into the slide.
$shape = $slide.Shapes.Paste()
#Position the table.
$shape.Left = 50
$shape.Top = 150
$shape.Width = 300
$shape.Height = 168
Thanks to those who have helped me here and on #powershell
I saw your question in the channel.
This worked for me:
$presentation = $ppt.Presentations.Open($ifile)
$sl = $presentation.Slides.Add(1, $ppLayoutBlank)
$shape = $sl.shapes.paste()
I think you can use this:
# Optionally, make sure you're on the last slide:
$ppt.ActiveWindow.View.GotoSlide( $ppt.ActivePresentation.Slides.Count )
# Specify the
$ppt.ActiveWindow.View.PasteSpecial( "ppPasteOLEObject", "msoFalse", "", 0, "", "msoFalse")
See the
MSDN Interop Docs
and thanks to this example:
Paste Excel Chart into Powerpoint using VBA

Parse HTML Table in PowerShell V3

I have the following HTML table Link To the HTML
I want to parse it and convert it to XML/CSV/PS Object,
I tried to do with HtmlAgilityPack.dll but no success.
Can anybody give me any directions to do it?
I want to convert the table to a PSObject and export it to csv,
I currently have just the beginning of the code,
and access to the lines but i can't access to the values in the lines
Add-Type -Path C:\Windows\system32\HtmlAgilityPack.dll
$HTML = New-Object HtmlAgilityPack.HtmlDocument
$res = $HTML.Load("C:\Test\Test.html")
$table = $HTML.DocumentNode.SelectNodes("//table/tr/td/nobr")
when i access to $table[0..47].InnerHtml i get only the first ** column ** of the file,
i can't access to the 2nd and etc
Thanks Ohad
you can try this to get all the html in <nobr> tags. I let you find the logic to output what you want...
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://urltoyourfile.html")
$doc = $ie.Document
($doc.getElementsByTagName("nobr"))|%{$_.innerHTML}
Output :
Lead User
Accesses
Last Accessed
Average
Max
Min
Total
amirt</NO br>
2
01/20/2013 09:40:47
04:18:17
06:19:26
02:17:09
08:36:35
andream
1
01/20/2013 10:33:01
02:34:37
02:34:37
02:34:37
02:34:37
avnerm
1
01/17/2013 11:34:16
00:30:44
00:30:44
00:30:44
00:30:44
brouria
a way to parse it :
($doc.getElementsByTagName("nobr"))|%{
write-host -nonew $_.innerHTML";"
$cpt++
if ($cpt % 8 -eq 0){$cpt=1;write-host ""}
}