Install windows component (Perl)

This code can be found in Chapter 2 of Windows Server Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# This code simulates the same steps from the command-line solution.
# First an answer file is created containing the parameters to install
# the DNS Server. Then the sysocmgr command is invoked to perform the
# installation.

# ---------------------------------------------------------------
# Adapted from VBScript code contained in the book:
#      "Windows Server Cookbook" by Robbie Allen
# ISBN: 0-596-00633-0
# ---------------------------------------------------------------
use Win32::OLE;
$Win32::OLE::Warn = 3;

$strFile = 'c:\\comp_install.txt';

use constant ForWriting => 2;
$objFSO = Win32::OLE->new('Scripting.FileSystemObject');
$objFile = $objFSO->OpenTextFile($strFile, $constForWriting, 1);
$objFile->WriteLine('[netoptionalcomponents]');
$objFile->WriteLine('dns=1');
$objFile->Close();

$objWshShell = Win32::OLE->new('WScript.Shell');
$intRC = $objWshShell->Run('sysocmgr /i:%windir%\\inf\\sysoc.inf /u:' . $strFile, 0, 1);
if ($intRC != 0) {
    print 'Error returned from sysocmgr command: ' . $intRC, "\n";
}
else {
    print "Windows Component installed\n";
}

This code has been viewed 951 times.

New from the creators of TechTasks.com: StatSheet.com