AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 696660
Accepted
MarcLaf
MarcLaf
Asked: 2015-06-05 07:08:02 +0800 CST2015-06-05 07:08:02 +0800 CST 2015-06-05 07:08:02 +0800 CST

如何组合两个foreach循环?

  • 772

我已经研究了如何将多个命令与 psexec 结合使用,但似乎没有一种方法有效。也许是因为我在 PowerShell 而不是 cmd.exe 中运行脚本?

我需要创建用户帐户并将它们添加到非域计算机(Windows 2003)上的本地管理员组。我已经派生了以下脚本(有效),但我想知道是否有一种简单的方法可以组合两个 foreach 循环,因为它必须为每个服务器建立两次 psexec 连接。

$username = Read-Host "User name:"
$password = Read-Host "Password:"
$fullname = Read-Host "Full name:"
$computers = Get-Content C:\Tools\scripts\ServerList.txt

foreach ($computer in $computers) {
    psexec \\$computer -u username -p password net user $username $password /add /fullname:""$fullname"" /comment:"Comment"
}

foreach ($computer in $computers) {
    psexec \\$computer -u username -p password net localgroup Administrators $username /add
}

正如我所提到的,这是在 PowerShell 中运行的。我运行它的服务器是未安装 PowerShell 的 Workgroup Windows 2003 Servers。

windows-server-2003
  • 1 1 个回答
  • 1122 Views

1 个回答

  • Voted
  1. Best Answer
    briantist
    2015-06-05T09:03:32+08:002015-06-05T09:03:32+08:00

    正如@CIA 所说,您可以通过psexec在一个循环中运行两次来组合这些循环。

    $username = Read-Host "User name:"
    $password = Read-Host "Password:"
    $fullname = Read-Host "Full name:"
    $computers = Get-Content C:\Tools\scripts\ServerList.txt
    
    foreach ($computer in $computers) {
        psexec \\$computer -u username -p password net user $username $password /add /fullname:""$fullname"" /comment:"Comment"
        psexec \\$computer -u username -p password net localgroup Administrators     
    }
    

    但似乎您真正要问的是如何在单个会话中运行两个net 命令。psexec

    $username = Read-Host "User name:"
    $password = Read-Host "Password:"
    $fullname = Read-Host "Full name:"
    $computers = Get-Content C:\Tools\scripts\ServerList.txt
    
    foreach ($computer in $computers) {
        psexec \\$computer -u username -p password net user $username $password ullname"" /comment:"Comment" ^&^& net localgroup Administrators     
    }
    

    您可以尝试这种方式,但我不确定它是否会起作用。使用&&转义,^以便将其传递给psexec而不是在本地解释,然后在net本地运行第二个命令。

    选择

    相反,您可以做的是根本不使用psexec并使用 PowerShell 远程处理。只需在您要远程访问的每台机器上启用它。我意识到这可能是一项艰巨的任务,但它是值得的,因为它更加通用,而且基本上,这是未来。

    如果您在域中,您甚至可以使用组策略来启用 PowerShell 远程处理(完全披露:这是我的文章)。

    如果这样做,您的代码将如下所示:

    $username = Read-Host "User name:"
    $password = Read-Host "Password:"
    $fullname = Read-Host "Full name:"
    $computers = Get-Content C:\Tools\scripts\ServerList.txt
    
    foreach ($computer in $computers) {
        Invoke-Command -ComputerName $computer -ArgumentList $username,$password,$fullname -ScriptBlock { param($u,$p,$f)
            # Everything in here is executed on the remote computer
            net user $u $p /add /fullname:""$f"" /comment:"Comment"
            net localgroup Administrators     
        }
    }
    
    • 3

相关问题

  • 在域加入时将“工作站管理员”添加到本地管理员组

  • 如何轻松地将 DFS 共享移动到新驱动器?

  • 服务器 2003 R2 上的 Cisco VPN 客户端

  • 通过 VPN 更改 Active Directory 密码

  • 如何从 ISA 服务器后面安装 magento 扩展?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve