【问题标题】:Running PowerShell from RunOnce从 RunOnce 运行 PowerShell
【发布时间】:2012-11-15 00:26:37
【问题描述】:

我正在尝试在模板自定义期间运行一组 powershell 命令。这些命令被注入到注册表中的 RunOnce。

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command &{Add-Type -Assembly System.Web; [Web.Security.Membership]::GeneratePassword(21,7) | Select-Object @{N="Group";E={"Test Group"}}, @{N="Username";E={"TestUser"}}, @{N="Password";E={$_}} | Export-Csv -Path C:\stuff\user.csv -NoTypeInformation}

我知道这些命令有效,因为我可以在 powershell 控制台中运行它们。我知道 "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command &{PSCODE}" 是正确的,因为其他 runonce 命令将正确运行。我无法弄清楚我的语法的哪一部分失败了。

【问题讨论】:

    标签: powershell registry runonce


    【解决方案1】:

    这可能是一个漫长的试错环节,为了节省您宝贵的时间,我建议您使用脚本文件而不是那个冗长的命令:

    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File c:\script.ps1
    

    【讨论】:

    • 此外,将日志记录插入脚本以捕获错误。 Start-Transcript 可能会有所帮助。
    【解决方案2】:

    只是想补充一点,为了运行一系列命令,您必须在不带引号的情况下运行它。至少只有这样对我有用:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit Import-Module Hyper-V; Get-VMSwitch \"*Virtual*\" | Remove-VMSwitch -Force; New-VMSwitch -NetAdapterName \"Ethernet\" -Name \"VMNet\"
    

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 2013-09-19
      • 2014-03-18
      • 2017-10-14
      • 1970-01-01
      • 2022-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多