问题描述
param (
[Parameter(mandatory=$true)]$XMLFile
)
Function New-WPFDialog() {
Param([Parameter(Mandatory = $True,HelpMessage = 'XaML Data defining a GUI',Position = 1)]
[string]$XamlData)
# Add WPF and Windows Forms assemblies
try { Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms }
catch { Throw 'Failed to load Windows Presentation Framework assemblies.' }
# Create an XML Object with the XaML data in it
[xml]$xmlWPF = $XamlData
# Create the XAML reader using a new XML node reader,UI is the only hard-coded object name here
Set-Variable -Name XaMLReader -Value @{ 'UI' = ([Windows.Markup.XamlReader]::Load((new-object -TypeName System.Xml.XmlNodeReader -ArgumentList $xmlWPF))) }
# Create hooks to each named object in the XAML reader
$Elements = $xmlWPF.SelectNodes('//*[@Name]')
ForEach ( $Element in $Elements ) {
$VarName = $Element.Name
$VarValue = $XaMLReader.UI.FindName($Element.Name)
$XaMLReader.Add($VarName,$VarValue)
}
return $XaMLReader
}
Function Replace-InlineFormatting() {
param([string]$InputString)
$Temp = $InputString
$Temp = $Temp.ToString().Replace("[","<")
$Temp = $Temp.ToString().Replace("]",">")
return $Temp
}
Function New-PopUpWindow() {
param(
[string]
$Header = "No Header Supplied",$Body = "No Body Supplied")
# This is the XaML that defines the GUI.
$WPFXamL = @'
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Popup" Background="#FF0066CC" Foreground="#FFFFFFFF" ResizeMode="noresize" ShowInTaskbar="False" WindowStartupLocation="CenterScreen" Width="900" SizetoContent="Height" WindowStyle="None" Padding="20" Margin="0" Topmost="True">
<StackPanel Name="Popup" Margin="10">
<TextBlock Name="Header" FontSize="25" Foreground="White" Margin="20,10,20,10" textwrapping="Wrap"/>
<TextBlock Name="Body" FontSize="17" Foreground="LightGray" Margin="20,10" textwrapping="Wrap"/>TextBlock with <Bold>bold</Bold>,<Italic>italic</Italic> and <Underline>underlined</Underline> text.</TextBlock>
<TextBlock Name="Spacer" FontSize="15" Foreground="LightGray" textwrapping="Wrap"/>
<Button Name="OKButton" Content="OK" Width="75" Background="#FF0066CC" BorderBrush="White" Foreground="White" Padding="8,4" Margin="0,10"/>
</StackPanel>
</Window>
'@
# Build Dialog
$WPFGui = New-WPFDialog -XamlData $WPFXaml
$WPFGui.Header.Text = $Header
$WPFGui.Body.Text = Replace-InlineFormatting -Input $Body
$WPFGui.OKButton.Add_Click( { $WPFGui.UI.Close() })
$null = $WPFGUI.UI.dispatcher.InvokeAsync{ $WPFGui.UI.ShowDialog() }.Wait()
}
If(Test-Path -PathType Leaf $XMLfile) {
[XML]$ScriptConfig = Get-Content $XMLfile
$ScriptName = $ScriptConfig.Config.ScriptName
$PopupHeader = $ScriptConfig.Config.Popup.Header
$PopupBody = $ScriptConfig.Config.Popup.Body
$CommandProgram = $ScriptConfig.Config.Command.Program
$CommandArguments = $ScriptConfig.Config.Command.Arguments
If($PopupHeader -and $PopupBody) { New-PopUpWindow -Header $PopupHeader -Body $PopupBody }
If($CommandProgram) {
If(Get-Command $CommandProgram -ErrorAction SilentlyContinue) {
Invoke-Expression "$CommandProgram $CommandArguments"
} Else {
New-PopUpWindow -Header "ERROR" -Body "The command '$Command' cannot be found."
}
}
} Else {
New-PopUpWindow -Header "ERROR" -Body "The XML file '$XMLfile' cannot be found."
}
我一直收到[],所以没有结果。有人可以帮助我理解为什么吗? 谢谢
解决方法
也许我误解了您的问题,但是从.txt收集数据确实非常容易。您可以使用3或4行代码打开和阅读文本文件:
f = open("file.txt","r+")
data = f.read()
print(data)
f.close()
,如果您要从.txt的特定行中查找数据,则可以使用:
f = open("file.txt","r+")
lines = f.readlines()
print(lines[25])
f.close()
或者如果您想在文本文件中一般查找特定数据
f = open("file.txt","r+")
read = f.read()
data = read
f.close()
if "<specific word you're looking for>" in data:
#react based on the information being in the data.
我不认为您必须弄乱.format或类似的东西,您可以只使用文件的路径,或者如果它与脚本位于同一文件夹中,则只需文件名即可。