Open IE maximized from Powershell script? - powershell

Pardon me if this is too simple a question, but I'm not finding anything in the help files or online so far regarding doing this. I'm opening up a new browser window to test the login/logout feature of a web based application, but I want to open the IE window in maximized mode. I could set the size as:
$ie.height = 1024
$ie.width - 768
But is there a keyword or anything that I can use to just open it up maximized automatically or would I need to query the screen size first and then fill in the values from that query?
/matt

(new-object -com wscript.shell).run("url",3)

Solved the Problem of starting IE maximized with following:
Function maxIE
{
param($ie)
$asm = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$ie.Width = $screen.width
$ie.Height =$screen.height
$ie.Top = 0
$ie.Left = 0
}
cls
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
maxIE $ie
while ($ie.busy) {sleep -milliseconds 50}
$ie.navigate("http://www.google.com")

I couldn't get any specific answer to work, but did get a combination to work. The order in which these are called is important else it doesn't work.
Full Example:
$ie = New-Object -Com "InternetExplorer.Application"
$urls = #("http://www.google.com","http://www.yahoo.com")
$ie.Visible = $true
CLS
write-output "Loading pages now..."
#Maximize IE window
$asm = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$ie.height = $screen.height
#Open webpages
foreach ($link in $urls) {
$ie.Navigate2($link, 0x1000)
}
#close first blank tab
$sa = New-Object -ComObject Shell.Application
$tab = $sa.windows() | Where {$_.Name -match 'Internet Explorer' -and $_.LocationName -eq ''}
$tab.quit()

If you have the PowerShell Community Extensions 1.2 (PSCX) installed on PowerShell 2.0, I have verified that this works:
Pscx\Start-Process IExplore.exe; Start-Sleep 3; $hwnd = Get-ForegroundWindow
$sig = #'
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
'#
Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32
[Win32.NativeMethods]::ShowWindowAsync($hwnd, 3)
It is a little dicey because it is using a wait (start-sleep) of 3 secs to wait for IE to open and then it uses a PSCX cmdlet to get the window handle of the foreground window. If you only have one instance of IExplore running then you can use this to get that handle:
#(Get-Process IExplore)[0].MainWindowHandle
PowerShell 2.0 is required for the Add-Type support that allows us to call down to the Win32 API.
BTW from a quick Bing search it seems that getting IE to start maximized is a pretty common problem. For instance, with Start-Process you can specify -WindowStyle Maximized but IE doesn't honor that.

Just incase anyone else needs help I ended up just calling up iexplore maximized and then connected to it. You need to sleep because it calls too fast to connect. I'm sure there is a better way but I couldn't figure it out.
start iexplore -WindowStyle maximized
Start-Sleep -seconds 1
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" }
$ie.navigate("URL")

#We will use the Win32 API function ShowWindowAsync, and spawn an IE Window Maximized.
#Parameters can be used for ShowWindowAsync
$Hide = 0
$Normal = 1
$Minimized = 2
$Maximized = 3
$ShowNoActivateRecentPosition = 4
$Show = 5
$MinimizeActivateNext = 6
$MinimizeNoActivate = 7
$ShowNoActivate = 8
$Restore = 9
$ShowDefault = 10
$ForceMinimize = 11
#Specify an interwebs address :)
$URL="http://www.google.com/"
#Create internetexplorer.application object
$IE=new-object -com internetexplorer.application
#Set some parameters for the internetexplorer.application object
$IE.TheaterMode = $False
$IE.AddressBar = $True
$IE.StatusBar = $False
$IE.MenuBar = $True
$IE.FullScreen = $False
$IE.visible = $True
#Navigate to the URL
$IE.navigate2($URL)
#the C#-style signature of an API function
$code = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
#add signature as new type to PowerShell (for this session)
$type = Add-Type -MemberDefinition $code -Name myAPI -PassThru
#Magic:
$type::ShowWindowAsync($IE.HWND[0], $Maximized)

