我基本上想要使用powershell并获得像
这样的控制台用户“查询会话| findstr console | awk'{print $ 2}'”
但不使用awk,但我无法使用它。
>console joe 2 Active
$ out看起来像:
ole joe 2 Active
$ consoleuser最终成为:
package com.addict.secret;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseGeoPoint;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
import de.hdodenhof.circleimageview.CircleImageView;
public class SignUp extends AppCompatActivity {
ProgressDialog login;
ProgressDialog progress;
EditText emailTxt, usernameTxt, passwordTxt, passwordConfirmTxt;
Button registerBtn, termsBtn, aboutBtn;
CheckBox mcheckbox;
ParseUser User = new ParseUser();
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
Toolbar toolbar = (Toolbar) findViewById(R.id.appBar_reset);
toolbar.setTitle(R.string.title_activity_sign_up);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.icon_back_arrow);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabprof);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder(SignUp.this, R.style.AppCompatAlertDialogStyle)
.setIcon(R.drawable.ic_new_releases_black_24dp)
.setTitle(R.string.comingsoontitle)
.setMessage(getResources().getString(R.string.comingsoon))
.setNegativeButton(android.R.string.ok, null)
.create().show();
}
});
//termsBtn = (Button) findViewById(R.id.termsBtn);
//termsBtn.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// TermsDialogFragment dialog = TermsDialogFragment.newInstance();
// dialog.show(getSupportFragmentManager(), "DialogFragment");
// }
// });
//aboutBtn = (Button) findViewById(R.id.aboutBtn);
//aboutBtn.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// startActivity(new Intent(SignUp.this, About.class));
// }
// });
emailTxt = (EditText) findViewById(R.id.email);
usernameTxt = (EditText) findViewById(R.id.Username);
passwordTxt = (EditText) findViewById(R.id.password);
passwordConfirmTxt = (EditText) findViewById(R.id.passwordConfirm);
registerBtn = (Button) findViewById(R.id.registerBtn);
//Snackbar.make(context,getResources().getString(R.string.accept_notification);
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailTxt.getText().toString();
String username = usernameTxt.getText().toString();
String password = passwordTxt.getText().toString();
String passwordConfirm = passwordConfirmTxt.getText().toString();
Log.d("email", email);
Log.d("username", username);
Log.d("password", password);
Log.d("password confirm", passwordConfirm);
if (username.equals("")){
Toast.makeText(SignUp.this, R.string.signup_error_message_username, Toast.LENGTH_SHORT).show();
}
else if (email.equals("")){
Toast.makeText(SignUp.this, R.string.signup_error_message_email, Toast.LENGTH_SHORT).show();
}
else if (password.equals("") || password.length() < 6){
Toast.makeText(SignUp.this, R.string.signup_error_message_password6, Toast.LENGTH_SHORT).show();
}
else if (!passwordConfirm.equals(password)){
Toast.makeText(SignUp.this, R.string.signup_error_message_password, Toast.LENGTH_SHORT).show();
}
else {
progress = new ProgressDialog(SignUp.this, R.style.AppCompatAlertDialogStyle);
progress.setTitle(R.string.signup_dialog_title);
progress.setMessage(getResources().getString(R.string.signup_message));
progress.setCanceledOnTouchOutside(false);
progress.show();
registration(username, email, password);
}
}
});
}
private void registration(final String username, String email, final String password){
ParseGeoPoint currentLocation = new ParseGeoPoint(49.2827, -123.1207);;
User.setUsername(username);
User.put("status", getResources().getString(R.string.default_status));
User.setEmail(email);
User.setPassword(password);
User.put("Location", currentLocation);
User.signUpInBackground(new SignUpCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
progress.dismiss();
Toast.makeText(SignUp.this, R.string.success_signup, Toast.LENGTH_SHORT).show();
signInUser(username, password);
SignUp.this.finish();
} else {
progress.dismiss();
final Dialog dialog = new Dialog(SignUp.this, R.style.AppCompatAlertDialogStyle);
dialog.setContentView(R.layout.dialog_error);
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.message);
text.setText(R.string.signup_error_parse);
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.error_fox);
Button dialogButton = (Button) dialog.findViewById(R.id.ok);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
});
}
private void signInUser(String username, String password){
login = new ProgressDialog(SignUp.this, R.style.AppCompatAlertDialogStyle);
login.setTitle(R.string.login_title);
login.setMessage(getResources().getString(R.string.login_message));
login.setCanceledOnTouchOutside(false);
login.show();
ParseUser.logInInBackground(username, password, new LogInCallback() {
@Override
public void done(ParseUser parseUser, ParseException e) {
if (e == null) {
login.dismiss();
Toast.makeText(SignUp.this, R.string.success_login, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SignUp.this, MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(SignUp.this, R.string.error_login, Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_sign_up, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
//
// //noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:3)
.Split()
是.Net字符串方法,它不使用正则表达式。 -split
是PowerShell运算符并使用正则表达式。
呼叫findstr
是可行的,但是没有必要让PS使用它。 e.g。
$out = query session | Where {$_ -match 'console'}
$consoleuser = ($out -split '\s+')[1]
((query session) -match 'console' -split '\s+')[1]
答案 1 :(得分:3)
正如其他人建议尝试以下
$out = query session | findstr console
$consoleuser = $($out -split('\s+'))[1]
或者你可以试试
$consoleuser = $ENV:username
答案 2 :(得分:1)
通过进一步优化来补充TessellatingHeckler's helpful answer(但请注意armorall171's helpful recommendation只需使用$env:USERNAME
即可:
(-split ((query session) -match '^>console'))[1]
来自外部命令query session
的输出由PowerShell作为字符串的数组返回,-match
运算符将该数组过滤为仅匹配的元素(在这种情况下只有1行)。
-split
运算符具有一元形式,其行为类似于awk
的默认字段解析行为强>:
它通过运行空格将输入拆分为数组元素,忽略前导和尾随空格。
示例:
> -split " ab `t `t cde `n `n efgh "
ab
cde
efgh
答案 3 :(得分:0)