使用java swing命令按钮调用powershell脚本
我创建了一个登录窗口,其中有两个用户名和密码文本框以及两个登录和取消命令按钮。按下取消按钮后,窗口关闭。登录凭据应作为参数传递给powershell脚本以登录网站。。我无法链接powershell和java swing代码 我的java swing代码如下所示:使用java swing命令按钮调用powershell脚本,java,swing,powershell,Java,Swing,Powershell,我创建了一个登录窗口,其中有两个用户名和密码文本框以及两个登录和取消命令按钮。按下取消按钮后,窗口关闭。登录凭据应作为参数传递给powershell脚本以登录网站。。我无法链接powershell和java swing代码 我的java swing代码如下所示: import static java.awt.GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT; import javax.swing.*; import java.awt.*
import static java.awt.GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Credential extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public Credential() {
setUndecorated(true);
setBackground(new Color(0, 0, 0, 0));
setSize(new Dimension(400, 300));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (g instanceof Graphics2D) {
final int R = 220;
final int G = 220;
final int B = 250;
Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B,
0), 0.0f, getHeight(), new Color(R, G, B, 255),
true);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(p);
g2d.fillRect(0, 0, getWidth(), getHeight());
Font font = new Font("Serif", Font.PLAIN, 45);
g2d.setFont(font);
g2d.setColor(Color.lightGray);
g2d.drawString("Get Credential", 60, 80);
}
}
};
setContentPane(panel);
setLayout(new FlowLayout());
placeComponents(panel);
}
public static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("User");
userLabel.setBounds(40, 100, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(130, 100, 160, 25);
userText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel.add(userText);
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(40, 140, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(130, 140, 160, 25);
panel.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(100, 180, 80, 25);
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel.add(loginButton);
JButton cancelButton = new JButton("cancel");
cancelButton.setBounds(220, 180, 80, 25);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(cancelButton);
}
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
boolean isPerPixelTranslucencySupported = gd
.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
if (!isPerPixelTranslucencySupported) {
System.out.println("Per-pixel translucency is not supported");
System.exit(0);
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Credential gtw = new Credential();
gtw.setVisible(true);
}
});
}
}
powershell脚本如下所示:
$url = "https://www.jabong.com/customer/account/login/"
$username = $cred.username
$username = $username.Replace("\", "")
$password = $cred.GetNetworkCredential().password
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
while ($ie.Busy -eq $true)
{
Start-Sleep 1;
}
$ie.Document.getElementById("LoginForm_email").value = $username
$ie.Document.getElementByID("LoginForm_password").value=$password
$ie.Document.getElementByID("qa-login-button").Click();
有几种方法可以实现这一点。例如,您可以使用用户名和密码作为参数运行PowerShell脚本:
Process流程=新建ProcessBuilder(
“powershell.exe”,
“-File”,“C:\\path\\to\\your.ps1”,
“-Username”,userText.getText(),
“-Password”,passwordText.getText()
).start();
并使脚本的$Username
和$Password
:
[CmdletBinding()]
Param(
[参数()][string]$Username,
[参数()][string]$Password
)
$url='1https://www.jabong.com/customer/account/login/'
$ie=新对象-COM internetexplorer.application
...
不过,这是一种不好的做法,因为这样密码就会以明文形式出现在进程列表中
更好的选择是将用户名和密码传递为:
ProcessBuilder pb=新的ProcessBuilder(
“powershell.exe”,
“-File”,“C:\\path\\to\\your.ps1”
);
Map env=pb.environment();
put(“USER”,userText.getText());
put(“PASS”,passwordText.getText());
Process进程=pb.start();
可以在脚本中访问,如下所示:
$username=$env:USER
$password=$env:PASS
或者,您也可以用另一种方法,从PowerShell脚本运行Java程序:
$username,$password=&java-jar'.\Credential.jar'
并将凭据写入Java程序中的stdout:
System.out.println(userText.getText());
System.out.println(passwordText.getText());
请注意,无论哪种方法,您都需要创建类的userText
和passwordText
实例变量,并将placeComponents()
更改为非静态方法
话虽如此,如果Java/Swing不是一个硬需求,那么使用还可以提供一种构建自定义对话框的方法(允许您将对话框和脚本代码保存在同一个文件中):
添加类型-AssemblyName System.Windows.Forms
添加类型-AssemblyName System.Drawing
功能新建按钮($label、$action、$width、$height、$top、$left){
$btn=新建对象System.Windows.Forms.Button
$btn.Location=新对象系统.Drawing.Size($left,$top)
$btn.Size=新对象系统.Drawing.Size($width,$height)
$btn.Text=$label
$btn.添加\单击($action)
$btn
}
函数新标签($Label、$width、$height、$top、$left){
$lbl=新对象System.Windows.Forms.Label
$lbl.Location=新对象系统.Drawing.Size($left,$top)
$lbl.Size=新对象系统.Drawing.Size($width,$height)
$lbl.Text=$label
$lbl
}
函数新输入($width、$height、$top、$left、$mask=$false){
$input=新对象System.Windows.Forms.TextBox
$input.UseSystemPasswordChar=$mask
$input.Location=新对象系统.Drawing.Size($left,$top)
$input.Size=新对象系统.Drawing.Size($WITH、$height)
$input
}
$form=新对象System.Windows.Forms.form
$form.Size=新对象系统.绘图.尺寸(250160)
$form.Controls.Add((新标签'Username:'60 20 10))
$user=新输入150 20 70
$form.Controls.Add($user)
$form.Controls.Add((新标签“Password:”60 20 50 10))
$pass=新输入150 20 50 70$true
$form.Controls.Add($pass)
$form.Controls.Add((新按钮“OK”{$form.Close()}70 23 85 70))
$form.Controls.Add((新按钮“取消”{$user.Text='';$pass.Text='';$form.Close()}70 23 85 150))
[void]$form.ShowDialog()
$username=$user.Text
$password=$pass.Text
您的问题是什么?我需要通过java swing窗口输入凭据,按下登录按钮后,powershell代码应该运行。。用户名和密码swing窗口的值需要传递给powershell代码并调用网站的登录名。如何使用java代码调用powershell代码并将java参数传递给itLooks给我,就像java程序所做的一样,是提示用户输入其凭据。有什么特别的原因不能简单地在PowerShell脚本中使用并完全删除Java程序吗?是的,我的要求是创建一个用于登录的自定义swing窗口