Related

PowerShell web scraping doesn't work in background

I wrote a script that works fine:
# Use Internet Explorer
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
# Set Credentials
$username="name.surname#mail.com"
$password="password"
#Navigate to URL
$ie.Navigate("https://service.post.ch/zopa/dlc/app/?service=dlc-web&inMobileApp=false&inIframe=false&lang=fr#!/main")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
# Login
$usernamefield = $ie.document.getElementByID('isiwebuserid')
$usernamefield.value = "$username"
$passwordfield = $ie.document.getElementByID('isiwebpasswd')
$passwordfield.value = "$password"
$Link = $ie.document.getElementByID('actionLogin')
$Link.click()
Start-Sleep -seconds 5
# Find file to download
$link = $ie.Document.getElementsByTagName('A') | where-object {$_.innerText -like 'post_adressdaten*'}
$link.click()
Start-Sleep -seconds 3
# Press "Alt + s" on the download dialog
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%s")
Start-Sleep -seconds 3
# Quit Internet Explorer
$ie.Quit()
But if I change $ie.Visible= $true to $ie.Visible= $false the script doesn't work.
Why?
Because of these two lines:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%s")
In these two lines I'm working on the download dialog box of Internet Explorer and if the browser works in background the the script cannot click on it.
How can I send the input in background or in alternative how to keep Internet Explorer always on top?
As your own answer implies, in order to be able to send keystrokes to an application with [System.Windows.Forms.SendKeys]::SendWait(), it must have a window that is (a) visible and (b) has the (input) focus.
A simpler and faster alternative to the technique shown in your answer - where you use ad-hoc compilation of C# code that wraps WinAPI functions via P/Invoke declarations, via Add-Type - is the following:
# Create an Internet Explorer instance and make it visible.
$ie = New-Object -ComObject 'internetExplorer.Application'; $ie.Visible= $true
# Activate it (give it the focus), via its PID (process ID).
(New-Object -ComObject WScript.Shell).AppActivate(
(Get-Process iexplore | Where-Object MainWindowHandle -eq $ie.hWnd).Id
)
Taking a step back:
GUI scripting (automating a task by simulating user input to a GUI) is inherently unreliable; for instance, the user may click away from the window that is expected to have the focus.
While there is no built in solution, it sounds like Selenium offers robust programmatic browser control.
The third-party Selenium PowerShell module is a PowerShell-friendly wrapper for it (available via the PowerShell Gallery and therefore with Install-Module Selenium), but I don't know if it still works (the project is looking for maintainers as of this writing).
The closest thing I have found is this and it's ugly as hell:
# Start Internet Explorer on top
Add-Type -TypeDefinition #"
using System;
using System.Runtime.InteropServices;
public class Win32SetWindow {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"#
$ie = new-object -comobject InternetExplorer.Application;
$ie.visible = $true;
[Win32SetWindow]::SetForegroundWindow($ie.HWND) # <-- Internet Explorer window on top
# Set Credentials
$username="name.surname#mail.com"
$password="password"
#Navigate to URL
$ie.Navigate("https://service.post.ch/zopa/dlc/app/?service=dlc-web&inMobileApp=false&inIframe=false&lang=fr#!/main")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
# Login
$usernamefield = $ie.document.getElementByID('isiwebuserid')
$usernamefield.value = "$username"
$passwordfield = $ie.document.getElementByID('isiwebpasswd')
$passwordfield.value = "$password"
$Link = $ie.document.getElementByID('actionLogin')
$Link.click()
Start-Sleep -seconds 5
# Find file to download
$link = $ie.Document.getElementsByTagName('A') | where-object {$_.innerText -like 'post_adressdaten*'}
$link.click()
Start-Sleep -seconds 3
# Press "Alt + s" on the download dialog
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%n{TAB}{ENTER}") # or use SendWait("%s")
Start-Sleep -seconds 3
# Quit Internet Explorer
$ie.Quit()

ProgressBar shows on ISE but not on Console. Powershell responsive GUI on Runspace

I've been working on a simple proof of concept / template of a script in which I have the default Runspace to run heavy tasks and another one to run a responsive GUI.
I have tested various methods to be able to communicate the runspaces. At first I tried the Control.Invoke but some opinions on different forums and a weird behaviour on tests led me to use a message based intercomunication based on a Synchronized HashTable. ProgressBar works with control.Invoke, but, executing some other actions, like disabling multiple buttons on a form performs very slow.
1st problem: I would like to show a progressbar (marquee) when the task is executing, changing its visible state. However, the progressbar is showed when the script runs on ISE, but not when it does on console. I think it is because main runspace is busy, but I don't understand how it could affect the GUI runspace...
2nd: on the script I post below, I'm using scriptblocks through a stack variable to pass the commands between runspaces. Then, each runspace (main runspace does it through pooling and GUI through a timer) checks if a task is pending of execution in the stack, and, if so, executes it. However if I would want to call a function declared on the other runspace (Test-OutOfMainScopeUiFunction in this example), I couldn't. I would get a runtime error on the scriptblock. I have thought solutions to this like:
-Importing functions on both runspaces
-Making functions global or use functon delegates¿?
-Passing a string with the commands to execute in spite of a scriptblock -> Using this one at the moment, but don't like very much... error prone
Any solution to the progress bar problem or script improvements will be appreciated. Thanks!
$global:x = [Hashtable]::Synchronized(#{})
$global:x.MainDispatcher = new-object system.collections.stack
$global:x.UiDispatcher = new-object system.collections.stack
$global:x.GuiExited = $false
$formSB = {
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
[reflection.assembly]::LoadWithPartialName("System.Drawing")
Function Test-OutOfMainScopeUiFunction
{
$x.Host.Ui.WriteLine("Test-OutOfMainScopeUiFunction Executed")
}
Function Execute-OnMainRs
{
param(
[Parameter(Mandatory=$true)]
[ScriptBlock]$ScriptBlock
)
$x.Host.Ui.WriteLine("`r`nAdding task")
$x.MainDispatcher.Push($ScriptBlock)
$x.Host.Ui.WriteLine("Task added")
$x.Host.Ui.WriteLine("Task: $($x.MainDispatcher)")
}
$form = New-Object System.Windows.Forms.Form
$button = New-Object System.Windows.Forms.Button
$button.Text = 'click'
$button.Dock = [System.Windows.Forms.DockStyle]::Right
$progressBar = (New-Object -TypeName System.Windows.Forms.ProgressBar)
$ProgressBar.Style = [System.Windows.Forms.ProgressBarStyle]::Marquee
$ProgressBar.MarqueeAnimationSpeed = 50
$ProgressBar.Dock = [System.Windows.Forms.DockStyle]::Bottom
$ProgressBar.Visible = $false
$label = New-Object System.Windows.Forms.Label
$label.Text = 'ready'
$label.Dock = [System.Windows.Forms.DockStyle]::Top
$timer=New-Object System.Windows.Forms.Timer
$timer.Interval=100
$timer.add_Tick({
if($x.UiDispatcher.Count -gt 0)
{
& $($x.UiDispatcher.Pop())
}
})
$form.Controls.add($label)
$form.Controls.add($button)
$form.Controls.add($progressBar)
Add-Member -InputObject $form -Name Label -Value $label -MemberType NoteProperty
Add-Member -InputObject $form -Name ProgressBar -Value $progressBar -MemberType NoteProperty
$button.add_click({
Execute-OnMainRs -ScriptBlock {
write-host "MainRS: Long Task pushed from the UI started on: $(Get-Date)"
start-sleep -s 2
write-host "MainRS: Long Task pushed from the UI finished on: $(Get-Date)"
}
})
$form.add_closed({ $x.GuiExited = $true })
$x.Form = $form
$timer.Start()
$form.ShowDialog()
}
Function Execute-OnRs
{
param(
[Parameter(Mandatory=$true)]
[ScriptBlock]$ScriptBlock
)
$x.Host = $Host
$rs = [RunspaceFactory]::CreateRunspace()
$rs.ApartmentState,$rs.ThreadOptions = "STA","ReUseThread"
$rs.Open()
$rs.SessionStateProxy.SetVariable("x",$x)
$ps = [PowerShell]::Create().AddScript($ScriptBlock)
$ps.Runspace = $rs
$handle = $ps.BeginInvoke()
#Almacenar variables del RS
$x.Handle = $handle
$x.Ps = $ps
}
Function Execute-OnUiRs
{
param(
[Parameter(Mandatory=$true)]
[ScriptBlock]$ScriptBlock
)
$x.UiDispatcher.Push($ScriptBlock)
}
Function Dispatch-PendingJobs
{
while($global:x.GuiExited -eq $false) {
if($global:x.MainDispatcher.Count -gt 0)
{
Execute-OnUiRs -ScriptBlock {
$msg = "UIRS: MainThread informs: Long Task started on $(Get-Date)."
$global:x.Form.Label.Text = $msg
$global:x.Form.ProgressBar.Visible = $true
$x.host.ui.WriteLine($msg)
#Next line throws an error visible on UI runspace error stream
Test-OutOfMainScopeUiFunction
}
& $($global:x.MainDispatcher.Pop())
Execute-OnUiRs -ScriptBlock {
$msg = "UIRS: MainThread informs: Long Task finished on $(Get-Date)."
$global:x.Form.Label.Text = $msg
$global:x.Form.ProgressBar.Visible = $false
$x.host.ui.WriteLine($msg)
}
write-host "UI Streams: $($global:x.Ps.Streams |out-string)"
}
else
{
start-sleep -m 100
}
}
}
Found the solution... http://community.idera.com/powershell/powertips/b/tips/posts/enabling-visual-styles
VisualStyles must be enabled first. The problem is not related with runspaces stuff. This is a brief and clearer code example taken from Power Shell marquee progress bar not working with the fix.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$window = New-Object Windows.Forms.Form
$window.Size = New-Object Drawing.Size #(400,75)
$window.StartPosition = "CenterScreen"
$window.Font = New-Object System.Drawing.Font("Calibri",11,[System.Drawing.FontStyle]::Bold)
$window.Text = "STARTING UP"
$ProgressBar1 = New-Object System.Windows.Forms.ProgressBar
$ProgressBar1.Location = New-Object System.Drawing.Point(10, 10)
$ProgressBar1.Size = New-Object System.Drawing.Size(365, 20)
$ProgressBar1.Style = "Marquee"
$ProgressBar1.MarqueeAnimationSpeed = 20
$window.Controls.Add($ProgressBar1)
$window.ShowDialog()

How to take screenshot of full page through simple program?

My requirement is to take screenshot of full page in Internet Explorer. I am aware of different tools available online for doing the same but since I want to achieve this on system where I don't have Admin rights, I can do it only with some program which doesn't require any installation.
I am looking for a program which can accept input(site URLs) from excel and then take full page screen shots of those sites.
Till now I only managed to open a site in IE, then do F11 to maximize screen and take screenshot of it. But, that gives cropped page snap since page height is more. I tried this with PowerShell.
Any suggestion/ideas?
Capturing a screenshot
Param(
#[Parameter(Mandatory = $true)][string]
$Path = "C:\Sagar\Screens"
)
$FileName = "$env:COMPUTERNAME - $(get-date -f yyyy-MM-dd_HHmmss).bmp"
$File = "$Path\$FileName"
Add-Type -AssemblyName System.Windows.Forms
Add-type -AssemblyName System.Drawing
# Gather Screen resolution information
$Screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
$Width = $Screen.Width
$Height = $Screen.Height
$Left = $Screen.Left
$Top = $Screen.Top
$ie = New-Object -com internetexplorer.application
$ie.Visible = $true
$ie.Navigate2("https://www.google.com/webhp?sourceid=chrome- instant&ion=1&espv=2&ie=UTF-8#q=how%20to%20send%20keys%20through%20powershell")
while($ie.busy){
Sleep -Seconds 2
}
Sleep -Seconds 2
$sig = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32
# Minimize window
[Win32.NativeMethods]::ShowWindowAsync($ie.HWND, 2)
# Restore window
[Win32.NativeMethods]::ShowWindowAsync($ie.HWND, 3)
#$ie.FullScreen = $true
Sleep -seconds 3
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("{F11}")
sleep -Seconds 2
Add-Type -AssemblyName System.Drawing
# Create bitmap using the top-left and bottom-right bounds
$bitmap = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $ie.Width, $ie.Height
# Create Graphics object
$graphic = [System.Drawing.Graphics]::FromImage($bitmap)
$ie.Left
$ie.Top
# Capture screen
$graphic.CopyFromScreen($ie.Left, $ie.Top, 0, 0, $bitmap.Size)
# Save to file
$bitmap.Save($File)
Write-Output "Screenshot saved to:"
Write-Output $File
#
Found a solution to this. There is this thing called 'Watin' which helps in automation using IE and Firefox. It has a method to capture full page screenshot. Though screenshot quality is average, Watin works perfectly.
$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$filePath = $executingScriptDirectory + "\WatiN-2.1.0.1196\bin\net40\WatiN.Core.dll" #$executingScriptDirectory + "\net20\WatiN.Core.dll"
$filePath1 = $executingScriptDirectory + "\WatiN-2.1.0.1196\bin\net40\Interop.SHDocVw.dll"
[System.Reflection.Assembly]::LoadFile($filePath);
[System.Reflection.Assembly]::LoadFile($filePath1);
$url = "https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=how%20to%20send%20keys%20through%20powershell"
$ie = new-object WatiN.Core.IE $URL
while($ie.busy){
sleep -milliseconds 50
}
$ie.BringToFront()
$ie.CaptureWebPageToFile("C:\Sagar\Scripts\IsIt.jpg");

PowerShell - Disable Win+D Desktop-View for my Script/Form only

I want to know if it´s possible to disable Win+D Windows-Shortkey?
And the "view Desktop"-Button in the bottom right corner must be disabled, too.
Only for my Script and I´ve got the approach to catch this two processes and after completely minimizing all running programms, THAN open up the Script (only) again. So the Script is everytime viewable on the desktop.
The Script owns a Form without a Controlbox.
Suggest it like a Windows-Sidebar-Element that is allways viewable on the Desktop.
To Save the $PID in a extra LOCAL-File (Script running on each PC from a Server) is necessary, because an other Script in the background run the Script again, if a worker would kill the Task.
Here is my Script Code:
#Clear Sessions
Get-PSSession | Remove-PSSession
#Save current used Process-ID in a TextFile
$PID > C:\Users\Public\Documents\PID.txt
#Imports
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#$Icon = New-Object System.Drawing.Icon ("Icon-Path here")
#Get NetworkData
$hostname = $env:computername
$IPAdress = (gwmi Win32_NetworkAdapterConfiguration | ? { $_.IPAddress -ne $null }).ipaddress[0]
#Create a Form
$myForm = New-Object System.Windows.Forms.Form
$myForm.DataBindings.DefaultDataSourceUpdateMode = 0
$myForm.Size = New-Object System.Drawing.Size(190,70)
$myForm.Text ="Nutzerdaten"
#Optic-Settings
$myForm.FormBorderStyle = "FixedDialog"
$myForm.MaximizeBox = $false
$myForm.MinimizeBox = $false
$myForm.ControlBox = $false
$myForm.StartPosition = "CenterScreen"
$myForm.ShowInTaskbar = $false #Close-Window-Button unlocked
$myForm.Opacity = 0.6 #0.1 - 1 for transparancy
#$myForm.Icon = $Icon
#Disable Win+D
$myForm.Add_KeyDown({
if( $_.KeyCode -eq "LWin" )
{
Start-Sleep -Seconds 5
[System.Windows.Forms.MessageBox]::Show( "Später wird das Fenster wieder sichtbar gemacht, wenn man Win+D gedrückt hat." )
}
})
#Disable normal closeing (only by crash Script or end PwerShell in Task-Manager)
$myForm.add_FormClosing({$_.Cancel = $true})
#Label with Hostname Data
$lblHost = New-Object System.Windows.Forms.Label
$lblHost.Size = New-Object System.Drawing.Size(180,15)
$lblHost.Top = "5"
$lblHost.Left = "5"
$lblHost.Text = "Hostname : " + $hostname #View Hostname in transparancy mode
$myForm.Controls.Add($lblHost)
#on Click open MessageBox and show closeable Dialog with the IP-Adress and Hostname)
$lblHost.Add_Click({
[System.Windows.Forms.MessageBox]::Show("Hostname = "+$hostname+" | IP-Adress = "+$IPAdress )
})
#Label with IP-Adress (v4 only)
$lblIP = New-Object System.Windows.Forms.Label
$lblIP.Size = New-Object System.Drawing.Size(180,15)
$lblIP.Top = "20"
$lblIP.Left = "5"
$lblIP.Text = "IP-Adress : " + $IPAdress #View Ip-Adress in transparancy mode
$myForm.Controls.Add($lblIP)
#on Click open MessageBox and show closeable Dialog with the IP-Adress and Hostname)
$lblIP.Add_Click({
[System.Windows.Forms.MessageBox]::Show("Hostname = "+$hostname+" | IP-Adress = "+$IPAdress )
})
#View Form/GUI
$myForm.ShowDialog()
Thank you for reading this and I hope that some got any idear how to clear this problem.
Kind regards,
M. Lengl

How to Open; SaveAs; then Close an Excel 2013 (macro-enabled) workbook from PowerShell4

Doing a search on the above Com operations yields links dating to '09 and even earlier. Perhaps it hasn't changed but I find myself bumping up against errors where 'it is being used by another process.' - even though no Excel app is open on my desktop. I have to reboot to resume.
To be clear - I'm trying to open an existing file; immediately SaveAs() (that much works), add a sheet, Save(); Close() - and then, importantly, repeat that cycle. In effect, I'm creating a few dozen new sheets within a loop that executes the above 'Open Master; SaveAs(); Edit Stuff; Save; Close;
From the examples I've seen this is not a typical workflow for PowerShell. Pasted at the very bottom is my provisional script - pretty rough and incomplete but things are opening what they need to open and adding sheet also works - until I know I have the right way to cleanly close stuff out I'm not worried about the iterations.
I've found a couple examples that address closing:
From http://theolddogscriptingblog.wordpress.com/2012/06/07/get-rid-of-the-excel-com-object-once-and-for-all/
$x = New-Object -com Excel.Application
$x.Visible = $True
Start-Sleep 5
$x.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($x)
Remove-Variable x
And from http://social.technet.microsoft.com/Forums/windowsserver/en-US/24e57b61-e792-40c1-8aff-b0a8205f48ab/updated-opened-excel-using-powershell?forum=winserverpowershell
Set-ItemProperty $path -name IsReadOnly -value $false
$Excel.ActiveWorkBook.Save()
$openfile.Close()
$openfile = $null
$Excel.Quit()
$Excel = $null
[GC]::Collect()
<>
function MakeNewBook($theWeek, $AffID){
$ExcelFile = "C:\csv\InvoiceTemplate.xlsm"
$Excel = New-Object -Com Excel.Application
$Excel.Visible = $True
$Workbook = $Excel.Workbooks.Open($ExcelFile)
$theWeek = $theWeek -replace "C:\\csv\\", ""
$theWeek = $theWeek -replace "\.csv", ""
$theWeek = "c:\csv\Invoices\" +$AffID +"_" + $theWeek + ".xlsm"
$SummaryWorksheet = $Workbook.worksheets.Item(1)
$Workbook.SaveAs($theWeek)
return $Excel
}
function MakeNewSheet($myBook, $ClassID){
$SheetName = "w"+$ClassID
#$Excel = New-Object -Com Excel.Application
#$Excel.Visible = $True
$wSheet = $myBook.WorkSheets.Add()
}
function SaveSheet ($myExcel)
{
#$WorkBook.EntireColumn.AutoFit()
#Set-ItemProperty $path -name IsReadOnly -value $false
$myExcel.ActiveWorkBook.Save()
$openfile= $myExcel.ActiveWorkBook
$openfile.Close()
$openfile = $null
$myExcel.Quit()
$myExcel = $null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($myExcel)
Remove-Variable $myExcel
[GC]::Collect()
}
$theWeek = "C:\csv\wkStart2013-11-04.csv"
$x = Import-Csv $theWeek
foreach ($xLine in $x){
if ($x[0]){
$AffID = $x[1].idAffiliate
$myExcel = MakeNewBook $theWeek $x[1].idAffiliate
$ClassID = $x[1].idClass
MakeNewSheet $myExcel $ClassID
continue
}
SaveSheet($myExcel)
$AffID = $_.$AffID
$wID = $xLine.idClass
#MakeNewSheet($wID)
Echo "$wID"
}
As a follow up after playing around with this issue myself. I geared my solution around Ron Thompson's comment minus the function calls:
# collect excel process ids before and after new excel background process is started
$priorExcelProcesses = Get-Process -name "*Excel*" | % { $_.Id }
$Excel = New-Object -ComObject Excel.Application
$postExcelProcesses = Get-Process -name "*Excel*" | % { $_.Id }
# your code here (probably looping through the Excel document in some way
# try to gently quit
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
# otherwise allow stop-process to clean up
$postExcelProcesses | ? { $priorExcelProcesses -eq $null -or $priorExcelProcesses -notcontains $_ } | % { Stop-Process -Id $_ }
My experience has been that the Quit method doesn't work well, especially when looping. When you get the error, instead of rebooting, open up Task Manager and look at the Processes tab. I'm willing to bet you'll see Excel still open -- maybe even multiple instances of it. I solved this problem by using Get-Process to find all instances of Excel and piping them to Stop-Process. Doesn't seem like that should be necessary, but it did the trick for me.
You should not have to keep track of processes and kill them off.
My experience has been that to properly and completely close Excel (including in loops), you also need to release COM references. In my own testing have found removing the variable for Excel also ensures no remaining references exist which will keep Excel.exe open (like if you are debugging in the ISE).
Without performing the above, if you look in Task Manager, you may see Excel still running...in some cases, many copies.
This has to do with how the COM object is wrapped in a “runtime callable wrapper".
Here is the skeleton code that should be used:
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook = $excel.Workbooks.Add()
# or $workbook = $excel.Workbooks.Open($xlsxPath)
# do work with Excel...
$workbook.SaveAs($xlsxPath)
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
# no $ needed on variable name in Remove-Variable call
Remove-Variable excel
Try this
$filePath = "E:\TSMBackup\TSMDATAPull\ODCLXTSM01_VM.xlsx"
$excelObj = New-Object -ComObject Excel.Application
$excelObj.Visible = $true
$workBook = $excelObj.Workbooks.Open($filePath)
$workSheet = $workBook.Sheets.Item("Sheet1")
$workSheet.Select()
$workBook.RefreshAll()
$workBook.Save()
$excelObj.Quit()