如何在Powershell中自动回答“是”?

时间:2012-12-28 14:31:14

标签: powershell

如何在PowerShell会话中输入“是”作为交互式问题的答案?我知道,在Bash中,Yes是在提示符上回答“是”的工具。在我的情况下,我无法抑制提示。

我正在运行的脚本停在

  

如果您想继续,请回复“是”:

powershell如何运行脚本并在提示时“回答”是?

7 个答案:

答案 0 :(得分:4)

您可以测试这些旧时尚方式:

Cmd /c "GpUpdate.exe /FORCE <C:\temp\response.txt"

ECHO 'Y' | GpUpdate.exe /FORCE

Get-Content "C:\temp\response.txt" | GpUpdate.exe /FORCE

答案 1 :(得分:4)

使用ECHO为我工作。我在我的Powershell脚本中运行RSKeyMgmt.exe,它提示是(Y)/否(N)。所以当我做的时候

ECHO Y | RSKeyMgmt.exe...

它没有提示问题,命令执行正确。

所以我认为ECHO 'someoption'也适用于其他案例。

答案 2 :(得分:3)

我也遇到了同样的问题。 我尝试的解决方案包括:

  • ECHO'Y'|
  • 命令名称-强制
  • $ ConfirmPreference ='无'

但是,他们似乎都没有做到这一点。

最终解决问题的是:

Powershell-Cmdlet -Confirm:$false

在命令执行期间,它将取消所有确认提示,并且将在不进行任何确认的情况下处理命令。

答案 3 :(得分:1)

受其他答案的启发,这是我的示例:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: DataTableDemo(),
    );
  }
}



class User {
  String firstName;
  String lastName;

  User({this.firstName, this.lastName});

  static List<User> getUsers() {
    return <User>[
      User(firstName: "Aaryan", lastName: "Shah"),
      User(firstName: "Ben", lastName: "John"),
      User(firstName: "Carrie", lastName: "Brown"),
      User(firstName: "Deep", lastName: "Sen"),
      User(firstName: "Emily", lastName: "Jane"),
    ];
  }
}

class DataTableDemo extends StatefulWidget {
  DataTableDemo() : super();

  final String title = "Data Table Flutter Demo";

  @override
  DataTableDemoState createState() => DataTableDemoState();
}

class DataTableDemoState extends State<DataTableDemo> {
  List<User> users;
  List<User> selectedUsers;
  bool sort;

  @override
  void initState() {
    sort = false;
    selectedUsers = [];
    users = User.getUsers();
    super.initState();
  }

  onSortColum(int columnIndex, bool ascending) {
    if (columnIndex == 0) {
      if (ascending) {
        users.sort((a, b) => a.firstName.compareTo(b.firstName));
      } else {
        users.sort((a, b) => b.firstName.compareTo(a.firstName));
      }
    }
  }

  onSelectedRow(bool selected, User user) async {
    setState(() {
      if (selected) {
        selectedUsers.add(user);
      } else {
        selectedUsers.remove(user);
      }
    });
  }

  deleteSelected() async {
    setState(() {
      if (selectedUsers.isNotEmpty) {
        List<User> temp = [];
        temp.addAll(selectedUsers);
        for (User user in temp) {
          users.remove(user);
          selectedUsers.remove(user);
        }
      }
    });
  }

  SingleChildScrollView dataBody() {
    return SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: DataTable(
        sortAscending: sort,
        sortColumnIndex: 0,
        columns: [
          DataColumn(
              label: Text("FIRST NAME"),
              numeric: false,
              tooltip: "This is First Name",
              onSort: (columnIndex, ascending) {
                setState(() {
                  sort = !sort;
                });
                onSortColum(columnIndex, ascending);
              }),
          DataColumn(
            label: Text("LAST NAME"),
            numeric: false,
            tooltip: "This is Last Name",
          ),
        ],
        rows: users
            .map(
              (user) => DataRow(
              selected: selectedUsers.contains(user),
              onSelectChanged: (b) {
                print("Onselect");
                onSelectedRow(b, user);
              },
              cells: [
                DataCell(
                  Text(user.firstName),
                  onTap: () {
                    print('Selected ${user.firstName}');
                  },
                ),
                DataCell(
                  Text(user.lastName),
                ),
              ]),
        )
            .toList(),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        verticalDirection: VerticalDirection.down,
        children: <Widget>[
          Expanded(
            child: dataBody(),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.all(20.0),
                child: OutlineButton(
                  child: Text('SELECTED ${selectedUsers.length}'),
                  onPressed: () {},
                ),
              ),
              Padding(
                padding: EdgeInsets.all(20.0),
                child: OutlineButton(
                  child: Text('DELETE SELECTED'),
                  onPressed: selectedUsers.isEmpty
                      ? null
                      : () {
                    deleteSelected();
                  },
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

答案 4 :(得分:0)

以下 Powershell 对我有用:

Write-Output 'Y' | .\plink.exe <ip_address> -l <username> -pw <password>

这会将“Y”字符输出到下一个管道对象。

答案 5 :(得分:0)

我在尝试使用时遇到了同样的问题:

net use * /delete

它总是促使我选择 Y/N 并尝试了这里的解决方案,但没有奏效,所以我报告了对我有用的方法:

net use * /delete /y

也许每个命令都有一个无声确认。

答案 6 :(得分:-1)

我试图杀死一个讨厌的启动程序,发现Stop-Process和/或该程序将不接受管道输入(对我来说没有Echo Y |!),但是我更改了JP的回答:Stop-进程名[program.name] -Force;以管理员身份运行,它像一种魅力。