从RunOnce运行PowerShell

从RunOnce运行PowerShell,powershell,registry,runonce,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"}}

我试图在模板自定义期间运行一组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命令将正确运行。我无法确定我的语法的哪一部分出现故障。

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

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

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

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

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

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

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

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

此外,在脚本中插入日志以捕获错误<代码>开始转录本可能会有所帮助。此外,在脚本中插入日志以捕获错误<代码>开始转录本可能会有所帮助。