Skip to content

Commit

Permalink
Merge pull request #119 from theaquamarine/pscredential
Browse files Browse the repository at this point in the history
Connect console with PSCredential
  • Loading branch information
rzander authored Aug 20, 2019
2 parents 45a9258 + 9a8b459 commit 1a106ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 71 deletions.
6 changes: 3 additions & 3 deletions SCCMCliCtrWPF/SCCMCliCtrWPF/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</LinearGradientBrush>
</DockPanel.Background>
<TextBlock Text="Target Computer: " VerticalAlignment="Center" Margin="15,0,0,0"></TextBlock>
<Controls:AutoCompleteBox Name="tb_TargetComputer2" Width="150" Height="25" Text="{Binding Source={x:Static p:Settings.Default}, Path=DefaultHostName, Mode=TwoWay}" KeyUp="tb_TargetComputer2_KeyUp" Populating="tb_TargetComputer2_Populating" TextChanged="tb_TargetComputer_TextChanged" />
<Controls:AutoCompleteBox Name="tb_TargetComputer2" Width="150" Height="25" Text="{Binding Source={x:Static p:Settings.Default}, Path=DefaultHostName, Mode=TwoWay}" Populating="tb_TargetComputer2_Populating" TextChanged="tb_TargetComputer_TextChanged" />
<Button Name="bt_Ping" Width="20" Height="20" Visibility="Hidden" Click="bt_Ping_Click" ToolTip="Ping the device...">
<StackPanel>
<Image Source="Images/Play.ico" />
Expand Down Expand Up @@ -51,7 +51,7 @@
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Password:" Height="28" HorizontalAlignment="Left" VerticalAlignment="Top" Width="70" />
<PasswordBox Height="23" HorizontalAlignment="Left" Name="pb_Password" VerticalAlignment="Top" Width="140" KeyDown="pb_Password_KeyDown" />
<PasswordBox Height="23" HorizontalAlignment="Left" Name="pb_Password" VerticalAlignment="Top" Width="140"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Port:" Height="28" HorizontalAlignment="Left" VerticalAlignment="Top" Width="80" />
Expand All @@ -68,7 +68,7 @@

<StackPanel Orientation="Horizontal" Height="30" VerticalAlignment="Center" Focusable="false">
<TextBlock Text="Target Computer: " VerticalAlignment="Center"></TextBlock>
<Controls:AutoCompleteBox Name="tb_TargetComputer" Text="127.0.0.1" Width="150" Height="25" Populating="tb_TargetComputer2_Populating" KeyUp="tb_TargetComputer_KeyUp" TextChanged="tb_TargetComputer2_TextChanged"/>
<Controls:AutoCompleteBox Name="tb_TargetComputer" Width="150" Height="25" Populating="tb_TargetComputer2_Populating" TextChanged="tb_TargetComputer2_TextChanged" Text="{Binding Source={x:Static p:Settings.Default}, Path=DefaultHostName, Mode=TwoWay}"/>
<RibbonButton Name="bt_Connect" VerticalAlignment="Center" Label="Connect" Height="23" Click="bt_Connect_Click" />
</StackPanel>
<RibbonApplicationMenuItem Header="Console extensions..." StaysOpenOnClick="True" Focusable="false">
Expand Down
87 changes: 19 additions & 68 deletions SCCMCliCtrWPF/SCCMCliCtrWPF/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public partial class MainPage : Window
{
public SCCMAgent oAgent;
public MyTraceListener myTrace;
private bool bPasswordChanged = false;
delegate void AnonymousDelegate();

public MainPage()
Expand Down Expand Up @@ -265,9 +264,6 @@ public MainPage()
}
catch { }

pb_Password.Password = Properties.Settings.Default.Password;
tb_Username.Text = Properties.Settings.Default.Username;

Common.Hostname = tb_TargetComputer.Text.Trim();

try
Expand Down Expand Up @@ -319,7 +315,9 @@ public MainPage()
if (!string.IsNullOrEmpty(sUser) && !string.IsNullOrEmpty(sPW))
{
tb_Username.Text = sUser;
pb_Password.Password = common.Encrypt(sPW, Application.ResourceAssembly.ManifestModule.Name);
Properties.Settings.Default.Username = tb_Username.Text; //Binding is not yet syncing these, so do it manually.
pb_Password.Password = sPW; //Not ideal but works
//pb_Password.SecurePassword is read-only so another variable with one-way binding is required.
sUser = "";
sPW = "";
}
Expand Down Expand Up @@ -472,15 +470,7 @@ private void bt_Connect_Click(object sender, RoutedEventArgs e)
PageReset();

}

if (bPasswordChanged)
{
Properties.Settings.Default.Password = common.Encrypt(pb_Password.Password, Application.ResourceAssembly.ManifestModule.Name);
Properties.Settings.Default.Save();
pb_Password.Password = Properties.Settings.Default.Password;
bPasswordChanged = false;
}


tb_TargetComputer.Text = tb_TargetComputer.Text.Trim();
tb_TargetComputer2.Text = tb_TargetComputer2.Text.Trim();

Expand All @@ -506,7 +496,7 @@ private void bt_Connect_Click(object sender, RoutedEventArgs e)
{
if (sTarget != "127.0.0.1")
{
if (string.IsNullOrEmpty(tb_Username.Text) | string.IsNullOrEmpty(pb_Password.Password))
if (string.IsNullOrEmpty(tb_Username.Text) | pb_Password.SecurePassword.Length == 0)
{
MessageBox.Show("connecting an IP Address requires Username and Password", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
}
Expand All @@ -527,7 +517,8 @@ private void bt_Connect_Click(object sender, RoutedEventArgs e)
{
tb_Username.Text = Environment.UserDomainName + @"\" + tb_Username.Text;
}
string sPW = common.Decrypt(pb_Password.Password, Application.ResourceAssembly.ManifestModule.Name);
//Hack to handle switing over to securestrings while sccmclictrlib only takes strings. Obviously this defeats the point of SecureStrings.
string sPW = new System.Net.NetworkCredential(string.Empty, pb_Password.SecurePassword).Password;
oAgent = new SCCMAgent(sTarget, tb_Username.Text, sPW, int.Parse(tb_wsmanport.Text), false, cb_ssl.IsChecked ?? false);
sPW = "";
}
Expand Down Expand Up @@ -806,26 +797,22 @@ private void btOpenPSConsole_Click(object sender, RoutedEventArgs e)
{
Process Explorer = new Process();
Explorer.StartInfo.FileName = "powershell.exe";
string sCred = "";
string sPS = string.Format(Properties.Settings.Default.OpenPSConsoleCommand, oAgent.TargetHostname, tb_wsmanport.Text);
if (!string.IsNullOrEmpty(tb_Username.Text))
{
sCred = string.Format("$creds = New-Object System.Management.Automation.PSCredential ('{0}', (ConvertTo-SecureString '{1}' -AsPlainText -Force))", tb_Username.Text, common.Decrypt(pb_Password.Password, Application.ResourceAssembly.ManifestModule.Name)) + ";";


if ((bool)cb_ssl.IsChecked)
Explorer.StartInfo.Arguments = @"-NoExit -Command " + sCred + sPS + " -UseSSL -Credential $creds)";
else
Explorer.StartInfo.Arguments = @"-NoExit -Command " + sCred + sPS + " -Credential $creds";
if ((bool)cb_ssl.IsChecked) sPS += " -UseSSL";
Explorer.StartInfo.Arguments = @"-NoExit -Command " + sPS;

}
else
if (!string.IsNullOrEmpty(tb_Username.Text))
{
if ((bool)cb_ssl.IsChecked)
Explorer.StartInfo.Arguments = @"-NoExit -Command " + sPS + " -UseSSL";
else
Explorer.StartInfo.Arguments = @"-NoExit -Command " + sPS;
System.Management.Automation.PSCredential credential = new System.Management.Automation.PSCredential(tb_Username.Text, pb_Password.SecurePassword);
string serializedCred = System.Management.Automation.PSSerializer.Serialize(credential);
string filename = System.IO.Path.GetTempFileName();
File.WriteAllText(filename, serializedCred);
string creds = "(Import-Clixml " + filename + ")";
//creds += "; rm " + filename;

Explorer.StartInfo.Arguments += " -Credential " + creds;
}

Explorer.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

Explorer.Start();
Expand Down Expand Up @@ -869,11 +856,6 @@ private void tvSWDist_Loaded(object sender, RoutedEventArgs e)
tviSWDistOverview.IsSelected = true;
}

private void pb_Password_KeyDown(object sender, KeyEventArgs e)
{
bPasswordChanged = true;
}

private void btClientMachineAuthentication_Click(object sender, RoutedEventArgs e)
{
Mouse.OverrideCursor = Cursors.Wait;
Expand Down Expand Up @@ -1108,22 +1090,6 @@ private void tb_TargetComputer_TextChanged(object sender, RoutedEventArgs e)
Common.Hostname = ((AutoCompleteBox)sender).Text;
}

private void tb_TargetComputer2_KeyUp(object sender, KeyEventArgs e)
{
Mouse.OverrideCursor = Cursors.Wait;
if (e.Key == Key.Enter)
{
tb_TargetComputer2.Text = tb_TargetComputer2.Text.Trim();
tb_TargetComputer.Text = tb_TargetComputer2.Text;
bt_Connect_Click(sender, null);
}

Common.Hostname = ((AutoCompleteBox)sender).Text;

Mouse.OverrideCursor = Cursors.Arrow;

}

private void tb_TargetComputer2_Populating(object sender, PopulatingEventArgs e)
{
try
Expand All @@ -1136,21 +1102,6 @@ private void tb_TargetComputer2_Populating(object sender, PopulatingEventArgs e)
catch { }
}

private void tb_TargetComputer_KeyUp(object sender, KeyEventArgs e)
{
Mouse.OverrideCursor = Cursors.Wait;
if (e.Key == Key.Enter)
{
tb_TargetComputer.Text = tb_TargetComputer.Text.Trim();
tb_TargetComputer2.Text = tb_TargetComputer.Text;
bt_Connect_Click(sender, null);
}

Common.Hostname = ((AutoCompleteBox)sender).Text;

Mouse.OverrideCursor = Cursors.Arrow;
}

private void bt_Ping_Click(object sender, RoutedEventArgs e)
{
try
Expand Down

0 comments on commit 1a106ef

Please sign in to comment.