簡體   English   中英

從RunOnce運行PowerShell

[英]Running PowerShell from 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命令將正確運行。 我無法弄清楚語法的哪一部分失敗。

這可能是一個漫長的反復試驗,為節省寶貴的時間,建議您使用腳本文件而不是較長的命令:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM