简体   繁体   中英

Running PowerShell from RunOnce

I am trying to run a set of powershell commands during a template customization. These commands are injected into the RunOnce in the registry.

%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}

I know that these commands work, as I can run them in a powershell console. I know that "%SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -command &{PSCODE}" is correct because other runonce commands will run correctly. I cannot figure out what part of my syntax is failing.

这可能是一个漫长的反复试验,为节省宝贵的时间,建议您使用脚本文件而不是较长的命令:

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

Just want to add, that in order to run a sequence of commands you have to run it without quotes. At least only in this way it worked for me:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM