Suppress PowerShell output messages new outlook application object is created?
$outlook = New-Object -ComObject outlook.application
$outlookItem = $outlook.CreateItem("olMailItem")
I already tried using these approaches. They did not work:
$outlook = New-Object -ComObject outlook.application > $Null
$outlook = New-Object -ComObject outlook.application |Out-Null
($outlook = New-Object -ComObject outlook.application) |Out-Null
These are the output message that I don't want them on the screen:
Application : Microsoft.Office.Interop.Outlook.ApplicationClass
Class : 5
...
...
#boxdog, thanks for pointing it out.
After many break points, I noticed the output is produced by Attachments.Add() method. It is resolved using >$null.
$outlookItem.Attachments.Add("fileName.txt") > $null
Related
I have written a dice roller script. I have one with a single die and one with two dice. they both work in powershell, and the single die version works after converting to .exe using ps2exe. but the two die version runs as an exe but I get the following error "You cannot call a method on a null-valued expression"
Below is the 2 die script that gives the error after converting.
<#
.NAME
Random Dice Roller
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Display1 = 1,2,3,4,5,6 | Get-Random
$Display2 = 1,2,3,4,5,6 | Get-Random
$LabelImage = [system.drawing.image]::FromFile("f:\psscripts\Die1.png")
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(400,300)
$Form.text = "Roll The Dice"
$Form.TopMost = $false
$Form.location = New-Object System.Drawing.Point(1,1)
$Form.StartPosition = "CenterScreen"
$Die1 = New-Object system.Windows.Forms.Label
$Die1.Text = "$Display1"
$Die1.AutoSize = $false
$Die1.width = 200
$Die1.height = 200
$Die1.location = New-Object System.Drawing.Point(1,1)
$Die1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',150,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))
$Die1.ForeColor = [System.Drawing.ColorTranslator]::FromHtml("#000000")
$Die1.BackgroundImage = $LabelImage
$Die2 = New-Object system.Windows.Forms.Label
$Die2.Text = "$Display2"
$Die2.AutoSize = $false
$Die2.width = 200
$Die2.height = 200
$Die2.location = New-Object System.Drawing.Point(200,1)
$Die2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',150,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))
$Die2.ForeColor = [System.Drawing.ColorTranslator]::FromHtml("#000000")
$Die2.BackgroundImage = $LabelImage
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(175,220)
$Button1.Size = New-Object System.Drawing.Size(80,50)
$Button1.Text = "Roll Me"
$Button1.Add_Click({Roll})
$Form.controls.AddRange(#($Die1,$Die2,$Button1))
$Die.Add_Click({ Roll })
#region Logic
function Roll{
$Form.Close()
$Form.Dispose()
.\Dice.exe}
#endregion
[void]$Form.ShowDialog()
There are two possible causes here:
Note that there is no safety in wrapping you PowerShell script in an .exe file. In fact the PowerShell script is extracted in a temporary folder and executed from there which might be the cause of your issue (e.g. the relative .\Dice.exe location)
It is unwise to do a $Form.Dispose() from within a form function/event
Remove that from the function and put it outside the form, e.g.:
[void]$Form.ShowDialog(); $Form.Dispose()
(Or do not use the Dispose method at all as PowerShell will usually already takes care of that if you [void] the $From.)
What is your motive for creating an EXE?
An alternative might be to create a CMD file with a Batch bootstrap instead. Create a CMD file with the following code and include your script after this.
<# :
#ECHO OFF
SET f0=%~f0
PowerShell -NoProfile -ExecutionPolicy RemoteSigned -Command ".([scriptblock]::Create((get-content -raw $Env:f0)))"
PAUSE
GOTO :EOF
<#~#>
# Insert PowerShell script after this line.
Trying to make it where powershell minimizes all windows, refreshes the desktop, then brings it back up. Thing is.. the desktop doesn't refresh after the windows minimize.
$shell = New-Object -ComObject "Shell.Application"
$shell.minimizeall()
start-sleep 1
$shell.sendkeys('{F5}')
start-sleep 1
$shell.undominimizeall()
exit
You can use this:
$shell = New-Object -ComObject Shell.Application
$shell.minimizeall()
start-sleep 1
$wsh = New-Object -ComObject Wscript.Shell
$wsh.sendkeys('{F5}')
start-sleep 1
$shell.undominimizeall()
exit
I am trying to issue approvals on mailbox items via Powershell but can't seem to find the right method to do so. Here is the code I have used thus far.
Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$recipient = $namespace.CreateRecipient('othermailbox#example.local')
$recipient.Resolve();
$sharedFolder = $namespace.GetSharedDefaultFolder($recipient, $olFolders::olFolderInBox)
$sharedFolder.Items
The member objects from $sharedFolder. Items contain string properties around VotingOptions and VotingResporse but I don't see a method to perform an approved vote. Any help is appreciated as there are hundreds of items I need to bulk approve. Thanks.
Loop through the items, for each item read the VotingOptions property, call MailItem.Reply (returns the new item), set the VotingResponse property on the reply message, call Send.
This is the code I ended up going with based on Dmitiry's input.
Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$recipient = $namespace.CreateRecipient('othermailbox#example.local')
$recipient.Resolve()
$sharedFolder = $namespace.GetSharedDefaultFolder($recipient, $olFolders::olFolderInBox)
$messages = $sharedFolder.Items | Where-Object { $_.subject -eq "Approval Required" }
foreach ($message in $messages)
{
$reply = $message.Reply()
$reply.VotingResponse = "Approve"
$reply.Send()
}
I'm trying to create a message dialogue in Powershell where the user has no option to action on the message as that is the intention. So the message will have the X button grayed along with the buttons (not showing buttons are even better).
The closest I could reach was disabling the X via below code:
$wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop
$wshell.Popup("Aborted",0,"ERROR!",48+4)
But cannot figure out disabling button part. Below MS articles were of little help as well:
http://blogs.technet.com/b/heyscriptingguy/archive/2006/07/27/how-can-i-display-a-message-box-that-has-no-buttons-and-that-disappears-after-a-specified-period-of-time.aspx
https://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx
Referred to few other articles over net some even suggesting custom made buttons using HTML, or VB library. But not what I was looking for.
Any help/hint/suggestion would be deeply appreciated.
Regards,
Shakti
Dig into the .NET Windows.Forms namespace, you can make pretty much any kind of window you want with that:
https://msdn.microsoft.com/en-us/library/system.windows.forms.aspx
Here's a quick sample window w/ no buttons that can't be moved/closed by the user, but closes itself after 5 seconds:
Function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
# Build Form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Test"
$objForm.Size = New-Object System.Drawing.Size(220,100)
# Add Label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(80,20)
$objLabel.Size = New-Object System.Drawing.Size(100,20)
$objLabel.Text = "Hi there!"
$objForm.Controls.Add($objLabel)
# Show the form
$objForm.Show()| Out-Null
# wait 5 seconds
Start-Sleep -Seconds 5
# destroy form
$objForm.Close() | Out-Null
}
generate-form
Using the script above as a launching point I'm attempting to make a function that will allow me to popup a please wait message run some more script then close the popup
Function Popup-Message {
param ([switch]$show,[switch]$close)
Add-Type -AssemblyName System.Windows.Forms
# Build Form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Test"
$objForm.Size = New-Object System.Drawing.Size(220,100)
# Add Label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(80,20)
$objLabel.Size = New-Object System.Drawing.Size(100,20)
$objLabel.Text = "Hi there!"
$objForm.Controls.Add($objLabel)
If ($show)
{
$objForm.Show() | Out-Null
$global:test = "Show"
}
If ($close)
{
# destroy form
$objForm.Close() | Out-Null
$global:test = "Close"
}
}
I can then get the popup to display by:
Popup-Message -show
At this point I can see the $test variable as Show
But when I try to close the window with:
Popup-Message -close
But the popup window will not close
If I look at $test again it will show as Close
I'm assuming this has something to do with keeping the function in the Global Scope but I can't figure out how to do this with the form
My first question here, sorry if something is wrong in the way I posted.
Well, I'm new in developing graphic things in Powershell, and I needed to make a simple script to configure hostname, IP, etc and make it simple to end users (ugh).
I'm developing a simple graphic interface in PowerGUI, I'm on it for a couple of days, testing it exclusively inside PowerGUI, and it was not looking bad, but when I finally ran the script outside PowerGUI, just running it in Powershell, the graphics looks quite different than in PowerGUI, like positioning, borders, themes, etc. I was disappointing. I'll post screenshots and a bit of the code.
Inside PowerGUI
Outside
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Get-Variable -Exclude PWD,*Preference | Remove-Variable -EA 0
ipconfig -all | Out-File $env:SYSTEMDRIVE"\old_network.txt"
#INICIO Fontes da aplicação
$font = New-Object System.Drawing.Font("Segoe UI",8.5,[System.Drawing.FontStyle]::Regular)
$font_btn_concluir = New-Object System.Drawing.Font("Segoe UI",15,[System.Drawing.FontStyle]::Regular)
$font_l_instru = New-Object System.Drawing.Font("Segoe UI",12,[System.Drawing.FontStyle]::Regular)
$font_i_n_sala = New-Object System.Drawing.Font("Segoe UI",20,[System.Drawing.FontStyle]::Regular)
#FIM Fontes da aplicação
$form = New-Object Windows.Forms.Form
$form.Size = New-Object Drawing.Size #(800,600)
$form.StartPosition = "CenterScreen"
$form.Font = $font
#$form.ControlBox = $false
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$form.Text = "Configuração Telesalas - UNIASSELVI"
$form.Icon = $icon
$form.FormBorderStyle = "FixedDialog
So, anyone have any clue why this happens?
Its Because of the version of your forms. powergui automatically call v4.0** form my system but powershell consol calls v2. you can test it by yourself after compiling check the consols.