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




 

目录
相关文章
|
3天前
|
数据采集 人工智能 安全
|
12天前
|
云安全 监控 安全
|
4天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1070 151
|
4天前
|
编解码 人工智能 机器人
通义万相2.6,模型使用指南
智能分镜 | 多镜头叙事 | 支持15秒视频生成 | 高品质声音生成 | 多人稳定对话
|
17天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1744 9
|
9天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
688 152
|
11天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
655 12
|
6天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
417 4