Deploy SCO IP using Powershell

简介: comObject.types.ps1xml System.__ComObject GetProperty type=this.

comObject.types.ps1xml

<Types>
    <Type>
        <Name>System.__ComObject</Name>
        <Members>
            <ScriptMethod>
                <Name>GetProperty</Name>
                <Script>
                    $type = $this.gettype();
                    $type.invokeMember($args[0],[System.Reflection.BindingFlags]::GetProperty,$null,$this,$null)
                </Script>
            </ScriptMethod>
            <ScriptMethod>
                <Name>SetProperty</Name>
                <Script>
                    $type = $this.gettype();
                    $type.invokeMember($args[0],[System.Reflection.BindingFlags]::GetProperty,$null,$this,@($args[1]))
                </Script>
            </ScriptMethod>
            <ScriptMethod>
                <Name>InvokeParamProperty</Name>
                <Script>
                    $type = $this.gettype();
                    $index = $args.count -1 ;
                    $methodargs=$args[1..$index]
                    $type.invokeMember($args[0],[System.Reflection.BindingFlags]::GetProperty,$null,$this,$methodargs)
                </Script>
            </ScriptMethod>
            <ScriptMethod>
                <Name>InvokeMethod</Name>
                <Script>
                    $type = $this.gettype();
                    $index = $args.count -1 ;
                    $methodargs=$args[1..$index]
                    $type.invokeMember($args[0],[System.Reflection.BindingFlags]::InvokeMethod,$null,$this,$methodargs)
                </Script>
            </ScriptMethod>
        </Members>
    </Type>
</Types>


Install-IP.ps1

function LoadTypeData()
{
# Load some TypeData
$SavedEA = $Global:ErrorActionPreference
$Global:ErrorActionPreference = "SilentlyContinue"
Update-TypeData -AppendPath "C:\DeployIP\comObject.types.ps1xml"
$Global:ErrorActionPreference = $SavedEA
}

function global:Get-MsiProperty
{    
    PARAM (        
    [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
    [String]$Filename,

    [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
    [String]$Propertyname
    )
    
    # A quick check to see if the file exist    
    if(!(Test-Path $Filename))
    {        
        throw "Could not find " + $Filename    
    }     

    # Create an empty hashtable to store properties in    
    $msiProp = @{}
    # Creating WI object and load MSI database    
    $wiObject = New-Object -com WindowsInstaller.Installer  
    #$wiObject | Get-Member
    $wiDatabase = $wiObject.InvokeMethod("OpenDatabase", (Resolve-Path $Filename).Path, 0)     
    # Open the Property-view    
    $view = $wiDatabase.InvokeMethod("OpenView", "SELECT * FROM Property")    
    $view.InvokeMethod("Execute")     
    # Loop thru the table    
    $r = $view.InvokeMethod("Fetch")    
    while($r -ne $null) 
    {        
        # Add property and value to hash table        
        $prop = $r.InvokeParamProperty("StringData",1)
        $value = $r.InvokeParamProperty("StringData",2)
        if ($prop -eq $PropertyName)
        {
            $msiProp = $value
        }
        # Fetch the next row        
        $r = $view.InvokeMethod("Fetch")    
    }     
    $view.InvokeMethod("Close")     
    
    return $msiProp
}


function Connect-SCOServer{
<#
.SYNOPSIS
   Establishes a connection handle to the Orchestrator COM interface

.DESCRIPTION
    Establishes a connection handle to the Orchestrator COM interface that can be used
    with other functions that require authentication to succeed. This script must
    be run from the Orchestrator Management Server.

.PARAMETER UserName
    A valid user account with rights to perform actions against Orchestrator
    objects such as runbooks, folders, etc. Must be entered in the form of:
    DOMAIN\USERNAME
    
.PARAMETER Password
    The password associated with the provided user account.
    
.EXAMPLE
    $ConnectionHandle = Connect-SCOServer -Username "domain\user" -Password "Password"
    
.OUTPUTS
    System.Int32

#>

[CmdletBinding()]
	PARAM (
		[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true, ParameterSetName="SpecifyUser")]
        [ValidateNotNullOrEmpty()]
        [String] $UserName = $(throw "Provide a valid user account (domain\username)"),
        
		[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true, ParameterSetName="SpecifyUser")]
        [ValidateNotNullOrEmpty()]
        [String] $password = $(throw "Provide a password for the user account)")
	)
    
	PROCESS 
    {
        try
        {
	        $oismgr = new-object -com OpalisManagementService.OpalisManager
			$ohandle = New-Object object
			$handle = $handle = New-Object Runtime.InteropServices.VariantWrapper($ohandle)
            $retval = $oismgr.Connect($UserName, $password, [ref]$handle)
            #Write-Host "     Handle: $($Handle)"
            if ($handle.GetType().FullName -eq "System.Int32")
            {
                return $handle
            }
            else
            {
                Write-Error "Unable to get a valid connection handle to Orchestrator!" -ErrorAction Stop
            }
	    }
        catch
        {
            Write-Error "Exception occurred in $($MyInvocation.MyCommand): `n$($_.Exception)"
            throw new-object system.formatexception
        }
    }
}


function Install-SCOIntegrationPack{
<#
.SYNOPSIS
  Registers and optionally deploys an Integration Pack to the current computer.
  Assumes the current computer is a Management Server (and a Designer/Runbook Server
  in the case of deploying the IP)

.DESCRIPTION
    
.PARAMETER Handle
  The handle to the COM interface created using New-SCOConnection

.PARAMETER Filename
  The path and filename of the OIP file to be imported

.PARAMETER Deploy
  Switch parameter used when you want to also deploy the IP.
      
.EXAMPLE
  Install-SCOIntegrationPack -OIPFile "C:\Files\Test.OIP" -Deploy
    

.OUTPUTS
   
#>
    [CmdletBinding()]
	PARAM (

		[Parameter(Mandatory=$true)]
        [Int] $Handle,

		[Parameter(Mandatory=$true)]
        [String] $Filename,
                       
        [Parameter(Mandatory=$false)]
	    [Switch] $Deploy
        
        
	)
    BEGIN
    {
        $oismgr = new-object -com OpalisManagementService.OpalisManager -Strict
        #oismgr | Get-Member
        $sh = new-object -com shell.application
        LoadTypeData

        #Write-Host "     Handle: $($Handle)"
    }
    PROCESS 
    {
        try
        {

            if ($(Test-Path $filename) -eq $false)
            {
                Write-Error "File Not Found!"
                return
            }
            [System.IO.FileInfo]$oipFile = gi $filename
            
            $extractDir = $(Join-Path -Path $oipFile.DirectoryName -ChildPath $oipFile.BaseName)
	        if (Test-Path $extractDir)
            {
                ri $extractDir -Recurse -Force
            }

	        $ipSourceDirObj = $sh.namespace($oipFile.DirectoryName)
	        $ipSourceDirObj.NewFolder($oipFile.BaseName)
            $extractDirObj = $sh.namespace($extractDir)

            Write-Debug "`n`nExtracting files from the OIP"
            $zipFileName = Join-Path $oipFile.DirectoryName "$($oipFile.BaseName).zip"
            if (Test-Path $zipFileName)
            {
                ri $zipFileName -Force
            }
            $zipFile = $oipFile.CopyTo($zipFileName)
	        $zipFileObj = $sh.namespace($zipFile.FullName)

	        
	        $extractDirObj.CopyHere($zipFileObj.Items(),8 -and 16 -and 256)

            $commonFiles = ${Env:CommonProgramFiles(x86)}
            if ($null -eq $commonFiles)
            {
                $commonFiles = ${Env:CommonProgramFiles}
            }
            $PacksDir = Join-Path $commonFiles "Microsoft System Center 2012\Orchestrator\Management Server\Components\Packs"
            $ObjectsDir = Join-Path $commonFiles "Microsoft System Center 2012\Orchestrator\Management Server\Components\Objects"

            if ($(Test-Path $PacksDir) -eq $false)
            {
                Write-Error "Could not find $($PacksDir)"
                return
            }
            if ($(Test-Path $ObjectsDir) -eq $false)
            {
                Write-Error "Could not find $($ObjectsDir)"
                return
            }

           

            # Copy the MSI File to %Common Files%\Microsoft System Center 2012\Orchestrator\Management Server\Components\Objects
            $msiFile = $extractDirObj.self | gci | Where {$_.extension -eq ".msi"}
            

            $newMSIfile = $(Join-Path $ObjectsDir $msiFile.Name)
            if (Test-Path $newMSIfile)
            {
                ri $newMSIfile -Force
            }
	        $msiFile.CopyTo($newMSIfile)
            $productName = Get-MSIProperty -Filename $msiFile.FullName -Propertyname "ProductName"
            $productCode =  Get-MSIProperty -Filename $msiFile.FullName -Propertyname "ProductCode"


            #now use the MgmtService to install the IP
	        [System.IO.FileInfo]$capfile = $extractDirObj.self | gci | Where {$_.extension -eq ".cap"}
	        if ($capfile)
	        {
	            Write-Host "     Extracting $($capFile)"
	            $capXml = New-Object XML
	            $capXml.Load($capfile.Fullname)

	        
	            # Need to modify the CAP file to add Product ID and Product Name because Deployment Manager
                # reads the MSI file for this and inserts it into the DB so it displays in the UI. The COM
                # interface does not do this, so it needs to be done manually if you want it displayed.
                #
	            #     <ProductName datatype="string">IP_SYSTEMCENTERDATAPROTECTIONMANAGER_1.0.OIP</ProductName>
	            #    <ProductID datatype="string">{9422FCC6-11C4-4827-AC49-C5FD352C8AA0}</ProductID>
	            

                [Xml]$prodName = "<ProductName datatype=`"string`">$($ProductName)</ProductName>"
	            [Xml]$prodID = "<ProductID datatype=`"string`">$($ProductCode)</ProductID>"
                
	            $c = $capXml.ImportNode($prodName.ProductName, $true)
	            $d = $capXml.ImportNode($prodID.ProductID, $true)
	       
	            $capXml.Cap.AppendChild($c) 
	            $capXml.Cap.AppendChild($d) 

	            $oIPinfo = new-object object
	            $ipinfo = $ipinfo= New-Object Runtime.InteropServices.VariantWrapper($capXml.get_innerxml())
	            #Write-Host "     Importing Integration Pack $($capXml.get_innerxml())"
	            Write-Host "     Importing Integration Pack $($extractDir+'\\'+$capFile)"
	            $retval = $oismgr.AddIntegrationPack($Handle, [ref]$ipinfo)
		    }

            # Copy the OIP File to the %Common Files%\Microsoft System Center 2012\Orchestrator\Management Server\Components\Packs
            # directory and change the name to the GUID of the IP
            $productCodeOipFilename = "$($ProductCode).OIP"

            $newOIPfile = $(Join-Path $PacksDir $productCodeOipFilename)
            if (Test-Path $newOIPfile)
            {
                ri $newOIPfile -Force
            }
            write-host "new OIP file: $($newOIPfile)"
            $oipFile.CopyTo($newOIPfile)



            if ($PSBoundParameters.ContainsKey('Deploy') -eq $false)
            {
                return
            }

            if ($(Test-Path $newMSIfile) -eq $false)
	        { 
                Write-Error "Could not find $($newMSIfile)"
                return
            }

	        Write-Verbose "Running msiexec to install  $($newMSIfile)"

            $proc = New-Object System.Diagnostics.Process
            $proc.StartInfo.FileName = "msiexec.exe"
            $proc.StartInfo.Arguments =  "/i `"$($newMSIfile)`" ALLUSERS=1 /q" 
            $proc.Start() | out-null
            $proc.WaitForExit()

        }
        catch
        {
            Write-Error "Exception occurred in $($MyInvocation.MyCommand): `n$($_.Exception)"
            
        }
		
    }    
	    
}


