mdrabble Posted May 17, 2017 Posted May 17, 2017 I have the following powershell script which I want to use as part of a task sequence to register a VM in Oracle box # ===================================================================================================== # Enable logging of script to help debugging # ===================================================================================================== $ErrorActionPreference="SilentlyContinue" Stop-Transcript | out-null $ErrorActionPreference = "Continue" Start-Transcript -path C:\Windows\Temp\VMSettings.log # ===================================================================================================== # Set Variables to be used within Script # ===================================================================================================== $LocalVMFolder = "C:\ProgrammingVM" $VMFileName = "ProgrammingVM.vbox" $VMName = "ProgrammingVM" $VMDiskPath = "\\SERVER\SHARE\ProgrammingVM.vmdk" $VBoxManage = "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" # ===================================================================================================== # Remove LocalVMFolder if exists first! # ===================================================================================================== if (Test-Path $LocalVMFolder ) { Remove-Item $LocalVMFolder -Recurse } # ===================================================================================================== # Add Extent Pack to Virtual Box for USB support # ===================================================================================================== &$VBoxManage extpack install "\\SCCM\SHARE\Software\Oracle Virtualbox\Oracle_VM_VirtualBox_Extension_Pack-5.1.22-115126.vbox-extpack" --replace # ===================================================================================================== # Create and add VBoxUsers group for LocalVM Folder Permissions # ===================================================================================================== Write-Host VBox installed... continuing installtion of VM Write-Host Creating VBOXUSERS local group... net localgroup VBOXUSERS /delete net localgroup VBOXUSERS /add /comment:'Group created for users of the Oracle Virtual Environment' Write-Host Adding 'Domain Users' to VBOXUSERS group... net localgroup VBOXUSERS "Domain Users" /add # ===================================================================================================== # Copy VM Settings from SCCM Software to Local Computer # ===================================================================================================== Write-Host Transferring Virtual Box image and settings to local computer... Robocopy "\\SCCM\SHARE\Software\Oracle Virtualbox\ProgrammingVM" "$LocalVMFolder" # ===================================================================================================== #Give access to VM Folder # ===================================================================================================== Write-Host Assigning permissions to $VMFileName directory... cacls $LocalVMFolder /t /e /g VBOXUSERS:C # Sets an environmental variable that Oracle reads to determine which home folder it should use. Write-Host Registering ProgrammingVM environmental variables... setx "-m VBOX_USER_HOME" "$LocalVMFolder" # ===================================================================================================== # Add VM to local Computer # ===================================================================================================== Write-Host Registering VM &$VBoxManage registervm "$LocalVMFolder\$VMFileName" Write-Host Making sure HDD is removed. &$VBoxManage storageattach ProgrammingVM --storagectl SATA --port 0 --device 0 --medium none Write-Host Re-attach the virtual media as an immutable media, so that it cannot be altered, and will always revert back to its original state. &$VBoxManage storageattach ProgrammingVM --storagectl SATA --device 0 --port 0 --type hdd --mtype immutable --medium $VMDiskPath # ===================================================================================================== # Set Restrictions on VM # ===================================================================================================== Write-Host Set Restrictions &$VBoxManage setextradata global GUI/SuppressMessages all &$VBoxManage setextradata global GUI/RestrictedGlobalSettingsPages General,Input,Update,Language,Display,Network,Extensions,Proxy &$VBoxManage setextradata "$LocalVMFolder\$VMFileName" GUI/RestrictedRuntimeMenus Machine,View,Devices,Help,Debug &$VBoxManage setextradata "$LocalVMFolder\$VMFileName" GUI/RestrictedStatusBarIndicators HardDisks,OpticalDisks,FloppyDisks,Network,USB,SharedFolders,Features,Mouse,Keyboard &$VBoxManage setextradata "$LocalVMFolder\$VMFileName" GUI/RestrictedCloseActions SaveState,PowerOff,Restore &$VBoxManage modifyvm $VMName --biosbootmenu disabled Write-Host Restrictions Set Write-Host . Write-Host Installation Complete Start-Sleep -s 30 # ===================================================================================================== # Stop Logging and Exit Script # ===================================================================================================== Stop-Transcript exit The script works perfectly fine if I run it manually, however if I run it as part of a task sequence it seems to hand The log file the transcript creates shows the following- ********************** Windows PowerShell transcript start Start time: 20170517115105 Username: SYSTEM RunAs User: SYSTEM Machine: W10DELL (Microsoft Windows NT 10.0.14393.0) Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command & 'C:\_SMSTaskSequence\Packages\UG1001D6\VMSettings.ps1' ; if ($LASTEXITCODE -eq 0) {exit $?} else {exit $LASTEXITCODE} Process ID: 1004 PSVersion: 5.1.14393.953 PSEdition: Desktop PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14393.953 BuildVersion: 10.0.14393.953 CLRVersion: 4.0.30319.42000 WSManStackVersion: 3.0 PSRemotingProtocolVersion: 2.3 SerializationVersion: 1.1.0.1 ********************** Transcript started, output file is C:\Windows\Temp\VMSettings.log Nothing to really indicate why the script is hanging. Cheers
markwilfan Posted May 17, 2017 Posted May 17, 2017 Does the script work outside of sccm. Can you run it manually?
markwilfan Posted May 17, 2017 Posted May 17, 2017 Also have you tried making a package and running it post deploy?
mdrabble Posted May 17, 2017 Author Posted May 17, 2017 Yea, script runs fine manually and I have packaged it. Ive set the execution policy to bypass. The script starts to run as part of the TS as the Start-Transcript creates the log file and then it just sits there. I'm wondering if I have the ErrorActionPreference correct and it is sitting waiting for some kind of response.
markwilfan Posted May 17, 2017 Posted May 17, 2017 So does the package run ok post deploy? I've had some scripts not work during the ts but fine after.
mdrabble Posted May 17, 2017 Author Posted May 17, 2017 Well I say I packaged it - I created a package with source files containing the relevant files but didn't create the program. In the TS I selected powershell command, selected the package and then typed in the script name and set the execution policy to bypass. Will see if I can get the script to run manually as system in the morning
mdrabble Posted May 18, 2017 Author Posted May 18, 2017 Ok, managed to test the script running as the System user and it runs run fine Tested other simple powershell scripts - mainly msiexec commands and they all run fine. Real head scratcher.....
ReadTheNetwork Posted May 18, 2017 Posted May 18, 2017 You might need to import a module? Maybe SCCM isn't automatically installing a required module? Not sure which though
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now