2012-11-15 44 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\"