I seem to be stuck trying to add a bit of style to my form. I have a ListBox and I want to add alternate shading to every other row. Is this even possible? I tried looking at the $ListBox.Items property and below that I don't see anything for background options. Any ideas?
$ListBox = New-Object System.Windows.Forms.ListBox
$ListBox.Size = '325,95'
$ListBox.Location = '345,25'
$ListBox.Items.Add("Checking...") > $null
The only way to do this in with the ListBox control in Windows Forms is by hijacking the actual drawing of each row.
First, change the DrawMode property of the ListBox:
$ListBox.DrawMode = [System.Windows.Forms.DrawMode]::OwnerDrawFixed
This will allow us to override graphic rendering of the items via the DrawItem event.
Now all we need is to define the function that will draw the items. I found this great example in C# on doing alternate row colors without affecting selected items.
Luckily, C# is easily ported to PowerShell:
$ListBox.add_DrawItem({
param([object]$s, [System.Windows.Forms.DrawItemEventArgs]$e)
if ($e.Index -gt -1)
{
Write-Host "Drawing item at index $($e.Index)"
<# If the item is selected set the background color to SystemColors.Highlight
or else set the color to either WhiteSmoke or White depending if the item index is even or odd #>
$color = if(($e.State -band [System.Windows.Forms.DrawItemState]::Selected) -eq [System.Windows.Forms.DrawItemState]::Selected){
[System.Drawing.SystemColors]::Highlight
}else{
if($e.Index % 2 -eq 0){
[System.Drawing.Color]::WhiteSmoke
}else{
[System.Drawing.Color]::White
}
}
# Background item brush
$backgroundBrush = New-Object System.Drawing.SolidBrush $color
# Text color brush
$textBrush = New-Object System.Drawing.SolidBrush $e.ForeColor
# Draw the background
$e.Graphics.FillRectangle($backgroundBrush, $e.Bounds)
# Draw the text
$e.Graphics.DrawString($s.Items[$e.Index], $e.Font, $textBrush, $e.Bounds.Left, $e.Bounds.Top, [System.Drawing.StringFormat]::GenericDefault)
# Clean up
$backgroundBrush.Dispose()
$textBrush.Dispose()
}
$e.DrawFocusRectangle()
})
Et voila:
By the looks of your code, you aren't using XAML, but I wanted to add this anyways as an alternative approach.
You can set this by setting up a style trigger by writing XAML as your front end code for the UI and specifying the setter properties within the trigger. Then within your ListBox control, you can specify the name of the style that you created on the ItemContanerStyle property and specify an AlertnationCount of 2 so it highlights each row with the colors you specified.
My example below shows how it works as you add text to the list box.
#Build the GUI
[xml]$xaml = #"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window" Title="Initial Window" WindowStartupLocation = "CenterScreen"
Width = "313" Height = "800" ShowInTaskbar = "True" Background = "lightgray">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel >
<StackPanel.Resources>
<Style x:Key="AlternatingRowStyle" TargetType="{x:Type Control}" >
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBox IsReadOnly="True" TextWrapping="Wrap">
Type something and click Add
</TextBox>
<TextBox x:Name = "inputbox"/>
<Button x:Name="button1" Content="Add"/>
<Button x:Name="button2" Content="Remove"/>
<Expander IsExpanded="True">
<ListBox x:Name="listbox" SelectionMode="Extended" AlternationCount="2"
ItemContainerStyle="{StaticResource AlternatingRowStyle}"/>
</Expander >
</StackPanel>
</ScrollViewer >
</Window>
"#
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )
#region Connect to Controls
Write-Verbose "Connecting to controls"
$xaml.SelectNodes("//*[#*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach {
New-Variable -Name $_.Name -Value $Window.FindName($_.Name) -Force
}
#endregion Connect to Controls
$Window.Add_SourceInitialized({
#Have to have something initially in the collection
$Script:observableCollection = New-Object System.Collections.ObjectModel.ObservableCollection[string]
$listbox.ItemsSource = $observableCollection
$inputbox.Focus()
})
#Events
$button1.Add_Click({
$observableCollection.Add($inputbox.text)
$inputbox.Clear()
})
$button2.Add_Click({
ForEach ($item in #($listbox.SelectedItems)) {
$observableCollection.Remove($item)
}
})
$Window.ShowDialog() | Out-Null
Related
I want to test notification in Powershell
I tried solution found on SO with https://gist.github.com/altrive/72594b8427b2fff16431
no error but no notification (I checked Background apps is on).
Then I tried this snippet (taken here https://michael-casey.com/2019/05/12/schedule-windows-notifications-with-powershell/) but I get error Exception calling "LoadXml" with "1" argument(s): "Exception from HRESULT: 0xC00CE558" and can't see how to fix it :
function test {
$app = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
$Template = [Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText01
$ToastTemplate = ([Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent($Template).GetXml())
$ToastTemplate = #"
<toast launch="app-defined-string">
<visual>
<binding template="ToastGeneric">
<text>Prevent Eye Strain</text>
<text>Take a 20 second break</text>
</binding>
</visual>
</toast>
"#
$ToastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
$ToastXml.LoadXml($ToastTemplate.OuterXml)
$notify = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($app)
$notify.Show($ToastXml)
#source: https://gist.github.com/Windos/9aa6a684ac583e0d38a8fa68196bc2dc
}
I am having 2 diffrent xml file with. Currently im trying to get the Last xml node. In the 1st ouput im getting because empty or Null value since i have a commented line. Please help how to handle in this case.
#Below line Run firstfile.xml
$xmlfile = 'C:\Programs\MyTasks\TCPROD-29658\Test_1309\D_SimulationTest\firstfile.xml'
#Below line to Run secondfile.xml
#$xmlfile = 'C:\Programs\MyTasks\TCPROD-29658\Test_1309\D_SimulationTest\secondfile.xml'
$xmllastsitenode = [xml]::new()
$xmllastsitenode.Load($xmlFile)
$lastsite_id = $xmllastsitenode.fccconfig.fccdefaults.LastChild.id
write-host 'Print1'
write-host $lastsite_id
if (!$lastsite_id) { Write-Host "variable is null" }
if ($lastsite_id) { Write-Host "variable is NOT null" }
write-host 'Print2'
Below is the xml files im trying to run
1st XML file
<?xml version="1.0" encoding="UTF-8"?>
<fccconfig version="1.3.2">
<fccdefaults>
<property name="CacheLocation" value="C:/Users/Public/" overridable="true"/>
<site id="-1940805554" overridable="true">
<parentfsc address="http://abcdefgh:1234/" priority="0" />
</site>
<!--__ANT_MARK__-->
</fccdefaults>
<!-- default parentfsc - this is a marker that will be overwritten by the installer -->
<parentfsc address="xyzlmnopq:10010" priority="0" transport="lan"/>
</fccconfig>
Output:-
Print1
variable is null
Print2
2nd XML file
<?xml version="1.0" encoding="UTF-8"?>
<fccconfig version="1.3.2">
<fccdefaults>
<property name="CacheLocation" value="C:/Users/Public/" overridable="true"/>
<site id="-1940805554" overridable="true">
<parentfsc address="http://abcdefgh:1234/" priority="0" />
</site>
</fccdefaults>
<!-- default parentfsc - this is a marker that will be overwritten by the installer -->
<parentfsc address="xyzlmnopq:10010" priority="0" transport="lan"/>
</fccconfig>
Output:-
Print1
-1940805554
variable is NOT null
Print2
Latest Code which have tried meantime
#Below line Run firstfile.xml
$xmlfile = 'C:\Programs\MyTasks\TCPROD-29658\Test_1309\D_SimulationTest\firstfile.xml'
#Below line to Run secondfile.xml
#$xmlfile = 'C:\Programs\MyTasks\TCPROD-29658\Test_1309\D_SimulationTest\secondfile.xml'
$xmllastsitenode = [xml]::new()
$xmllastsitenode.Load($xmlFile)
$lastsite_id = $xmllastsitenode.fccconfig.fccdefaults.LastChild.id
write-host 'Print1'
write-host $lastsite_id
if (!$lastsite_id)
{
Write-Host "variable is null"
# New logic to handle firt xml file cases
#$somesitetag ---> Having some xml tag content here
$newSiteNode = $xmlcontent.ImportNode($somesitetag, $true)
$antmarkline = (Get-Content $xmlfile) | select-string -Pattern $antmark
$xmlcontent.fccconfig.fccdefaults.InsertAfter($newSiteNode,$antmarkline)
}
if ($lastsite_id)
{
Write-Host "variable is NOT null"
#Insterting some new nodes
#This Working as second file conetent works here
}
write-host 'Print2'
You need to ignore the comments. Check the output of this for the first XML file.
$settings = [System.Xml.XmlReaderSettings]::new()
$settings.IgnoreComments = $true
$xmlreader = [System.Xml.XmlReader]::Create('./Desktop/Test.xml', $settings)
$xmldoc = [System.Xml.XmlDocument]::new()
$xmldoc.Load($xmlreader)
$xmldoc.fccconfig.fccdefaults.LastChild
I have need of retrieving and inspecting the delegate forwarding rule (the built-in delegate commands in EWS being inadequate for my needs since they choke on groups being used as delegates).
I am able to successfully locate the rule created by "Schedule+ EMS Interface". However, I am unable to retrieve PR_RULE_ACTIONS. Turning on tracing.
I see that the PidTagRuleMsgProvider property is getting returned just fine, but PR_RULE_ACTIONS never does.
I suspect that I am using the wrong MAPI property type in the propertyset definition, but I've gone through everything listed at http://msdn.microsoft.com/en-us/library/exchangewebservices.mapipropertytypetype(v=exchg.140).aspx . Any clues?
Here is the relevant snippet of code:
# Setup Basic EWS Properties for Message Search - Used to locate Hidden Forwarding Rule
$searchFilterForwardRule = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ItemSchema]::ItemClass, "IPM.Rule", [Microsoft.Exchange.WebServices.Data.ContainmentMode]::Prefixed, [Microsoft.Exchange.WebServices.Data.ComparisonMode]::Exact)
$itemViewForwardRule = New-Object Microsoft.Exchange.WebServices.Data.ItemView(30, 0, [Microsoft.Exchange.Webservices.Data.OffsetBasePoint]::Beginning)
$itemViewForwardRule.PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties, [Microsoft.Exchange.WebServices.Data.ItemSchema]::ItemClass, [Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject)
$itemViewForwardRule.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Associated
# Properties for Hidden Delegate Forwarding Rule
$PID_TAG_RULE_MSG_PROVIDER = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x65EB,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
$PID_TAG_RULE_ACTIONS = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x6680,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
# Property Set for Delegate Forward Rule
$propertySetForwardRule = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties, $PID_TAG_RULE_MSG_PROVIDER)
$forwardRuleExists = $false
$findResults = $service.FindItems([Microsoft.Exchange.Webservices.Data.WellKnownFolderName]::Inbox, $searchFilterForwardRule, $itemViewForwardRule)
If ($findResults.TotalCount -lt 1) {
Write-Error "Failed to find rule" "Error"
} Else {
Foreach ($item in $findResults.Items) {
$item.Load($propertySetForwardRule)
If ($item.ExtendedProperties.Count -ge 1) {
If ($item.ExtendedProperties[0].Value -eq "Schedule+ EMS Interface") {
$forwardRuleExists = $true
write-host "Delegate forwarding rule found." -ForegroundColor Cyan
$propertySetForwardRule.Add($PID_TAG_RULE_ACTIONS)
$item.Load($propertySetForwardRule)
Write-Host "Attempting to retrieve x6680 PR_RULE_ACTIONS (PidTagRuleActions)" -ForegroundColor Cyan
$PR_RULE_ACTIONS = $null
if($Item.TryGetProperty($Pid_Tag_Rule_Actions,[ref]$PR_RULE_ACTIONS)){
return $PR_RULE_ACTIONS
} # endif
else {write-host "TryGetProperty for PR_RULE_ACTIONS failed!" -ForegroundColor Red
} # endelse
} # End If - Correct Message
} # End If - Has Extended Properties
} # End ForEach
} # End If - Message Count
Glen Scales was able to set me on the right path. It turns out that PR_RULE_ACTIONS is not exposed via EWS, but the same data exposed via an attribute called PR_EXTENDED_RULE_ACTIONS. Now I'm happily slinging code to parse the binary blob.
http://msdn.microsoft.com/en-us/library/ee218391(v=EXCHG.80).aspx
The property tag for PR_RULE_ACTIONS is 0x668000FE. You can see it (and the property data) in OutlookSpy (I am its author) - go to the Inbox folder, click IMAPIFolder button, go to the PR_RULES_TABLE tab, select the rule, double click on the PR_RULE_ACTIONS property.
Note that PT_ACTIONS MAPI type (0x000FE) is only accessing in Extended MAPI, I don't think EWS will be able to return it.
I have some xml files where I want to insert the contents of one xml file into another. I thought I'd use LastChild and the InsertAfter method to accomplish this. So far it's not working for me.
Here is the parent.xml file:
<manifest>
<manifestExecution>
<assetDetail>
<fileAsset fileAssetGuid="parentguid1">
<parentfile1 />
</fileAsset>
<fileAsset fileAssetGuid="parentguid2">
<parentfile2 />
</fileAsset>
</assetDetail>
</manifestExecution>
</manifest>
And here is the child.xml file:
<manifest>
<manifestExecution>
<assetDetail>
<fileAsset fileAssetGuid="childguid1">
<childfile1 />
</fileAsset>
</assetDetail>
</manifestExecution>
</manifest>
What I want to do is select the fileAsset node(s) from child.xml and insert into parent.xml after the last fileAsset node in parent.xml.
Here is my test code:
$parent = [xml] (Get-Content d:\temp\parent.xml)
$parentnode = $parent.manifest.manifestExecution.assetDetail
$child = [xml] (Get-Content d:\temp\child.xml)
$childnode = $child.manifest.manifestExecution.assetDetail.InnerXml
$parentnode.InsertAfter($childnode, $parentnode.LastChild)
Here is the error msg I'm getting:
Cannot convert argument "0", with value: "<fileAsset fileAssetGuid="childguid1"> <childfile1 /></fileAsset>", for "InsertAfter" to type "System.Xml.XmlNode": "Cannot conver
t the "<fileAsset fileAssetGuid="childguid1"><childfile1 /></fileAsset>" value of type "System.String" to type "System.Xml.XmlNode"."
At line:5 char:24
+ $parentnode.InsertAfter <<<< ($childnode, $parentnode.LastChild)
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
What am I doing wrong?
You need to iterate through $childnode's children, remove them from their parent, and import them into the new document context ($child and $parent are different XmlDocument instances) before appending to $parentnode.
This will append all fileAsset nodes from $childnode into $parentnode.
$parent = [xml](get-content d:\temp\parent.xml)
$parentnode = $parent.manifest.manifestexecution.assetdetail
$child = [xml](get-content d:\temp\child.xml)
$childnode = $child.manifest.manifestexecution.assetdetail
while ($childnode.haschildnodes) {
$cn = $childnode.firstchild
$cn = $childnode.removechild($cn)
$cn = $parentnode.ownerdocument.importnode($cn, $true)
$parentnode.appendchild($cn)
}
Fortunately, most of these methods return the same XmlNode or a new version of it, so the body of the while loop could chained together like this:
$parentnode.appendchild( $parentnode.ownerdocument.importnode( $childnode.removechild( $childnode.firstchild ), $true ))
InsertAfter(newChild,referenceChild) could also work, but would be done a little differently since it also needs a reference to the the node that it will be inserted after.
your first problem is that you're not getting an XML element, but a string. You need to get an XML node from your XML document, but the shorthand method you're using is guessing you want a string. Usually you can force it by explicitly casting it over to [System.Xml.XmlElement], but that doesn't always work. You can reliably get an element using "SelectSingleNode".
You've not hit your second problem yet, but it's just around the corner. Once you've got XML, it still won't work because it's from a different XML document, so you need to "Import" the node. You'll want to tweak this to get the XML to align the way you envision, but the code works.
$parentString = #"
<manifest>
<manifestExecution>
<assetDetail>
<fileAsset fileAssetGuid="parentguid1">
<parentfile1 />
</fileAsset>
<fileAsset fileAssetGuid="parentguid2">
<parentfile2 />
</fileAsset>
</assetDetail>
</manifestExecution>
</manifest>
"#
$childString = #"
<manifest>
<manifestExecution>
<assetDetail>
<fileAsset fileAssetGuid="childguid1">
<childfile1 />
</fileAsset>
</assetDetail>
</manifestExecution>
</manifest>
"#
$parent = [xml] ($parentString)
$parentnode = $parent.manifest.manifestExecution.assetDetail
$child = [xml] ($childString)
$xpath = '/manifest/manifestExecution/assetDetail'
$childnode = $child.SelectSingleNode($xpath)
Write-Host("So the child is $($childnode.OuterXML)")
$importedNode = $parent.ImportNode($childNode,$true)
Write-Host("And after importing: $($importedNode.OuterXML)")
$parentnode.InsertAfter($importednode, $parentnode.LastChild)
Write-Host("To finally yield: $($parent.OuterXML)")
Also, you may find you can use something like your original code if you cast it to XmlElement properly.
$childnode = [System.Xml.XmlElement]$child.manifest.manifestExecution.assetDetail.InnerXml
I'm using the following PowerShell 2.0 code to grab input from a vb inputbox:
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$name = [Microsoft.VisualBasic.Interaction]::InputBox("What is your name?", "Name", "bob")
Sometimes when I run it the input box appears behind the active window. Is there a way to make the input box top most? Or an easy way to get its handle and just use setforegroundwindow?
Thanks!!
I'm not sure how to do this easily considering that the InputBox call is modal so you can't easily try to find the window handle and do a set-foreground on that window (unless you attempt to use a background job). Rather than use this VisualBasic text input box, how about a "roll your own" implementation using WPF/XAML. It is pretty easy but it does require WPF which is installed by PowerShell 2.0 if necessary.
$Xaml = #'
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="Name" Height="137" Width="444" MinHeight="137" MinWidth="100"
FocusManager.FocusedElement="{Binding ElementName=TextBox}"
ResizeMode="CanResizeWithGrip" >
<DockPanel Margin="8">
<StackPanel DockPanel.Dock="Bottom"
Orientation="Horizontal" HorizontalAlignment="Right">
<Button x:Name="OKButton" Width="60" IsDefault="True"
Margin="12,12,0,0" TabIndex="1" >_OK</Button>
<Button Width="60" IsCancel="True" Margin="12,12,0,0"
TabIndex="2" >_Close</Button>
</StackPanel>
<StackPanel >
<Label x:Name="Label" Margin="-5,0,0,0" TabIndex="3">Label:</Label>
<TextBox x:Name="TextBox" TabIndex="0" />
</StackPanel>
</DockPanel>
</Window>
'#
if ([System.Threading.Thread]::CurrentThread.ApartmentState -ne 'STA')
{
throw "Script can only be run if PowerShell is started with -STA switch."
}
Add-Type -Assembly PresentationCore,PresentationFrameWork
$xmlReader = [System.Xml.XmlReader]::Create([System.IO.StringReader] $Xaml)
$form = [System.Windows.Markup.XamlReader]::Load($xmlReader)
$xmlReader.Close()
$window = $form.FindName("Window")
$window.Title = "My App Name"
$label = $form.FindName("Label")
$label.Content = "What is your name?"
$textbox = $form.FindName("TextBox")
$okButton = $form.FindName("OKButton")
$okButton.add_Click({$window.DialogResult = $true})
if ($form.ShowDialog())
{
$textbox.Text
}
This could be rather easily wrapped up into a Read-GuiText function.
If you set a default value for the input box it kind of makes it "modal", something like this:
$response = [Microsoft.VisualBasic.Interaction]::InputBox("Do you want to include servers in MANUAL REBOOT group ? If YES, please type: Include MANUAL reboot group","Warning!!!","")
Sub SetInputBoxFocus()
System.Threading.Thread.Sleep(300)
Microsoft.VisualBasic.AppActivate("Title)
''Console.WriteLine("Setting focus ") '"
End Sub
Dim strPW as String = ""
Dim tsStartInfo As New System.Threading.ThreadStart(AddressOf SetInputBoxFocus)
Dim tBackgroundJob As New System.Threading.Thread(tsStartInfo)
tBackgroundJob.Start()
strPW = Microsoft.VisualBasic.InputBox("Prompt: ", "Title", "", -1, -1)
tBackgroundJob = Nothing
tsStartInfo = Nothing