从RunOnce运行PowerShell

时间:2012-11-15 00:26:37

标签: powershell registry runonce

我正在尝试在模板自定义期间运行一组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命令将正确运行。我无法弄清楚我的语法的哪个部分失败了。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

只想添加,为了运行一系列命令,你必须在没有引号的情况下运行它。至少只有这样才对我有用:

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\"