我的项目是编写一个脚本,其中包含将在PowerShell中显示的XAML表单。
我有两个脚本文件。 FirstScript.ps1
,其代码如下:
# Required to load the XAML form and create the Powershell Variables
.\LoadDialog.ps1 -XamlPath '.\ADMgmtForm.xaml'
Write-Output "Hello, World!"
Write-Output ""
我的第二个脚本文件LoadDialogue.ps1
包含以下代码:
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$XamlPath
)
[xml]$Global:xmlWPF = Get-Content -Path $XamlPath
# 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 the XAML reader using a new XML node reader
$Global:xamGUI = [Windows.Markup.XamlReader]::Load((new-object
System.Xml.XmlNodeReader $xmlWPF))
# Create hooks to each named object in the XAML
$xmlWPF.SelectNodes("//*[@Name]") | %{
Set-Variable -Name ($_.Name) -Value $xamGUI.FindName($_.Name) -Scope Global
}
我的表单文件包含以下代码:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="0,0,61,0">
<Label Name="Label1" Content="Label" HorizontalAlignment="Left"
Margin="68,38,0,0" VerticalAlignment="Top" Width="197"/>
<Button Name="DispGrps" Content="Button" HorizontalAlignment="Left"
Margin="49,251,0,0" VerticalAlignment="Top" Width="124" Height="41"
FontWeight="Bold"/>
<Button Name="SelectGrps" Content="Button" HorizontalAlignment="Left"
Margin="301,251,0,0" VerticalAlignment="Top" Width="106" Height="41"
FontWeight="Bold"/>
</Grid>
</Window>
当我在PowerShell中调用FirstScript.ps1
时,我看到“Hello World”的输出,但我也收到此错误消息:
PS C:\Users\myusernamehere\Desktop\ADMgmtProject\Scripts> .\FirstScript.ps1
.\LoadDialog.ps1 : The term '.\LoadDialog.ps1' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and
try again.
At C:\Users\myusernamehere\Desktop\ADMgmtProject\Scripts\FirstScript.ps1:2
char:1
+ .\LoadDialog.ps1 -XamlPath '.\ADMgmtForm.xaml'
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\LoadDialog.ps1:String) [],
CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Hello, World!
我尝试初始化. .\FirstScript.ps1
并收到相同的错误消息。我尝试在&""
脚本中LoadDialogue
之前添加LoadDialogue.ps1
。
有人可以帮忙吗?