Hi Everyone!
I wrote a simple program with a bunch of code (say hello to UI). Here the contents:
Add-Type –assemblyName PresentationFramework
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = "STA"
$Runspace.ThreadOptions = "ReuseThread"
$Runspace.Open()
$code = {
#Build the GUI
[xml]$xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="SwitchOver AutoMation Tool" Height="600" Width="480" WindowStartupLocation = "CenterScreen" ResizeMode="NoResize">
<Grid Margin="0,0,0,-1">
<Button x:Name="Start_Switchover" Content="Start SwitchOver" HorizontalAlignment="Left" Margin="50,100,0,0" VerticalAlignment="Top" Height="31" Width="200" FontSize="14"/>
<TextBox x:Name="Servers" HorizontalAlignment="Left" Margin="70,62,0,0" VerticalAlignment="Top" TextWrapping="Wrap" Text="Enter server names here..." Height="23" Width="150" Foreground="DarkGray"/>
<TextBox x:Name="Process_Output" HorizontalAlignment="Left" Margin="10,150,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="400" Width="440" FontSize="11" IsReadOnly = "True" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" />
<Label Content="Servers List:" HorizontalAlignment="Left" Margin="100,30,0,0" VerticalAlignment="Top" Height="26" Width="97" FontSize="14"/>
<Label Content="SwitchOver Automation" HorizontalAlignment="Left" Margin="30,0,0,0" VerticalAlignment="Top" Width="338" FontSize="18" FontWeight="Bold"/>
</Grid>
</Window>
"@
$syncHash = [hashtable]::Synchronized(@{ })
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Window = [Windows.Markup.XamlReader]::Load($reader)
function RunspaceScript
{
param($syncHash, $Servers, $TargetBox)
if (!$Servers -or $Servers -eq "Enter server names here...")
{
NullValue
}
else
{
$syncHash.Host = $host
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = "STA"
$Runspace.ThreadOptions = "ReuseThread"
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable("syncHash", $syncHash)
$Runspace.SessionStateProxy.SetVariable("Servers", $Servers)
$Runspace.SessionStateProxy.SetVariable("TargetBox", $TargetBox)
$code = {
Function Add-OutputBoxLine
{
Param ($Message)
$syncHash.Window.Dispatcher.invoke(
[action]{
$syncHash.$TargetBox.AppendText("$Message")
$syncHash.$TargetBox.Refresh()
$syncHash.$TargetBox.ScrollToCaret()
}
)
}
$syncHash.host.ui.PromptForCredential('Windows PowerShell credential request', 'Enter your credentials.',"","")
$syncHash.Window.Dispatcher.invoke(
[action]{ $syncHash.$TargetBox.Clear(); $syncHash.Start_Switchover.IsEnabled = $False })
Add-OutputBoxLine -Message "This is a test information block!"
$syncHash.Window.Dispatcher.invoke(
[action]{ $syncHash.$TargetBox.ScrollToEnd(); $syncHash.Start_Switchover.IsEnabled = $True }
)
}
$PSinstance = [powershell]::Create().AddScript($Code)
$PSinstance.Runspace = $Runspace
$PSinstance.Runspace.Name = "SwitchOver"
$job = $PSinstance.BeginInvoke()
$Runspaces = Get-Runspace -Name "SwitchOver"
foreach ($item in $Runspaces)
{
if ($item.RunspaceAvailability -ne "Busy")
{
$item.close()
$item.dispose()
}
}
}
}
function NullValue
{
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
[Microsoft.VisualBasic.Interaction]::MsgBox("Please enter server names serapated by comma under which you would like to perform switchover operation", 'OKOnly,Information', "Information")
}
# XAML objects
# ComputerNames
$syncHash.Servers = $syncHash.Window.FindName("Servers")
# Ping buttons
$syncHash.Start_Switchover = $syncHash.Window.FindName("Start_Switchover")
# Result boxes
$syncHash.Process_Output = $syncHash.Window.FindName("Process_Output")
$syncHash.Servers.Add_GotFocus({
if ($syncHash.Servers.Text -eq "Enter server names here...")
{
$syncHash.Servers.Foreground = "Black"
$syncHash.Servers.Text = ""
}
})
$syncHash.Servers.Add_LostFocus({
if (!$syncHash.Servers.Text)
{
$syncHash.Servers.Foreground = 'DarkGray'
$syncHash.Servers.Text = "Enter server names here..."
}
})
#$syncHash.Window.Add_Closed({
#[System.Windows.Forms.Application]::Exit(); Stop-Process $pid
#})
# Click Actions
$syncHash.Start_Switchover.Add_Click(
{
RunspaceScript -syncHash $syncHash -Servers $syncHash.Servers.Text -TargetBox "Process_Output"
})
$syncHash.Window.ShowDialog()
$Runspace.Close()
$Runspace.Dispose()
}
$PSinstance1 = [powershell]::Create().AddScript($Code)
$PSinstance1.Runspace = $Runspace
$job = $PSinstance1.BeginInvoke()
### Close Runspaces
$Runspaces = Get-Runspace
foreach ($item in $Runspaces)
{
if ($item.RunspaceAvailability -ne "Busy")
{
$item.close()
$item.dispose()
}
}
The problem is when i click the button a window should pop up asking me to enter the credentials, but nothing happens. I suspect this is because command “$syncHash.host.ui.PromptForCredential(‘Windows PowerShell credential request’, ‘Enter your credentials.’,”“,”“)” are executed inside a runspace.
Any ideas?