Anyone can help me fix my code below. I am displaying a log file in the textbox, but the textbox wont update automatically when the log file is updated. I tried using a timer but it wont work since it will refresh the whole form and the input file name will be reset too, so it will return a null value. Please go to "viewComo" function.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
#2. Instantiate a Form Object
#-----------------------------------------------------------------------------------------
$Form_MAIN = New-Object system.Windows.Forms.Form;
$Form_MAIN.ClientSize = New-Object System.Drawing.Point(1024,1000);
$Form_MAIN.StartPosition = "manual";
$Form_MAIN.Location = New-Object System.Drawing.Size(600,300);
$Form_MAIN.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#6699CC");
$Form_MAIN.text = "DataCenter Operation Applications";
$Form_MAIN.TopMost = $false;
$Form_MAIN.AutoSize = $false;
$Form_MAIN.AutoScale = $false;
$Form_MAIN.MaximizeBox = $false;
#3. Build the Form Components
#-----------------------------------------------------------------------------------------
$MainMenu = New-Object System.Windows.Forms.MenuStrip;
$Menu_File = New-Object System.Windows.Forms.ToolStripMenuItem("File");
$SubMenu_Open = New-Object System.Windows.Forms.ToolStripMenuItem("Open");
$SubMenu_Save = New-Object System.Windows.Forms.ToolStripMenuItem("Save");
$SubMenu_Exit = New-Object System.Windows.Forms.ToolStripMenuItem("Exit");
$Menu_DCApp = New-Object System.Windows.Forms.ToolStripMenuItem("DCApp");
$SubMenu_Reports_Checker = New-Object System.Windows.Forms.ToolStripMenuItem("Reports_Checker");
$SubMenu_Reports_Transfer = New-Object System.Windows.Forms.ToolStripMenuItem("Reports_Transfer");
$SubMenu_COB_Monitoring = New-Object System.Windows.Forms.ToolStripMenuItem("COB_Monitoring");
$SubMenu_AD_AccountLock_Checker = New-Object System.Windows.Forms.ToolStripMenuItem("AD_AccountLock_Checker");
$Menu_Help = New-Object System.Windows.Forms.ToolStripMenuItem("Help");
$Menu_About = New-Object System.Windows.Forms.ToolStripMenuItem("About");
$GB_Top = New-Object system.Windows.Forms.Groupbox;
$GB_Top.height = 330;
$GB_Top.width = 990;
$GB_Top.text = "Como Checker, Please input the Service name below";
$GB_Top.location = New-Object System.Drawing.Point(15,30);
$GB_Top.Font = New-Object System.Drawing.Font('Calibri',12);
$TB_Top_Input = New-Object system.Windows.Forms.TextBox;
$TB_Top_Input.multiline = $true;
$TB_Top_Input.width = 840;
$TB_Top_Input.height = 30;
$TB_Top_Input.location = New-Object System.Drawing.Point(10,25);
$TB_Top_Input.Font = New-Object System.Drawing.Font('Calibri',10);
$TB_Top_Button = New-Object system.Windows.Forms.Button;
$TB_Top_Button.Text = "Search COMO";
$TB_Top_Button.width = 124;
$TB_Top_Button.height = 30;
$TB_Top_Button.location = New-Object System.Drawing.Point(850,25);
$TB_Top_Button.Add_Click({checkComoFiles})
$RB_LabelQuery = New-Object System.Windows.Forms.Label;
$RB_LabelQuery.Text = "Choose the Range to query:";
$RB_LabelQuery.AutoSize = $true;
$RB_LabelQuery.width = 104;
$RB_LabelQuery.height = 10;
$RB_LabelQuery.location = New-Object System.Drawing.Point(10,55);
$RB_LabelQuery.Font = New-Object System.Drawing.Font('Callibri',9);
$RB_Option1 = New-Object system.Windows.Forms.RadioButton;
$RB_Option1.Text = "Last 1 Hour";
$RB_Option1.AutoSize = $true;
$RB_Option1.width = 104;
$RB_Option1.height = 10;
$RB_Option1.location = New-Object System.Drawing.Point(170,55);
$RB_Option1.Font = New-Object System.Drawing.Font('Callibri',9);
$RB_Option2 = New-Object system.Windows.Forms.RadioButton;
$RB_Option2.Text = "Last 3 Hours";
$RB_Option2.AutoSize = $true;
$RB_Option2.width = 104;
$RB_Option2.height = 10;
$RB_Option2.location = New-Object System.Drawing.Point(270,55);
$RB_Option2.Font = New-Object System.Drawing.Font('Callibri',9);
$RB_Option3 = New-Object system.Windows.Forms.RadioButton;
$RB_Option3.Text = "Last 24 Hours";
$RB_Option3.AutoSize = $true;
$RB_Option3.width = 104;
$RB_Option3.height = 10;
$RB_Option3.location = New-Object System.Drawing.Point(360,55);
$RB_Option3.Font = New-Object System.Drawing.Font('Callibri',9);
$RB_LabelOutput = New-Object System.Windows.Forms.Label;
$RB_LabelOutput.Text = "Result:";
$RB_LabelOutput.AutoSize = $true;
$RB_LabelOutput.width = 104;
$RB_LabelOutput.height = 10;
$RB_LabelOutput.location = New-Object System.Drawing.Point(10,85);
$RB_LabelOutput.Font = New-Object System.Drawing.Font('Calibri',12);
#$TB_Top_Output = New-Object system.Windows.Forms.TextBox;
#$TB_Top_Output.multiline = $true;
#$TB_Top_Output.width = 970;
#$TB_Top_Output.height = 200;
#$TB_Top_Output.location = New-Object System.Drawing.Point(10,110);
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size=New-Object System.Drawing.Size(970,200)
$dataGridView.Location = New-Object System.Drawing.Point(10,110);
$dataGridView.BackgroundColor = "White";
#$form.Controls.Add($dataGridView)
$dataGridView.ColumnCount = 1
$dataGridView.ColumnHeadersVisible = $true
$dataGridView.Columns[0].Name = "Como Name"
$datagridview.Columns[0].Width = 300;
$datagridview.Font = New-Object System.Drawing.Font('Courier New',9);
$datagridview.Add_CellMouseDoubleClick({viewComoGrid})
$GB_Bottom = New-Object system.Windows.Forms.Groupbox;
$GB_Bottom.height = 600;
$GB_Bottom.width = 990;
$GB_Bottom.text = "View COMO";
$GB_Bottom.location = New-Object System.Drawing.Point(15,380);
$GB_Bottom.Font = New-Object System.Drawing.Font('Calibri',12);
$TB_Bottom_Input = New-Object system.Windows.Forms.TextBox;
$TB_Bottom_Input.multiline = $true;
$TB_Bottom_Input.width = 840;
$TB_Bottom_Input.height = 30;
$TB_Bottom_Input.location = New-Object System.Drawing.Point(10,20);
$TB_Bottom_Input.Font = New-Object System.Drawing.Font('Calibri',10);
$TB_Bottom_Button = New-Object system.Windows.Forms.Button;
$TB_Bottom_Button.Text = "View COMO";
$TB_Bottom_Button.width = 124;
$TB_Bottom_Button.height = 30;
$TB_Bottom_Button.location = New-Object System.Drawing.Point(850,20);
$TB_Bottom_Button.Font = New-Object System.Drawing.Font('Calibri',10);
$TB_Bottom_Button.Add_Click({viewComo})
$RB_LabelOutput_Bottom = New-Object System.Windows.Forms.Label;
$RB_LabelOutput_Bottom.Text = "Result:";
$RB_LabelOutput_Bottom.AutoSize = $true;
$RB_LabelOutput_Bottom.width = 104;
$RB_LabelOutput_Bottom.height = 10;
$RB_LabelOutput_Bottom.location = New-Object System.Drawing.Point(10,55);
$RB_LabelOutput_Bottom.Font = New-Object System.Drawing.Font('Calibri',12);
$TB_Top_Output_Bottom = New-Object system.Windows.Forms.TextBox;
$TB_Top_Output_Bottom.multiline = $true;
$TB_Top_Output_Bottom.size = New-Object System.Drawing.Size(970,510)
$TB_Top_Output_Bottom.ReadOnly = $True
#$TB_Top_Output_Bottom.width = 970;
#$TB_Top_Output_Bottom.height = 510;
$TB_Top_Output_Bottom.location = New-Object System.Drawing.Size(10,80);
$TB_Top_Output_Bottom.ScrollBars = 'Both'
$TB_Top_Output_Bottom.Font = New-Object System.Drawing.Font('Courier New',9);
#4. Add the Components to the Group Boxes
#-----------------------------------------------------------------------------------------
$GB_Top.controls.AddRange(#($RB_LabelQuery, $RB_Option1,$RB_Option2,$RB_Option3,$TB_Top_Input, $TB_Top_Button, $RB_LabelOutput, $dataGridView));
$GB_Bottom.controls.AddRange(#($L_Bottom_Output,$B_Enter, $TB_Bottom_Input, $TB_Bottom_Button, $RB_LabelOutput_Bottom, $TB_Top_Output_Bottom ));
#4. Add the Components to the Form
#-----------------------------------------------------------------------------------------
$Menu_File.DropDownItems.Add($SubMenu_Open);
$Menu_File.DropDownItems.Add($SubMenu_Save);
$Menu_File.DropDownItems.Add($SubMenu_Exit);
$Menu_DCApp.DropDownItems.Add($SubMenu_Reports_Checker);
$Menu_DCApp.DropDownItems.Add($SubMenu_Reports_Transfer);
$Menu_DCApp.DropDownItems.Add($SubMenu_COB_Monitoring);
$Menu_DCApp.DropDownItems.Add($SubMenu_AD_AccountLock_Checker);
$MainMenu.Items.Add($Menu_File);
$MainMenu.Items.Add($Menu_DCApp);
$MainMenu.Items.Add($Menu_Help);
$MainMenu.Items.Add($Menu_About);
$Form_MAIN.controls.AddRange(#($MainMenu,$TB_Output));
#4.1 Add the Group Boxes to the Form
#-----------------------------------------------------------------------------------------
$Form_MAIN.controls.AddRange(#($GB_Top,$GB_Bottom));
#5. Code the Event Handlers
#-----------------------------------------------------------------------------------------
$SubMenu_Open.Add_Click({ $TB_Top_Output_Bottom.Text = "`r`n Open File."; })
$SubMenu_Save.Add_Click({ $TB_Top_Output_Bottom.Text = "`r`n Save File."; })
$SubMenu_Exit.Add_Click({ $TB_Top_Output_Bottom.Text = "`r`n Exit Program."; })
$SubMenu_Reports_Checker.Add_Click({ $TB_Top_Output_Bottom.Text = "`r`n Reports_Checker"; })
$SubMenu_Reports_Transfer.Add_Click({ $TB_Top_Output_Bottom.Text = "`r`n Reports_Transfer"; })
$SubMenu_COB_Monitoring.Add_Click({ $TB_Top_Output_Bottom.Text = "`r`n COB_Monitoring"; })
$SubMenu_AD_AccountLock_Checker.Add_Click({ $TB_Top_Output_Bottom.Text = "`r`n AD_AccountLock_Checker"; })
$Menu_DCApp.Add_Click({ $TB_Top_Output_Bottom.Text = "`r`n Options!"; })
$Menu_Help.Add_Click({ $TB_Top_Output_Bottom.Text = "`r`n Help me!"; })
$Menu_About.Add_Click({ $TB_Top_Output_Bottom.Text = "`r`n All about ME."; })
function checkComoFiles {
$servicename= $TB_Top_Input.Text
$dataGridView.Rows.Clear()
$TB_Top_Input.Clear()
if ($RB_Option1.Checked) {
$hours = 1
$ResultQuery = Get-ChildItem -Path C:\Users\user\Downloads\ -File | Where-Object {($_.LastWriteTime - gt (Get-Date (Get-Date).AddHours(-$hours))) } |
ForEach-Object { $_ | Select-String -List -Pattern $servicename -SimpleMatch |
Select-Object -first 1 -ExpandProperty FileName}
}
if ($RB_Option2.Checked) {
$hours = 3
$ResultQuery = Get-ChildItem -Path C:\Users\user\Downloads\ -File | Where-Object {($_.LastWriteTime -gt (Get-Date (Get-Date).AddHours(-$hours))) } |
ForEach-Object { $_ | Select-String -List -Pattern $servicename -SimpleMatch |
Select-Object -first 1 -ExpandProperty FileName}
}
if ($RB_Option3.Checked) {
$hours = 24
$ResultQuery = Get-ChildItem -Path C:\Users\user\Downloads\ -File | Where-Object {($_.LastWriteTime -gt (Get-Date (Get-Date).AddHours(-$hours))) } |
ForEach-Object { $_ | Select-String -List -Pattern $servicename -SimpleMatch |
Select-Object -first 1 -ExpandProperty FileName}
}
$dataGridView.Columns[0].Name = $servicename
$rows = #($ResultQuery)
foreach ($row in $rows)
{
$dataGridView.Rows.Add($row)
}
}
Function viewComo {
$comoName = $TB_Bottom_Input.Text
$openComolive = Get-Content C:\Users\user\Downloads\$comoName
$TB_Top_Output_Bottom.Lines = $openComolive
$TB_Bottom_Input.Clear()
}
Function viewComoGrid {
$rowIndex = $datagridview.CurrentRow.Index
$columnIndex = $datagridview.CurrentCell.ColumnIndex
#Write-Host $rowIndex
#Write-Host $columnIndex
#Write-Host $datagridview.Rows[$rowIndex].Cells[0].value
#Write-Host $datagridview.Rows[$rowIndex].Cells[$columnIndex].value
$comoNameGrid = $datagridview.Rows[$rowIndex].Cells[0].value
$openComoliveGrid = Get-Content C:\Users\user\Downloads\$comoNameGrid
$TB_Top_Output_Bottom.Lines = $openComoliveGrid
$TB_Bottom_Input.Clear()
}
#6. Display Form
#-----------------------------------------------------------------------------------------
$Form_Main.ShowDialog();
Im trying to use below timer in my above code but it wont work since the whole form will be refresh
$timer = new-object Windows.Forms.Timer
$timer.Interval=10000
$timer.add_Tick({$TB_Top_Output_Bottom.Lines = Get-Content C:\Users\user\Downloads\$logname | Out-String; $TB_Top_Output_Bottom.Refresh()})
$timer.Start()
Original Version:
This isn't exactly intended to be an answer!
I think mklement0's answer is probably a safer way of getting the job done. But I remember reading about FileSystemWatcher some years ago, and having never used it before, wanted to give it a try.
Found this C# FileSystemWatcher answer, and figured out how to recreate the work in PowerShell.
Found this interesting use of SynchronizingObject for System.Timers.Timer that appears to allow Timer's Elapsed event to to run in the same thread as a control or form, and discovered that FileSystemWatcher also has a SynchronizingObject Property.
This seems to work flawlessly when editing and saving MyLogFile.TXT with NotePad.exe, but when I load MyLogFile.TXT in VSCode, the script either crashes or stops working. I think VSCode is locking the file and preventing the script from reading it, but I'm not really sure.
I would like to stress that this is an experiment, and this is outside my experience, use with with caution.
Basic code for setting up a Form for testing FileSystemWatcher:
using namespace System.Windows.Forms
Add-Type -AssemblyName System.Windows.Forms
[Application]::EnableVisualStyles()
$FilePathToWatch = $PSScriptRoot
$FileNameToWatch = "MyLogFile.TXT"
$FilePathNameToWatch = Join-Path -Path $FilePathToWatch -ChildPath $FileNameToWatch
$Form_MAIN = [Form]#{
AutoSize = $false
AutoScale = $false
BackColor = '0x6699CC'
ClientSize = "1024,1000"
Location = "600,300"
MaximizeBox = $false
StartPosition = "manual"
Text = "DataCenter Operation Applications"
Topmost = $true
}
$TextBox_Output = [TextBox]#{
Anchor = 'Top, Left, Bottom, Right'
Location = '12, 12'
Multiline = $true
Name = 'TextBox_Output'
Size = "$($Form_MAIN.ClientSize.Width - 24), $($Form_MAIN.ClientSize.Height - 24)"
Text = Get-Content -Raw $FilePathNameToWatch
}
$Form_Main.Controls.Add($TextBox_Output)
Code for setting up FileSystemWatcher, ending with ShowDialog() to open the form, $watcher.Dispose() to, as mklement0 pointed out, to stop $watcher from continuing to fire after form closes.:
[IO.FileSystemWatcher]$watcher = [IO.FileSystemWatcher]#{
Path = $FilePathToWatch
NotifyFilter = [IO.NotifyFilters]::LastWrite
Filter = $FileNameToWatch
SynchronizingObject = $TextBox_Output
}
$watcher.Add_Changed({
$TextBox_Output.Text = Get-Content -Raw $FilePathNameToWatch
})
$watcher.EnableRaisingEvents = $true
$null = $Form_Main.ShowDialog()
$watcher.Dispose()
UPDATED Version:
Walk through of changes from above code, full code listed in order in following sections:
Add function GetLogFileContent for safely reading the file, or returning an empty string when the file doesn't exist.
using namespace System.Windows.Forms
Add-Type -AssemblyName System.Windows.Forms
[Application]::EnableVisualStyles()
$FilePathToWatch = $PSScriptRoot
$FileNameToWatch = "MyLogFile.TXT"
$FilePathNameToWatch = Join-Path -Path $FilePathToWatch -ChildPath $FileNameToWatch
function GetLogFileContent {
param ( [Parameter(Mandatory = $true, Position = 0)][string]$FilePathName )
if(Test-Path -PathType Leaf -LiteralPath $FilePathName) { Get-Content -Raw $FilePathName } else { '' }
}
Added code for Form's FormClosing event to shutdown $watcher so it no longer is firing events and properly disposed.
$Form_MAIN = [Form]#{
AutoSize = $false
AutoScale = $false
BackColor = '0x6699CC'
ClientSize = "1024,1000"
Location = "600,300"
MaximizeBox = $false
StartPosition = "manual"
Text = "DataCenter Operation Applications"
Topmost = $true
}
$Form_MAIN.Add_FormClosing({
$watcher.Dispose()
})
The TextBox Text property is assigned results of call to GetLogFileContent function.
$TextBox_Output = [TextBox]#{
Anchor = 'Top, Left, Bottom, Right'
Location = '12, 12'
Multiline = $true
Name = 'TextBox_Output'
Size = "$($Form_MAIN.ClientSize.Width - 24), $($Form_MAIN.ClientSize.Height - 24)"
Text = GetLogFileContent $FilePathNameToWatch
}
$Form_Main.Controls.Add($TextBox_Output)
$watcher's NotifyFilter property now set for checking for both FileName and LastWrite. Just a reminder, SynchronizingObject set to the TextBox so it can be updated on the same thread.
[IO.FileSystemWatcher]$watcher = [IO.FileSystemWatcher]#{
Path = $FilePathToWatch
NotifyFilter = [IO.NotifyFilters]::FileName -bor [IO.NotifyFilters]::LastWrite
Filter = $FileNameToWatch
SynchronizingObject = $TextBox_Output
}
Added Deleted and Renamed events to $watcher to catch deletions and renames, and using GetLogFileContent function to populate the textbox Text property.
$watcher.Add_Changed({
$TextBox_Output.Text = GetLogFileContent $FilePathNameToWatch
})
$watcher.Add_Deleted({
$TextBox_Output.Text = ''
})
$watcher.Add_Renamed({
$TextBox_Output.Text = if($_.Name -eq $FileNameToWatch) { GetLogFileContent $FilePathNameToWatch } else { '' }
})
Watcher.Dispose() removed from end of code (taken care of in FormClosing event).
$watcher.EnableRaisingEvents = $true
$null = $Form_Main.ShowDialog()
Note:
This answer addresses the question as asked.
Darin's helpful answer shows an alternative approach that doesn't unconditionally, periodically re-read the log file (based on a timer), but uses an event-based file-system watcher to only update re-read the file if and when it changes.
This is more elegant and generally preferable, except if the log file is updated very frequently, in which case you'd need a throttling mechanism (which the timer-based approach implicitly provides, though one could be implemented with the file-watcher approach too).
There is no obvious problem with your approach:
The System.Windows.Forms.Timer type fires its events on the GUI thread and therefore allows modifying the form state.
While script blocks ({ ... }) that are passed as event delegates in .add_<eventName>() calls run in a child scope of the script, thanks to PowerShell's dynamic scoping you can still read the variables from the script scope.
Since WinForms is in control of the event loop when you show a form modally with .ShowDialog(), it is sufficient to assign new content to the .Lines property of your text-box control: the control should refresh automatically (and even an explicit .Refresh() call should not make the whole form refresh).
The following is a self-contained proof of concept:
A background job is started that writes the current timestamp to a given file every second.
The WinForms code uses a timer event to read that file and update its multi-line text box control with it.
using namespace System.Windows.Forms
using namespace System.Drawing
Add-Type -AssemblyName System.Windows.Forms
# Create the form.
$form = [Form] #{
Text = "Textbox Timer-Based Refresh Demo"
Size = [Size]::new(380,200)
StartPosition = "CenterScreen"
}
# Create the textbox and add it to the form.
$form.Controls.AddRange(#(
($textBox = [TextBox] #{
Location = [Point]::new(10, 10)
Size = [Size]::new(320, 90)
MultiLine = $true
})
))
# Create a timer that fires every second, and
# reads the then-current file content.
$timer = [Timer]::new()
$timer.InterVal = 1000
$timer.add_Tick({
$textBox.Lines = Get-Content -Raw $logName
})
$timer.Start()
# The log file to read.
$logName = 't.txt'
# Create a background job that updates the log file every second.
$jb = Start-Job {
while ($true) {
Get-Date > "$using:PWD/$using:logName"
Start-Sleep 1
}
}
# Show the form modally.
$null = $form.ShowDialog()
# Clean up
$timer.Dispose()
$jb | Remove-Job -Force
As stated in the Title, I would like to add an image into a Word document. Though my goal is to NOT use a path (stating where the image is located).
**Like this: **
$word = New-Object -ComObject Word.Application
$word.visible = $true
$document = $word.documents.Add()
$selection = $word.selection
$newInlineShape = $selection.InlineShapes.AddPicture($path)
But rather using some type of Base64String, so that this skript works with every device, regardless if the image path doesn't exist.
My attempt:
$word = New-Object -ComObject Word.Application
$word.visible = $true
$document = $word.documents.Add()
$selection = $word.selection
$base64ImageString = [Convert]::ToBase64String((Get-Content $path -encoding byte))
$imageBytes = [Convert]::FromBase64String($base64ImageString)
$ms = New-Object IO.MemoryStream($imageBytes, 0, $imageBytes.Length)
$ms.Write($imageBytes, 0, $imageBytes.Length);
$alkanelogo = [System.Drawing.Image]::FromStream($ms, $true)
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width = $alkanelogo.Size.Width;
$pictureBox.Height = $alkanelogo.Size.Height;
$pictureBox.Location = New-Object System.Drawing.Size(153,223)
$pictureBox.Image = $alkanelogo;
$newInlineShape = $selection.InlineShapes.AddPicture($pictureBox.Image)
Note: The variable "$path" is only here as a placeholder
I've figured it out. I downloaded the image to my local computer, converted it to a base64 string and then back to an image.
So that this script works with every user regardless of there path, I built in it to download the file to a specific path (that I created).
Powershell will then extract the image from the path I created.
$filepath = 'C:\temp\image.png'
$folderpath = 'C:\temp\'
if([System.IO.File]::Exists($filepath -or $folderpath)){
rmdir 'C:\temp\image.png'
$b64 = "AAA..."
$bytes = [Convert]::FromBase64String($b64)
[IO.File]::WriteAllBytes($filepath, $bytes)
}else{
mkdir 'C:\temp\' -ErrorAction SilentlyContinue
$b64 = "AAA..."
$bytes = [Convert]::FromBase64String($b64)
[IO.File]::WriteAllBytes($filepath, $bytes)
}
I have the below script that was posted here: Split Excelfile .xlxs with Powershell based on column values and it works as described but I am experiencing an issue.
The script will check one xlsx file and sort the content by unique values in column A and then copy these data set and create a new file.
The issue I am having is out of about 4000 files, 20 of them are created with the full data set from the original file. The rest of the files a split and created correctly. Not sure as to why this is occurring. Any help is appreciated.
Function Create-Excel-Spreadsheet {
Param($NameOfSpreadsheet)
# open excel
$excel = New-Object -ComObject excel.application
$excel.visible = $true
# add a worksheet
$workbook = $excel.Workbooks.Add()
$xl_wksht= $workbook.Worksheets.Item(1)
$xl_wksht.Name = $NameOfSpreadsheet
return $workbook
}
$objexcel = New-Object -ComObject Excel.Application
$wb = $objexcel.WorkBooks.Open("C:\Temp\Test.xlsx") # Changing path for test.xlsx file.
$objexcel.Visible = $true
$objexcel.DisplayAlerts = $False
$ws = $wb.Worksheets.Item(1)
$usedRange = $ws.UsedRange
$usedRange.AutoFilter()
$totalRows = $usedRange.Rows.Count
$rangeForUnique = $usedRange.Offset(1, 0).Resize($UsedRange.Rows.Count-1)
[string[]]$UniqueListOfRowValues = $rangeForUnique.Columns.Item(1).Value2 | sort -Unique
for ($i = 0; $i -lt $UniqueListOfRowValues.Count; $i++) {
$newRange = $usedRange.AutoFilter(1, $UniqueListOfRowValues[$i])
$workbook = Create-Excel-Spreadsheet $UniqueListOfRowValues[$i]
$wksheet = $workbook.Worksheets.Item(1)
$range = $ws.UsedRange.Cells
$range.Copy()
$wksheet.Paste($wksheet.Range("A1"))
$workbook.SaveAs("C:\temp\" + $UniqueListOfRowValues[$i], $xlFixedFormat)
$workbook.Close()
}
I want to decide which folder that I need to choose based on my data, then if I can't find it, it will show the GUI for waiting and do looping to check it. I try this code, I can find the folder, but when I can't find it once I want to show the GUI it returns some error.
This is how I checking the folder
function FIND {
Write-Host "call the function that can call the GUI.ps1 script"
$Path = "D:\Process"
Write-Host "Starting Mapping SSID and Finding Job"
$SSID_Unit = "111dddddfafafesa"
Try{
$Path_Job = (Get-Item (Get-ChildItem "$Path\*\SSID_LST" | Select-String -Pattern "$SSID_Unit").Path).Directory.FullName
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $Path_Job.FullName
Attempts = 1
}
Write-Host "Job'$($global:Result.FileName)' Exists. Found after $($global:Result.Attempts) attempts." -ForegroundColor Green
Write-Host "Continue to Assigned Job"
Pause
} Catch {
Write-Host "Waiting for the jobss"
& D:\X\Wait_GUI.ps1 -Path $Path_Job -MaxAttempts 20
Write-Host "Job not found after $($global:Result.Attempts) attempts." -ForegroundColor Red
}
}
FIND
This is the GUI
Param (
[string]$Path = '*.*',
[string]$MaxAttempts = 5
)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# set things up for the timer
$script:nAttempts = 0
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000 # 1 second
$timer.Add_Tick({
$global:Result = $null
$script:nAttempts++
$Path_Job = Get-Item -Path $Path
if ($Path_Job) {
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $Path_Job.FullName
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
elseif ($script:nAttempts -ge $MaxAttempts) {
$global:Result = [PSCustomObject]#{
Exists = $false
FileName = ''
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
})
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '617,418'
$Form.text = "AutoGM"
$Form.BackColor = "#8b572a"
$Form.TopMost = $false
$Form.WindowState = 'Maximized'
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "UNDER AUTOMATION PROCESS"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.Anchor = 'top,right,bottom,left'
$Label1.ForeColor = "#ffffff"
$Label1.Anchor = "None"
$Label1.TextAlign = "MiddleCenter"
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "Waiting for the job..."
$Label2.AutoSize = $true
$Label2.width = 25
$Label2.height = 10
$Label2.ForeColor = "#ffffff"
$Label2.Anchor = "None"
$Label2.TextAlign = "MiddleCenter"
$Form.controls.AddRange(#($Label1,$Label2))
[void]$Form.Show()
# Write-Host $Form.Height
# Write-Host $Form.Width
$Label1.location = New-Object System.Drawing.Point(($Form.Width*0.35), ($Form.Height*0.4))
$Label2.location = New-Object System.Drawing.Point(($form.Width*0.43), ($Form.Height*0.5))
$L_S = (($Form.Width/2) - ($Form.Height / 2)) / 15
$Label1.Font = "Microsoft Sans Serif, $L_S, style=Bold"
$Label2.Font = "Microsoft Sans Serif, $L_S, style=Bold"
$Form.controls.AddRange(#($Label1,$Label2))
# start the timer as soon as the dialog is visible
$Form.Add_Shown({ $timer.Start() })
$Form.Visible = $false
[void]$Form.ShowDialog()
# clean up when done
$Form.Dispose()
if not found, it return this
Waiting for the jobss
Job not found after 1 attempts.
and the GUI is not shown
Ok, first of all, you are using different tests for the file and/or directory in the code and in the GUI. Furthermore, you call the GUI.ps1 file with a path set to a $null value.
I would change your code to something like this:
$Path = "D:\Process\*\SSID_LST\*" # the path to look for files
$SSID_Unit = "111dddddfafafesa" # the Search pattern to look for inside the files
function Test-FileWithGui {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$Path,
[Parameter(Mandatory = $true, Position = 2)]
[string]$Pattern,
[int]$MaxAttempts = 5
)
Write-Host "Starting Mapping SSID and Finding Job"
# set up an 'empty' $global:Result object to return on failure
$global:Result = '' | Select-Object #{Name = 'Exists'; Expression = {$false}}, FileName, Directory, #{Name = 'Attempts'; Expression = {1}}
# test if the given path is valid. If not, exit the function
if (!(Test-Path -Path $Path -PathType Container)) {
Write-Warning "Path '$Path' does not exist."
return
}
# try and find the first file that contains your search pattern
$file = Select-String -Path $Path -Pattern $Pattern -SimpleMatch -ErrorAction SilentlyContinue | Select-Object -First 1
if ($file) {
$file = Get-Item -Path $file.Path
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $file.FullName
Directory = $file.DirectoryName
Attempts = 1
}
}
else {
& "D:\GUI.ps1" -Path $Path -Pattern $Pattern -MaxAttempts $MaxAttempts
}
}
# call the function that can call the GUI.ps1 script
Test-FileWithGui -Path $Path -Pattern $SSID_Unit -MaxAttempts 20
# show the $global:Result object with all properties
$global:Result | Format-List
# check the Global result object
if ($global:Result.Exists) {
Write-Host "File '$($global:Result.FileName)' Exists. Found after $($global:Result.Attempts) attempts." -ForegroundColor Green
}
else {
Write-Host "File not found after $($global:Result.Attempts) attempts." -ForegroundColor Red
}
Next the GUI file.
As you are now searching for a file that contains some text, you need a third parameter to call this named Pattern.
Inside the GUI file we perform the exact same test as we did in the code above, using the parameter $Pattern as search string:
Param (
[string]$Path,
[string]$Pattern,
[int]$MaxAttempts = 5
)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# set things up for the timer
$script:nAttempts = 0
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000 # 1 second
$timer.Add_Tick({
$global:Result = $null
$script:nAttempts++
# use the same test as you did outside of the GUI
# try and find the first file that contains your search pattern
$file = Select-String -Path $Path -Pattern $Pattern -SimpleMatch -ErrorAction SilentlyContinue | Select-Object -First 1
if ($file) {
$file = Get-Item -Path $file.Path
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $file.FullName
Directory = $file.DirectoryName
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
elseif ($script:nAttempts -ge $MaxAttempts) {
$global:Result = [PSCustomObject]#{
Exists = $false
FileName = $null
Directory = $null
Attempts = $script:nAttempts
}
$script:nAttempts = 0
$timer.Dispose()
$Form.Close()
}
})
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '617,418'
$Form.Text = "AutoGM"
$Form.BackColor = "#8b572a"
$Form.TopMost = $true
$Form.WindowState = 'Maximized'
# I have removed $Label2 because it is easier to use
# just one label here and Dock it to Fill.
$Label1 = New-Object system.Windows.Forms.Label
$Label1.Text = "UNDER AUTOMATION PROCESS`r`n`r`nWaiting for the job..."
$Label1.AutoSize = $false
$Label1.Dock = 'Fill'
$Label1.TextAlign = "MiddleCenter"
$Label1.ForeColor = "#ffffff"
$L_S = (($Form.Width/2) - ($Form.Height / 2)) / 10
$Label1.Font = "Microsoft Sans Serif, $L_S, style=Bold"
$Form.controls.Add($Label1)
# start the timer as soon as the dialog is visible
$Form.Add_Shown({ $timer.Start() })
[void]$Form.ShowDialog()
# clean up when done
$Form.Dispose()
The results during testing came out like below
If the file was found within the set MaxAttempts tries:
Starting Mapping SSID and Finding Job
Exists : True
FileName : D:\Process\test\SSID_LST\blah.txt
Directory : D:\Process\test\SSID_LST
Attempts : 7
File 'D:\Process\test\SSID_LST\blah.txt' Exists. Found after 7 attempts.
When the file was NOT found:
Starting Mapping SSID and Finding Job
Exists : False
FileName :
Directory :
Attempts : 20
File not found after 20 attempts.
If even the folder $Path was not found, the output is
Starting Mapping SSID and Finding Job
WARNING: Path 'D:\Process\*\SSID_LST\*' does not exist.
Exists : False
FileName :
Directory :
Attempts : 1
File not found after 1 attempts.
Hope that helps
I have two sheets on my excel spread sheet. The script is placing the info in the proper sheets but it is putting blanks in the sheet. If Computer1 is placed in the first sheet and the Computer2 is in the second sheet. The first cell in the second sheet will be blank and the Computer2 entry will be in the second second row instead of the first. How can I stop this from happening? It is creating a ghost cell for the one that is in the first sheet.
Here is my code:
$erroractionpreference = "SilentlyContinue"
$date = 0
$date = get-date -format "yyyy-MMM-dd-hhmm"
$date
$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
# Create 3 worksheets
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Add()
# Assign each worksheet to a variable
$Sheet1 = $Excel.Worksheets.Item(1)
$Sheet2 = $Excel.WorkSheets.Item(2)
# name the worksheet.
$Sheet1.Name = "Machines with PerfStat"
$Sheet2.Name = "No PerfStat"
#Create Heading for General Sheet
$Sheet1.Cells.Item(1, 1) = "Machine_Name"
$Sheet1.Cells.Item(1, 2) = "PerfStat Installed"
$Sheet2.Cells.Item(1, 1) = "Machine_Name"
$Sheet2.Cells.Item(1, 2) = "PerfStat_Installed"
#Create Heading for No PerfStat
$colSheets = ($Sheet1, $Sheet2)
#Auto Fit all sheets in the Workbook
foreach ($colorItem in $colSheets)
{
$WorkBook = $colorItem.UsedRange
$WorkBook.EntireColumn.AutoFit()
clear
}
foreach ($colorItem in $colSheets)
{
$intRow = 2
$WorkBook = $colorItem.UsedRange
$WorkBook.Interior.ColorIndex = 20
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True
}
$Computers = Get-Content -Path C:\temp\servers.txt
foreach ($Server in $computers)
{
remove-variable reply
$reply = Invoke-WebRequest http://$Server":1055" | Select-Object Content
if ($reply.Content -like "*Windows PerfStat v1.1.4*")
{
$Sheet1.Cells.Item($intRow, 1) = $server
$Sheet1.Cells.Item($intRow, 2) = "Yes"
}
else
{
$Sheet2.Cells.Item($intRow, 1) = $server
$Sheet2.Cells.Item($intRow, 2) = "No"
}
$intRow = $intRow + 1
}
$outputfile = "c:\temp\" + $date.toString() + "-PerfStat"
$Excel.SaveAs($outputfile)
$Excel.Close()
I have figured it out! I placed this code at the bottom of the script and it sorts both sheets before creating the file and saving it.
$Range1 = $Sheet1.range('A2:D2500')
$Range2 = $sheet1.Range('B2')
$Range1.Sort($Range2, 1)
$Range.entirecolumn.autofit()
$Range1 = $Sheet2.range('A2:D2500')
$Range2 = $sheet2.Range('B2')
$Range1.Sort($Range2, 1)
$Range.entirecolumn.autofit()