Tag Archives: Windows

Start and stop windows services with powershell scripts

With Powershell you can easily start or stop specific windows services depending on current status. An example is starting the VMWare Services only if they are currently stopped. The command

get-service

list all windows service with their status, short – and display name. Create a new File startVMWare.ps1 and paste the following

foreach ($svc in Get-Service){
  if(($svc.displayname.StartsWith("VMware")) -AND ($svc.Status -eq "Stopped")) {
    echo $svc.DisplayName
    Start-Service $svc.name
  }
}

Sign the script as shown in my previous post to run self signed scripts. Run the script in an powershell with administrator rights.

.\startVMWare.ps1

The script starts only stopped VMWare services. To run this script directly you can write a small dos file startVMWare.cmd with the following content

powershell -file <FULL PATH TO YOUR SCRIPT>\startVMWare.ps1

Start the Dos file with right click and admin rights to execute the vmware start powershell script.

Howto run self signed powershell scripts

Windows command line scripts was for a long time the only way for scripting windows. With the Windows PowerShell you have can write scripts more like an program in object oriented way. Starting with Windows 7 it is preinstalled with version 2.0 but can as well installed under Windows XP or Vista. Windows 8 will ship with version 3.0 which adds windows work flow foundation functionality. Windows 7 ships with an IDE for PowerShell called Windows PowerShell ISE”. Scripts stored in files with .ps1 suffix. A sample hello world looks like this:

echo "hello world"

Save the content in a file called hello.ps1. Start the powershell by searching for powershell in the windows 7 search box above the start button and with right click to run as administrator. Change the current folder with cdto the one where you saved your first powershell script. Run your script with

.\hello.ps1

Unfortunately you get a PSSecurityException because powershell script execution is controlled by an security policy. Like Java signed jars you must sign your scripts and set the policy to execute only signed scripts

Set-ExecutionPolicy Restricted

Powershell accepts self signed certificates and can be created by the makecert command. Makecert is part of the windows SDK. Download the installer and run through the wizard steps. At the last screen deselect all parts and check only the tools under first section called Windows Native Code Development. Open a command shell with shift and right click on the folder C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin. Enter the following commands to create a authority:

makecert -n "CN=PowerShell Local Certificate Root" -a sha1 -eku 1.3.6.1.5.5.7.3.3 -r -sv root.pvk root.cer -ss Root -sr localMachine

and the following command to create a self signed certificate

makecert -pe -n "CN=PowerShell User" -ss MY -a sha1 -eku 1.3.6.1.5.5.7.3.3 -iv root.pvk -ic root.cer

The following command in the powershell show your created certificate:

Get-ChildItem cert:\CurrentUser\My -codesign

Now we can sign our first script with the following command

Set-AuthenticodeSignature .\hello.ps1 @(Get-ChildItem cert:\CurrentUser\My -codesigning)[0]

Run now your signed script with

.\hello.ps1

with prints

hello world

Powershell scripts can use profile scripts to store common functions for own scripts, functions,.. Enter the following command to your standard profile file

$profile

Open the file or create it with a text editor and paste the following

function sign ($filename) {
 $cert = @(gci cert:\currentuser\my -codesigning)[0]
 Set-AuthenticodeSignature $filename $cert
 }

Sign the profile file

Set-AuthenticodeSignature $profile @(Get-ChildItem cert:\CurrentUser\My -codesigning)[0]

Write now your own little powershell script like test.ps1. The sign function can now be used like this

sign .\test1.ps1

Convert VMWare 2.x Image to VirtualBox 3.x (seamless mode)

Windows 7 Home Premium has no XP Mode Feature like in Pro or Ultimate Edition to run an Windows XP virtual machine seamless. Home Premium can be upgraded by Anytime Upgrade within Windows to Professional or Ultimate (90$* or 140$*). You need at least Professional Version for XP Mode. So i looked for alternatives. Under Windows XP was VMWare server 2.x my first choice without seamless mode. Seamless Mode made VM windows be available in host OS as “normal” windows without the rest of the VM OS. One Alternative is VMWare Workstation 7 with seamless mode (176€*). Paralells has currently an beta version available with Coherence (seamless) feature. Virtualbox as open source alternative has seamless mode too. You need only to install the guest additions of virtualbox, reboot after installation and call from menu “seamless mode”.

So i give Virtualbox a try and installed the current 3.0.10 version under Windows 7. VirtualBox can work with VMware VMDK Hard disks out of the box.

Steps to convert from VMware to Virtualbox:

  1. Start VMware image under VMware
  2. Uninstall VMware Tools inside VM
  3. Shutdown VM
  4. Open DOS command shell in VM folder
  5. set path variable to include VMWare server folder: set path=;%PATH%
  6. convert splitted VM hard disk to single file: vmware-vdiskmanager -r source_multiples.vmdk -t 2 single_file.vmdk (use ” to surround file name if file name contains spaces)
  7. copy new single_file.vmdk and single_file-flat.vmdk to \harddisks on host os with virtualbox
  8. Start VirtualBox under host OS
  9. Open in file menu “Virtual media manager”
  10. Go to “Hard Disks” tab
  11. Click on Add Button and choose single_file.vmdk
  12. Close dialog with OK button
  13. Click on “New” button to create a new VM
  14. Click on “Next” button
  15. Enter name for new VM and choose operation system including version (in my example Win_vista” as name, “Microsoft Windows” as OS and “Windows Vista” as version)
  16. Set memory to a suitable value (on my machine i prefer 512MB for good enough performance)
  17. On the next page choose “Use existing hard disk” and select from Drop down the old VMware hard disk image
  18. Click on Finish
  19. Open Settings of new VM to correct problems with different hard ware in VMware and VirtualBox
  20. Go to System => Motherboard and “enable IO APIC”
  21. a had an scsci lsi logic controller under VMware: Go to “Hard Disks” and “Enable Additional Controller” including selection of “SCSI (Lsilogic)”s
  22. Start new VM
  23. Install Virtualbox guest additions from vm window menu under “Devices”
  24. reboot vm
  25. Start for example a IE windows inside vm and call from “Machine” menu “seamless mode”

Currently that doesn’t seems to work with windows vista. Another VM in virtualbox with windows XP Professional works as expected in seamless mode.

Keep in mind that such a migration with Windows Vista as guest os needs another activation because of changed multiple hardware components.

Another possibility is to install the unofficial Virtual PC 6.1 for windows 7 from the knowledge base (32bit / 64bit). As described here you see in the windows 7 start menu the entries of the installed programs under the xp vm to start them directly in seamless mode. Comparing to the windows 7 XP mode their is the requirement for a separate windows xp license for a windows home or starter host os. Windows 7 professional or ultimate include such a license for a virtual windows xp on the same machine. Virtual PC needs a Virtualization hardware support like intel VT-X or AMD-V. The installation of Virtual PC fails if the processor and mainboard don’t fit to this requirement.

Startmenu entry of Virtual PC

Startmenue Virtual PC

Start installed Apps inside XP VM from Windows 7

Start Apps inside XP VM from Windows 7

Seamless started XP VM Application under Windows 7

Seamless started App

*Price are only snapshots from the manufacturer websites without any guarantee and only provided to compare the possibilities.