【发布时间】:2015-10-27 21:42:35
【问题描述】:
所以我正在开发一个基于 Powershell 的 WPF 应用程序,我收到以下错误:
Exception calling "ShowDialog" with "0" argument(s): "Unidentified: USB_Disable="
At C:\Users\miste\OneDrive\Documents\Visual Studio 2015\Projects\Vaccinate-USB\Vaccinate-USB\Vaccinate-USB.ps1:344 char:5
+ $Form.ShowDialog() | out-null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : RuntimeException
现在相当奇怪的是,"USB_Disable=" 字符串只能在从未与$Form.ShowDialog() 接触的 hereString 中找到。下面是包含 hereString 的初始化函数的摘录,然后是表单加载器函数。如果您需要完整的代码,请告诉我(大约 800 行)
初始化器:
function Initialize-ARD
{
if (-not (Test-Admin))
{
$wshpop = New-Object -ComObject Wscript.Shell
$wshpop.Popup('Please run as an administrator.',0,'Error!',0x0)
exit
}
if(-not (Test-Path -Path $ARDHome))
{
if(-not (Test-IsNetConnected))
{
$wshpop = New-Object -ComObject Wscript.Shell
$wshpop.Popup('Please make sure you have an internet connection for first run. Connect to the internet and the re-run the program.',0,'Error!',0x0)
exit
}
New-Item -Path $ARDHome -ItemType 'Directory' -Force
New-Item -Path "$ARDHome\config.ini" -ItemType 'File' -Force
#HERESTRING STARTS HERE!
Set-Content -Path "$ARDHome\config.ini" -Force -Value @'
[USB]
USB_Block=False
USB_Lock=False
USB_Delete=False
USB_Disable=False
[CD]
CD_Block=False
CD_Lock=False
CD_Delete=False
CD_Disable=False
[Firewall]
Firewall_Enabled=False
Firewall_Disabled=True
Firewall_UpdateFreq=0.5
'@
#New-Download -URL 'http://ha.x10.bz/package.zip/' -Destination "$ARDHome\package.zip"
#Expand-ZIPFile -File "$ARDHome\package.zip" -Destination $ARDHome
#Import-Module "$ARDHome\DeviceManagment\DeviceManagement.psd1" -Force
}
else{}
$script:ini = Get-IniFile -FilePath "$ARDHome\config.ini"
$USBBlock = $ini.USB.USB_Block
$USBLock = $ini.USB.USB_Lock
$USBDelete = $ini.USB.USB_Delete
$USBDisable = $ini.USB.USB_Disable
$CDBlock = $ini.CD.CD_Block
$CDLock = $ini.CD.CD_Lock
$CDDelete = $ini.CD.CD_Delete
$CDEject = $ini.CD.CD_Eject
$FirewallEnabled = $ini.Firewall.Firewall_Enabled
$FirewallDisabled = $ini.Firewall.Firewall_Disabled
$FirewallValue = $ini.Firewall.Firewall_UpdateFreq
if($USBBlock -eq "True")
{
$USBBlock = $true
}
else
{
$USBBlock = $false
}
if($USBLock -eq "True")
{
$USBLock = $true
}
else
{
$USBLock = $false
}
if($USBDelete -eq "True")
{
$USBDelete = $true
}
else
{
$USBDelete = $false
}
if($USBDisable -eq "True")
{
$USBDisable = $true
}
else
{
$USBDisable = $false
}
if($CDBlock -eq "True")
{
$CDBlock = $true
}
else
{
$CDBlock = $false
}
if($CDDelete -eq "True")
{
$CDDelete = $true
}
else
{
$CDDelete = $false
}
if($CDEject -eq "True")
{
$CDEject = $true
}
else
{
$CDEject = $false
}
if($CDLock -eq "True")
{
$CDLock = $true
}
else
{
$CDLock = $false
}
if($FirewallEnabled -eq "True")
{
$FirewallEnabled = $true
}
else
{
$FirewallEnabled = $false
}
if($FirewallDisabled -eq "True")
{
$FirewallDisabled = $true
}
else
{
$FirewallDisabled = $false
}
$USB_Block.IsChecked = $USBBlock
$USB_Lock.IsChecked = $USBLock
$USB_Delete.IsChecked = $USBDelete
$USB_Disable.IsChecked = $USDisable
$CD_Block.IsChecked = $CDBlock
$CD_Lock.IsChecked = $CDLock
$CD_Delete.IsChecked = $CDDelete
$CD_Eject.IsChecked = $CDEject
$Firewall_Enabled.IsChecked = $FirewallEnabled
$Firewall_Disabled.IsChecked = $FirewallDisabled
$Firewall_UpdateFreq.Value = $FirewallValue
}
表单及其加载器:
function Initialize-Window
{
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Autorun Defender" Height="376" Width="528" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" Background="Black" Opacity="1">
<Grid Margin="0,10,4,1">
<TabControl Name="tabControl" HorizontalAlignment="Left" Height="285.5" VerticalAlignment="Top" Width="493" Margin="10,0,0,0" Background="#FF2E2E2E">
<TabItem Header="Welcome" Background="#FF2E2E2E" Margin="-2,0,2,0" Width="80">
<Grid Background="#FF2E2E2E">
<Viewbox HorizontalAlignment="Left" Margin="10,0,0,87.5" Width="116">
<Image Name="image" Height="100" Width="100" Source="$(Get-ScriptDirectory)\Logo.png"/>
</Viewbox>
<Label Name="title1" Content="Autorun" HorizontalAlignment="Left" Margin="126,0,0,0" VerticalAlignment="Top" Height="67" Width="191" FontSize="48" FontStyle="Italic" FontWeight="Bold" FontFamily="Harlow Solid Italic"/>
<Label Name="title2" Content="Defender" HorizontalAlignment="Left" Margin="206,67,0,87.5" Width="205" FontSize="48" FontStyle="Italic" FontWeight="Bold" FontFamily="Harlow Solid Italic"/>
<Label Name="copyright" Content="© 2015 LogoiLab" VerticalAlignment="Bottom" FontSize="12" Height="26.5" HorizontalAlignment="Left" Width="101" Foreground="#FFD4D4D4"/>
</Grid>
</TabItem>
<TabItem Header="USB" Background="#FF2E2E2E" Margin="-2,0,2,0" Width="80">
<Grid Background="#FF2E2E2E">
<GroupBox Name="USB_Passive" Header="Passive" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="224" Width="230" Foreground="#FFD4D4D4">
<Grid>
<CheckBox Name="USB_Block" Content="Block Autorun" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
<CheckBox Name="USB_Lock" Content="Lock Autorun Edits" Margin="10,30,89.656,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
</Grid>
</GroupBox>
<GroupBox Name="USB_Active" Header="Active" HorizontalAlignment="Left" Margin="249,10,0,0" VerticalAlignment="Top" Height="224" Width="230" Foreground="#FFD4D4D4">
<Grid>
<CheckBox Name="USB_Delete" Content="Delete Autorun.inf Files" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
<CheckBox Name="USB_Disable" Content="Disable USBs With Autorun" HorizontalAlignment="Left" Margin="10,30,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
</Grid>
</GroupBox>
</Grid>
</TabItem>
<TabItem Header="CD/DVD" Background="#FF2E2E2E" Margin="-2,0,2,0" Width="80">
<Grid Background="#FF2E2E2E">
<GroupBox Name="CD_Passive" Header="Passive" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="224" Width="230" Foreground="#FFD4D4D4">
<Grid>
<CheckBox Name="CD_Block" Content="Block Autorun" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
<CheckBox Name="CD_Lock" Content="Lock Autorun Edits" Margin="10,30,89.656,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
</Grid>
</GroupBox>
<GroupBox Name="CD_Active" Header="Active" HorizontalAlignment="Left" Margin="249,10,0,0" VerticalAlignment="Top" Height="224" Width="230" Foreground="#FFD4D4D4">
<Grid>
<CheckBox Name="CD_Delete" Content="Delete Autorun.inf Files" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
<CheckBox Name="CD_Eject" Content="Eject CDs With Autorun" HorizontalAlignment="Left" Margin="10,30,0,0" Width="227" Height="26" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
</Grid>
</GroupBox>
</Grid>
</TabItem>
<TabItem Header="Firewall" Background="#FF2E2E2E" Margin="-2,0,2,0" Width="80">
<Grid Background="#FF2E2E2E">
<TextBlock TextWrapping="WrapWithOverflow" Foreground="#FFD4D4D4">
This is a device firewall. It will block new devices from being "Pluged and Played". This type of firewall is meant to prevent such attacks as "BadUSB" or other hardware attacks. Please note that this may cause system errors(especially if your system has a lot of software defined devices).
</TextBlock>
<RadioButton Background="black" Name="Firewall_Enabled" Content="Enable Device Firewall" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,83.04,0,0" Foreground="#FFD4D4D4"/>
<RadioButton Background="black" Name="Firewall_Disabled" Content="Disable Device Firewall" HorizontalAlignment="Left" Margin="30,103,0,111.54" Foreground="#FFD4D4D4"/>
<TextBlock TextWrapping="WrapWithOverflow" Height="87.12" Margin="10,0" VerticalAlignment="Bottom" Foreground="#FFD4D4D4">
Check Frequency:
<LineBreak/>
This is how often the firewall checks for new devices, set this very low if you're afraid of BadUSB or its variants.
</TextBlock>
<Slider Minimum=".50" Maximum="300" TickPlacement="BottomRight" TickFrequency="5" IsSnapToTickEnabled="True" Name="Firewall_UpdateFreq" Margin="10,0,75,10" Height="29.96" VerticalAlignment="Bottom" Value="{Binding Count}"/>
<Label Name="SliderValue_Label" Content="" Height="29.96" Margin="0,0,10,10" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="60" Foreground="#FFD4D4D4"/>
</Grid>
</TabItem>
<TabItem Header="Info" Background="#FF2E2E2E" Margin="-2,0,2,-1" RenderTransformOrigin="0.137,0.501" Height="46" VerticalAlignment="Top" Width="80" >
<Grid Background="#FF2E2E2E">
<Viewbox HorizontalAlignment="Left" Height="97.389" Margin="10,10,0,0" VerticalAlignment="Top" Width="100">
<Image Name="Avatar" Height="100" Width="100" Source="$(Get-ScriptDirectory)\LogoiLab.png"/>
</Viewbox>
<Label Name="username" Content="© 2015 LogoiLab" Margin="115,10,10,0" VerticalAlignment="Top" FontSize="24" FontWeight="Bold" FontStyle="Italic" Foreground="#FFD4D4D4"/>
<Label Name="quote" Content="~ The logos behind the pathos." Margin="115,51.923,10,0" VerticalAlignment="Top" FontWeight="Bold" FontStyle="Italic" Foreground="#FFD4D4D4"/>
<Button Name="link" Content="http://www.github.com/LogoiLab" VerticalAlignment="Top" Margin="115,79.656,10,0" Cursor="Hand" FontStyle="Oblique"/>
<WebBrowser Margin="10,115,10,10" Source="http://ha.x10.bz/AutorunDefender/changelog.html"/>
</Grid>
</TabItem>
<TabItem Header="Feedback" Background="#FF2E2E2E" Margin="-2,0,2,-1" RenderTransformOrigin="0.137,0.501" Height="46" VerticalAlignment="Top" Width="80" >
<Grid Background="#FF2E2E2E">
<WebBrowser Margin="10,10,10,10" Source="http://goo.gl/forms/B77vvnhLPV"/>
</Grid>
</TabItem>
</TabControl>
<Button Name="Apply" Content="Apply" HorizontalAlignment="Left" Margin="353,290.5,0,0" VerticalAlignment="Top" Width="150" Height="35.5"/>
<Label Name="Status_Label" VerticalContentAlignment="Center" Content="" Margin="10,0,170,10" VerticalAlignment="Bottom" Height="35.5" Foreground="White"/>
</Grid>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$form=[Windows.Markup.XamlReader]::Load($reader)
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name) -Scope Script }
$Firewall_Enabled.Add_Click({
$Firewall_Enabled.IsChecked = $True
$FireWall_Disabled.IsChecked = $False
})
$Firewall_Disabled.Add_Click({
$Firewall_Enabled.IsChecked = $False
$FireWall_Disabled.IsChecked = $True
})
$link.Add_Click({
Start-Process 'http://www.github.com/LogoiLab'
})
$Firewall_UpdateFreq.Add_ValueChanged({
$SliderValue_Label.Content = ($Firewall_UpdateFreq.value).ToString() + " Sec"
})
$Apply.Add_Click({
$Status_Label.ForeGround = 'yellow'
$Status_Label.Content = 'Applying Changes...'
Start-Handler
})
$Status_Label.Foreground = 'green'
$Status_Label.Content = 'Ready...'
$Form.Add_ContentRendered({
Initialize-ARD
})
#$Form.Add_Loaded({
# Initialize-ARD
#})
$Form.Add_Closing({
Deinitialize-Window
})
$Form.ShowDialog() | out-null
}
这是我以前从未遇到过的一个非常奇怪的错误,不知何故 ShowDialog 方法正在获取一个永远不应该被解析的字符串(更不用说甚至不允许访问)。
任何帮助将不胜感激!
【问题讨论】:
-
无论如何你可以把它归结为一个最小的可验证的例子?更短的东西来产生你的问题?我不想要完整的代码(反正这里没有空间),但我们无法运行你在那里拥有的东西,因为我看到至少一个我们没有的功能
-
还可以查看jonathanmedd.net/2012/05/… 以摆脱所有这些 if 语句。
$USBBlock = [System.Convert]::ToBoolean($ini.USB.USB_Block) -
感谢您的转换想法,我认为这更多地与脚本如何加载到内存中有关,并且更多的是 WMI 的问题。另外,这里有一个人有类似错误的线程,那里的人建议运行一个 ccleaner:link
-
转换不是为了解决问题,而是帮助使代码更简洁。如果是这样,那将是一个奇怪的错误。
-
我认为它现在与系统相关(Windows 10 技术预览版)。所以代码与它无关,我发布的那个链接几乎总结了任何人都可以给出的唯一答案。我现在正在盲目开发......感觉就像旧的大型机蜗牛邮件打卡时代。我将尝试将文件移动到其他驱动器,可能尝试更改编码并重新安装 WMI。
标签: wpf xaml powershell dialog runtime-error