如何从PowerShellpipe理COM +?

是否有PowerShell模块或用于pipe理COM +组件的命令? 特别是,我想:

  • 查询COM +应用程序是否已安装/注册。
  • 启动或停止一个COM +应用程序。
  • 安装/注册或卸载/注销COM +应用程序。

我是新来的PowerShell和COM +,所以可能会使用错误的条款(安装VS寄存器等)。

列出COM +组件

$comAdmin = New-Object -com ("COMAdmin.COMAdminCatalog.1") $applications = $comAdmin.GetCollection("Applications") $applications.Populate() foreach ($application in $applications) { $components = $applications.GetCollection("Components",$application.key) $components.Populate() foreach ($component in $components) { $dllName = $component.Value("DLL") $componentName = $component.Name "Component Name:$componentName" "DllName: $dllName`n" } } 

停止/启动组件

 $comAdmin.StartApplication("appName") $comAdmin.ShutdownApplication("appName") 

使用InstallApplication方法安装Com +应用程序

 # Refer to: http://msdn.microsoft.com/en-us/library/ms681806(v=vs.85).aspx $comAdmin.InstallApplication("fileName") 

这是一种使用PowerShell注册com +应用程序的方法。 您需要用您的对象的128位唯一标识符replace$ AppID和$ CLSID。

 $AppID = "{123456789}" $CLSID = "{123456789}" $comAdmin = New-Object -comobject COMAdmin.COMAdminCatalog function InstallApplication ( $comAdmin ) { $AppName = "foobar" $AppDesc = "foobar com plus object does stuff" $apps = $comAdmin.GetCollection("Applications") $apps.Populate(); $appFound = $false foreach ($app in $apps ) { if ($app.Name -eq $AppName ) { $appFound = $true #break } } if (!($appFound )) { $app = $apps.Add() $app.Value("ID") = $AppID $app.Value("Name") = $AppName $app.Value("Description") = $AppDesc $app.Value("ApplicationAccessChecksEnabled") = $True $app.Value("AccessChecksLevel") = 1 #Component level $app.Value("Activation") = "Local" $apps.SaveChanges(); } Else { Write-Host "$AppName already exists, skipping" -ForegroundColor green } } function InstallComponents( $comAdmin ) { $comAdmin.ImportComponent( $AppID, $CLSID ) # Configure the directory service component $apps = $comAdmin.GetCollection("Applications") $apps.Populate(); $comps = $apps.GetCollection("Components", $AppID) $comps.Populate(); ForEach ($comp in $comps) { If ($comp.Key -eq $CLSID) { $ComponentFound = $True #break } } If ($ComponentFound) { $comp.Value("Description") = "foobar " $comp.Value("ComponentAccessChecksEnabled") = $true $comp.Value("ObjectPoolingEnabled") = $true $comp.Value("JustInTimeActivation") = $false $comp.Value("Synchronization") = 2 #Supported $comp.Value("Transaction") = 1 #Not supported $comps.SaveChanges(); } Else { Write-Warning "CLSID $CLSID not found" } } function ConfigureSecurity( $comAdmin ) { $apps = $comAdmin.GetCollection("Applications") $apps.Populate(); # Add Administrator, and User roles to the application. #https://msdn.microsoft.com/en-us/library/windows/desktop/ms678849%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 $roles = $apps.GetCollection("Roles", $AppID) $roles.Populate(); $found = $false foreach ( $role in $roles ) { if ($role.Key -eq "Administrators") { $found = $true #break } } if (!($found)) { $role = $roles.Add() $role.Value("Name") = "Administrators" } $found = $false foreach ($role in $roles ) { if ($role.Key -eq "Users") { $found = $true #break } } if (!($found)) { $role = $roles.Add() $role.Value("Name") = "Users" } $roles.SaveChanges(); # Assign accounts to the roles $domain = (Get-WmiObject Win32_ComputerSystem).Domain if ($domain -like 'WORKGROUP') { Write-Warning "Not joined to domain, skipping com+ roles" } else { $users = $roles.GetCollection("UsersInRole", "Users") $user = $users.Add() $roleDescription = "$domain" + "\" + "Users" Write-Host "Adding com+ role $roleDescription" -ForegroundColor green $user.Value("User") = $roleDescription $users.SaveChanges(); $users = $roles.GetCollection("UsersInRole", "Administrators") $user = $users.Add() $roleDescription = "$domain" + "\" + "fooAdmin" Write-Host "Adding com+ role $roleDescription" -ForegroundColor green $user.Value("User") = $roleDescription $user = $users.Add() $roleDescription = "$domain" + "\" + "barAdmin" Write-Host "Adding com+ role $roleDescription" -ForegroundColor green $user.Value("User") = $roleDescription $users.SaveChanges(); # Configure component to allow access to role. $comps = $apps.GetCollection("Components", $AppID) $comps.Populate(); ForEach ($comp In $comps ) { If ($comp.Key -eq $CLSID) { $ComponentFound = $True # break } } If ($ComponentFound ) { $RolesForComponent = $comps.GetCollection("RolesForComponent", $CLSID) $RoleForComponent = $RolesForComponent.Add() $RoleForComponent.Value("Name") = "Administrators" $RoleForComponent = $RolesForComponent.Add() $RoleForComponent.Value("Name") = "Users" $RolesForComponent.SaveChanges(); } Else { Write-Warning "CLSID $CLSID not found" } } } InstallApplication $comAdmin InstallComponents $comAdmin ConfigureSecurity $comAdmin