Я хочу знать, как использовать флажок, чтобы показать или скрыть пароль в блоке паролей с powershell (с использованием WPF).Я нашел много примеров о C #, но я не могу заставить его работать с powershell.Как установить свойство paswordrevealmode в powershell на «скрытый» или «видимый»?
в c # это делается так:
private void CheckBox_Changed(object sender, RoutedEventArgs e)
{
if (revealModeCheckBox.IsChecked == true)
{
passwordBox1.PasswordRevealMode = PasswordRevealMode.Visible;
}
else
{
passwordBox1.PasswordRevealMode = PasswordRevealMode.Hidden;
}
}
я знаю, что здесь что-то не хватает,я все еще изучаю wpf с powershell :)
Код, с которым я работаю, следующий:
function Get-Info () {
[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="Informations requises" Height="161.4" Width="544.4">
<Grid>
<Label Name="label" Content="Est-ce que le serveur *** est local, saas ou cloud ?" HorizontalAlignment="Left" Margin="13,6,0,0" VerticalAlignment="Top" Foreground="#FF1843BA" FontWeight="Bold" Width="296" />
<Label Name="label1" Content="Mot de passe genere dans *** :" HorizontalAlignment="Left" Height="26" Margin="12,33,0,0" VerticalAlignment="Top" Width="245" RenderTransformOrigin="0.5,0.5" Foreground="#FF1843BA" FontWeight="Bold">
<Label.RenderTransform>
<TransformGroup>
<ScaleTransform />
<SkewTransform />
<RotateTransform Angle="0.035" />
<TranslateTransform />
</TransformGroup>
</Label.RenderTransform>
</Label>
<TextBox Name="tbIndex" HorizontalAlignment="Left" Height="23" Margin="94,62,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="203" />
<Label Name="label2" Content="Index Client:" HorizontalAlignment="Left" Margin="13,60,0,0" VerticalAlignment="Top" Foreground="#FF1843BA" FontWeight="Bold" />
<Label Name="label3" Content="Adresse IP:" HorizontalAlignment="Left" Margin="13,91,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.202,0.727" Foreground="#FF1843BA" FontWeight="Bold" />
<Label Name="label7" Content="DNS Restreint ?" HorizontalAlignment="Left" Margin="302,60,0,0" VerticalAlignment="Top" Foreground="#FF1843BA" FontWeight="Bold" />
<Button Name="btSave" Content="Enregistrer les informations" HorizontalAlignment="Left" Margin="326,91,0,0" VerticalAlignment="Top" Width="164" RenderTransformOrigin="0.501,-0.221" Height="23" />
<StackPanel Name="spServerType" Height="28" Margin="306,7,-18.8,0" Width="212" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Left">
<RadioButton Name="rbCloud" Content="Cloud" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="Serveur" Margin="5" />
<RadioButton Name="rbServerLocal" Content="Serveur Local" HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" Width="91" GroupName="Serveur" Margin="5" />
<RadioButton Name="rbSaas" Content="SaaS" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="Serveur" Margin="5" />
</StackPanel>
<StackPanel Name="spDNS" HorizontalAlignment="Left" Height="24" Margin="405,61,0,0" VerticalAlignment="Top" Width="101" Orientation="Horizontal">
<RadioButton Name="rbDnsYes" Content="Oui" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="DNS" Margin="5" />
<RadioButton Name="rbDnsNo" Content="Non" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="DNS" Margin="5" />
</StackPanel>
<TextBox Name="tbIP" HorizontalAlignment="Left" Height="24" Margin="94,96,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="204" />
<StackPanel Name="spPassword" Margin="254,32,8.4,67" Orientation="Horizontal">
<PasswordBox Name="pbPassword" Width="245" Margin="0,1,0,1" />
<CheckBox Name="revealModeCheckBox" Content="" IsChecked="False" ToolTip="Show password" Margin="6,6" />
</StackPanel>
</Grid>
</Window>
'@
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
## Item1
$revealModeCheckBox.Add_Checked({
#***this is the part i have trouble with***
$pbPassword.PasswordRevealMode = "PasswordRevealMode.Visible"
})
$btSave.Add_Click({
$global:hastable = @{}
#get the password as SecureString
$hastable.add( "Password", ($pbPassword.securePassword | ConvertFrom-SecureString))
$form.Close()
})
$Form.ShowDialog() | out-null
return $Global:hastable
}
get-info
Я пробовал несколько значений для свойства PasswordRevealMode, как показано ниже:
$revealModeCheckBox.Add_Checked({
#***this is the part i have trouble with***
$pbPassword.PasswordRevealMode = 1
})
или
$revealModeCheckBox.Add_Checked({
#***this is the part i have trouble with***
$pbPassword.PasswordRevealMode = $true
})
кажется, что это неправильный способ установить это свойство в powershell .. я получил следующие ошибки при этом:
The property 'PasswordRevealMode' cannot be found on this object. Verify that the property exists and can be set.
At C:\git\temp\Samplescript.ps1:54 char:5
+ $pbPassword.PasswordRevealMode = "PasswordRevealMode.Visible"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
или
The property 'PasswordRevealMode' cannot be found on this object. Verify that the property exists and can be set.
At C:\git\temp\Samplescript.ps1:54 char:5
+ $pbPassword.PasswordRevealMode = 1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
или
The property 'PasswordRevealMode' cannot be found on this object. Verify that the property exists and can be set.
At C:\git\temp\Samplescript.ps1:54 char:5
+ $pbPassword.PasswordRevealMode = $true
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
фактический графический интерфейс выглядит так:
я нашелОбойти использование всплывающего сообщения, чтобы показать пароль, но я пытаюсь показать его, используя passwordBox, как описано здесь:
MSDN
Спасибо взаранее!