Invoke-Install-IP.ps1

 

Param (
    [Parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [String] $OIPfile,
    [Parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [String] $UserName,
    [Parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [String] $UserPassWord
)

Process
{
    #
    . C:\DeployIP\Install-IP.ps1
    $conn = Connect-SCOServer –username $UserName -password $UserPassWord
    Install-SCOIntegrationPack –handle $conn –filename $OIPfile -Deploy
}

IIP.bat

@echo off
set currentDirectory=%~dp0
set OIPFILE=%1
set UserName=%2
set UserPassword=%3
echo %currentDirectory%
md "C:\Program Files (x86)\Common Files\Microsoft System Center 2012"
md "C:\Program Files (x86)\Common Files\Microsoft System Center 2012\Orchestrator"
md "C:\Program Files (x86)\Common Files\Microsoft System Center 2012\Orchestrator\Management Server"
md "C:\Program Files (x86)\Common Files\Microsoft System Center 2012\Orchestrator\Management Server\Components"
md "C:\Program Files (x86)\Common Files\Microsoft System Center 2012\Orchestrator\Management Server\Components\Packs"
md "C:\Program Files (x86)\Common Files\Microsoft System Center 2012\Orchestrator\Management Server\Components\Objects"

@pushd %currentDirectory%
start %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe set-executionpolicy remotesigned
ping localhost -n 20
echo "begin" %date%, %time%>>Install-IP.log
start %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe .\Invoke-Install-IP.ps1 %OIPFILE% %UserName% %UserPassword%
ping localhost -n 60
echo "end" %date%, %time%>>Install-IP.log
@popd




 

目录
打赏
0
0
0
0
20
分享
相关文章
Mac Please try running this command again as root/Administrator.
mac 终端安装程序,需要权限,出现以下提示语句;   Please try running this command again as root/Administrator.     需要执行以下命令即可: sudo chown -R $USER /usr/local
1326 0
mac/linux提示bash: telnet: command not found
mac/linux提示bash: telnet: command not found
HOSTS文件详解【win|mac】
hosts文件是一个用于储存计算机网络中各节点信息的计算机文件。这个文件负责将主机名映射到相应的IP地址。 hosts文件通常用于补充或取代网络中DNS的功能。和DNS不同的是,计算机的使用者可以直接对hosts文件进行控制。
3120 0