2012-11-15 36 views
1

我试图在模板定制期间运行一组powershell命令。这些命令被注入注册表中的RunOnce。从RunOnce运行PowerShell

%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命令将正确运行。我无法弄清楚我的语法的哪一部分是失败的。

回答

1

这可能是一个长期的试验和错误的会话,为您节省宝贵的时间,我建议你使用一个脚本文件,而不是那么长的命令:

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

另外,在脚本中插入日志记录来捕获错误。 “开始 - 成绩单”可能会有所帮助。 – vonPryz

+0

从脚本文件运行。不知道为什么。 – pizzim13

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