I've been struggling to display either OK.png / NOK.png on a specific column by testing if the path of the actual PV.pdf exists ,
$dataGrid = New-Object System.Windows.Forms.DataGridView
$dataGrid.Width = 503
$dataGrid.Height = 250
$dataGrid.location = new-object system.drawing.point(120,380)
$dataGrid.DataSource = $DataTable
$dataGrid.ReadOnly = $true
$dataGrid.RowHeadersVisible = $false
$dataGrid.AllowUserToAddRows = $false
$dataGrid.AutoSizeColumnsMode = 'Fill'
$ImageColumn = New-Object System.Windows.Forms.DataGridViewImageColumn
$ImageColumn.Width = 40
$dataGrid.Columns.Insert($dataGrid.Columns.Count, $ImageColumn)
$dataGrid.Columns[$dataGrid.Columns.Count - 1].HeaderText = "PV"
$dataGrid.Columns[$dataGrid.Columns.Count - 1].Name = "ImageColumn"
$OKImage = [System.Drawing.Image]::FromFile([string]$curDir+"\images\OK.png")
$NOKImage = [System.Drawing.Image]::FromFile([string]$curDir+"\images\NOK.png")
for($i=0;$i -lt $dataGrid.RowCount;$i++) {
Write-Host $dataGrid.Rows[$i].Cells[1].Value
$dir = [string]$curDir+"\pvs\"+[string]$dataGrid.Rows[$i].Cells[1].Value+".pdf"
if ((Test-Path $dir) -eq $true) {
$dataGrid.Rows[$i].Cells[0].Value = $OKImage
} else {
$dataGrid.Rows[$i].Cells[0].Value = $NOKImage
}
}
$dataGrid.Refresh()
i get this when i run the script :
Screenshot
Please help
i was expecting to display the OK.png (20x20) when the file is found and NOK.png when the file is not found .
I wrote these code for upload files through laravel 6.
But I only success for upload one file,even I am sure I got all files while uploading by check from dd($request->files)
in the begining the $index = 0 and in the end it change to 1,first round is ok but the it didn't continue to second run.
I don't know why,please help! thank you~
if($request->hasFile('files')){
$index = 0;
foreach($request->files as $key=>$file){
$originalName = $file[$index]->getClientOriginalName();
$size = $file[$index]->getClientSize();
$ext = $file[$index]->getClientOriginalExtension();
$newName = date('Ymd').mt_rand(100,999).$originalName;
$savePath = '/attachments/'.$newName;
$movePath = base_path().'/public/attachments/'.$newName;
$file[$index]->move(base_path().'/public/attachments',$newName);
$attachment = new attachment();
$attachment->bulletin_id = $bulletin->id; //already got this value before
$attachment->original_name = $originalName;
$attachment->name = $newName;
$attachment->type = $file[$index]->getClientMimeType();
$attachment->path = $savePath;
$attachment->size = ($size/1000);
$attachment->save();
++$index;
}
}
You try this way and it is simple
foreach($request->file('files') as $key=>$file){
$originalName = $file->getClientOriginalName();
$size = $file->getClientSize();
$ext = $file->getClientOriginalExtension();
$newName = date('Ymd').mt_rand(100,999).$originalName;
$savePath = '/attachments/'.$newName;
$movePath = base_path().'/public/attachments/'.$newName;
$file->move(base_path().'/public/attachments',$newName);
$attachment = new attachment();
$attachment->bulletin_id = $bulletin->id; //already got this value before
$attachment->original_name = $originalName;
$attachment->name = $newName;
$attachment->type = $file->getClientMimeType();
$attachment->path = $savePath;
$attachment->size = ($size/1000);
$attachment->save();
}
Attempting to download an image from a URL into memory. Getting an error on the response stream.
Exception calling "FromStream" with "1" argument(s): "Value of 'null' is not valid for 'stream'."
Function Download-Image {
$req = [System.Net.WebRequest]::Create
("http://www.wired.com/images_blogs/business/2012/12/google.jpg")
$response = $req.GetResponse()
$ResponseStream = $Response.GetResponseStream()
[System.IO.Stream]$stream = $ResponseStream
#$response.close()
}
[byte[]]$image = Download-Image
Add-Type -AssemblyName System.Windows.Forms
$img = [System.Drawing.Image]::FromStream([System.IO.MemoryStream]$image)
[System.Windows.Forms.Application]::EnableVisualStyles()
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width = $img.Width
$pictureBox.Height = $img.Height
$pictureBox.Image = $img
$form = new-object Windows.Forms.Form
$form.Width = $img.Width
$form.Height = $img.Height
$form.AutoSize = $True
$form.AutoSizeMode = "GrowAndShrink"
$form.Icon = $icon
$form.controls.add($pictureBox)
$form.Add_Shown( { $form.Activate() } )
$form.ShowDialog()
You need to return value from function after assignment:
Function Download-Image {
$req = [System.Net.WebRequest]::Create
("http://www.wired.com/images_blogs/business/2012/12/google.jpg")
$response = $req.GetResponse()
$ResponseStream = $Response.GetResponseStream()
[System.IO.Stream]$stream = $ResponseStream
#$response.close()
$req
}
In your case $image variable just have a value of $null:
[byte[]]$image = Download-Image
Updated:
To download image as byte array you can use solution from this post:
var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData("http://www.google.com/images/logos/ps_logo2.png");
I am trying to get some dynamic array values into a PowerShell windows Form. When I use the same command on the PowerShell console it works but in form I get a weird .dot net class returned. Below I mark the code that results in wrong output.
Import-Module NetAdapter
$nics = Get-NetAdapter | Select-Object name, interfacedescription,macaddress
function GenerateForm
{
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$frmVMTask = New-Object System.Windows.Forms.Form
$groupBox1 = New-Object System.Windows.Forms.GroupBox
$btnCancel = New-Object System.Windows.Forms.Button
# $btnOK = New-Object System.Windows.Forms.Button
$ChkVMTask = New-Object System.Windows.Forms.CheckedListBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$handler_ChkVMTask_itemCheckChanged =
{
$VMTask = $ChkVMTask.CheckedItems
If ($ChkVMTask.CheckedItems.count -gt 1)
{
$buttons = [system.windows.forms.messageboxbuttons]::ok;
[System.Windows.Forms.MessageBoxDefaultButton]::Button1;
$icon = [system.windows.forms.messageboxicon]::error;
$tasks = [System.Windows.Forms.MessageBox]::Show("
############################
# Too Many VM Tasks are selected. #
#############################`n
Please Select 1 item only.
"
, "VM Task Error Summary", $buttons, $icon)
}
else
{
$VMTask = $ChkVMTask.CheckedItems
}
}
{
# Close the form if Cancel clicked
$frmVMTask.close()
}
$groupBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 16
$System_Drawing_Point.Y = 16
$groupBox1.Location = $System_Drawing_Point
$groupBox1.Name = "groupBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 940
$System_Drawing_Size.Width = 800
$groupBox1.Size = $System_Drawing_Size
$groupBox1.TabIndex = 0
$groupBox1.TabStop = $False
$groupBox1.Text = "NIC Automation Task"
$groupBox1.add_Enter($handler_groupBox1_Enter)
$frmVMTask.Controls.Add($groupBox1)
$btnCancel.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 280
$System_Drawing_Point.Y = 430
$btnCancel.Location = $System_Drawing_Point
$btnCancel.Name = "btnCancel"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 75
$btnCancel.Size = $System_Drawing_Size
$btnCancel.TabIndex = 15
$btnCancel.Text = "Cancel"
$btnCancel.UseVisualStyleBackColor = $True
$btnCancel.add_Click($handler_btnCancel_Click)
$groupBox1.Controls.Add($btnCancel)
$groupBox1.Controls.Add($lblTask)
$ChkVMTask.[Windows.Forms.SelectionMode]::One;
$ChkVMTask.CheckOnClick = $True;
$ChkVMTask.DataBindings.DefaultDataSourceUpdateMode = 0
$ChkVMTask.FormattingEnabled = $True
foreach ($nic in $nics)
{
Below is where output is code and not same as get-netadapter
$ChkVMTask.Items.Add("$Nic") | Out-Null
}
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 20
$System_Drawing_Point.Y = 54
$ChkVMTask.Location = $System_Drawing_Point
$ChkVMTask.Name = "ChkVMTask"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 158
$System_Drawing_Size.Width = 722
$ChkVMTask.Size = $System_Drawing_Size
$ChkVMTask.TabIndex = 1
$ChkVMTask.Tag = ""
$ChkVMTask.add_SelectedIndexChanged($handler_ChkVMTask_itemCheckChanged)
$groupBox1.Controls.Add($ChkVMTask)
#Save the initial state of the form
$InitialFormWindowState = $frmVMTask.WindowState
#Init the OnLoad event to correct the initial state of the form
$frmVMTask.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$frmVMTask.ShowDialog() | Out-Null
$frmVMTask.close() #new parent test close visible and hidden work perfectly. close not yet.
} #End Function
#Call the Function
GenerateForm
I want to show status information after a VBS based login script runs. I could simply use a Messagebox, but would rather have this information appear above the clock briefly after the user logs in.
What alternatives do I have instead of a Messagebox?
What you are referring to is something like a balloon tip. There is nothing like this available in VBScript natively. You would need to use a third-party component.
As an aside, PowerShell IS able to do this.
[reflection.assembly]::loadwithpartialname("System.Windows.Forms")
[reflection.assembly]::loadwithpartialname("System.Drawing")
$notify = new-object system.windows.forms.notifyicon
$notify.icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(10,"Reboot Required","Please restart your computer",[system.windows.forms.tooltipicon]::None)
If you can go pure powershell I highly recommend this snippet works great!
Example
Code
##################
# Messenger like popup dialog
# Usage:
# New-Popup.ps1
# New-Popup.ps1 -slide -message "hello world" -title "PowerShell Popup"
param(
[int]$formWidth=200,
[int]$formHeight=110,
[string]$title="Your title here",
[string]$message="Your message here",
[int]$wait=4,
[switch]$slide
)
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
################
# extract powershell icon if doesn't exist
$icon = "$env:temp\posh.ico"
if( !(test-path -pathType leaf $icon)){
[System.Drawing.Icon]::ExtractAssociatedIcon((get-process -id $pid).path).ToBitmap().Save($icon)
}
################
# Create the form
$form = new-object System.Windows.Forms.Form
$form.ClientSize = new-object System.Drawing.Size($formWidth,$formHeight)
$form.BackColor = [System.Drawing.Color]::LightBlue
$form.ControlBox = $false
$form.ShowInTaskbar = $false
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$form.topMost=$true
# initial form position
$screen = [System.Windows.Forms.Screen]::PrimaryScreen
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
if($slide){
$top = $screen.WorkingArea.height + $form.height
$left = $screen.WorkingArea.width - $form.width
$form.Location = new-object System.Drawing.Point($left,$top)
} else {
$top = $screen.WorkingArea.height - $form.height
$left = $screen.WorkingArea.width - $form.width
$form.Location = new-object System.Drawing.Point($left,$top)
}
################
# pictureBox for icon
$pictureBox = new-object System.Windows.Forms.PictureBox
$pictureBox.Location = new-object System.Drawing.Point(2,2)
$pictureBox.Size = new-object System.Drawing.Size(20,20)
$pictureBox.TabStop = $false
$pictureBox.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::StretchImage
$pictureBox.Load($icon)
################
# create textbox to display the message
$textbox = new-object System.Windows.Forms.TextBox
$textbox.Text = $message
$textbox.BackColor = $form.BackColor
$textbox.Location = new-object System.Drawing.Point(4,26)
$textbox.Multiline = $true
$textbox.TabStop = $false
$textbox.BorderStyle = [System.Windows.Forms.BorderStyle]::None
$textbox.Size = new-object System.Drawing.Size(192,77)
$textbox.Cursor = [System.Windows.Forms.Cursors]::Default
$textbox.HideSelection = $false
################
# Create 'Close' button, when clicked hide and dispose the form
$button = new-object system.windows.forms.button
$button.Font = new-object System.Drawing.Font("Webdings",5)
$button.Location = new-object System.Drawing.Point(182,3)
$button.Size = new-object System.Drawing.Size(16,16)
$button.Text = [char]114
$button.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$button.Add_Click({ $form.hide(); $form.dispose() })
if($slide) {$button.visible=$false}
################
# Create a label, for title text
$label = new-object System.Windows.Forms.Label
$label.Font = new-object System.Drawing.Font("Microsoft Sans Serif",8,[System.Drawing.FontStyle]::Bold)
$label.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
$label.Text = $title
$label.Location = new-object System.Drawing.Point(24,3)
$label.Size = new-object System.Drawing.Size(174, 20)
################
# Create a timer to slide the form
$timer = new-object System.Windows.Forms.Timer
$timer.Enabled=$false
$timer.Interval=10
$timer.tag="up"
$timer.add_tick({
if(!$slide){return}
if($timer.tag -eq "up"){
$timer.enabled=$true
$form.top-=2
if($form.top -le ($screen.WorkingArea.height - $form.height)){
#$timer.enabled=$false
$timer.tag="down"
start-sleep $wait
}
} else {
$form.top+=2
if($form.top -eq ($screen.WorkingArea.height + $form.height)){
$timer.enabled=$false
$form.dispose()
}
}
})
# add form event handlers
$form.add_shown({
$form.Activate()
(new-Object System.Media.SoundPlayer "$env:windir\Media\notify.wav").play()
$timer.enabled=$true
})
# draw seperator line
$form.add_paint({
$gfx = $form.CreateGraphics()
$pen = new-object System.Drawing.Pen([System.Drawing.Color]::Black)
$gfx.drawLine($pen,0,24,$form.width,24)
$pen.dispose()
$gfx.dispose()
})
################
# add controls to the form
# hide close button if form is not sliding
if($slide){
$form.Controls.AddRange(#($label,$textbox,$pictureBox))
} else {
$form.Controls.AddRange(#($button,$label,$textbox,$pictureBox))
}
################
# show the form
[void]$form.showdialog()
save as New-Popup.ps1 to keep the usage the same. Kudo's to the author of http://scriptolog.blogspot.com/2008/02/windows-messenger-like-popup.html