Write-host 帮助信息

如下说明是翻译 : help write-host 产生的帮助信息 .
译者 : Edengundam( 马涛 )
 
Write-Host
 
大纲
使用 host 用户接口显示输入对象
 
语法
Write-Host [[-object] <Object>] [-noNewLine] [-separator <Object>] [-foregroundcolor {<Black> | <DarkBlue> | <DarkGreen> | <DarkCyan> | <DarkRed> | <DarkMagenta> | <DarkYellow> | <Gray> | <DarkGray> | <Blue> | <Green> | <Cyan> | <Red> | <Magenta> | <Yellow> | <White>}] [-backgroundColor {<Black> | <DarkBlue> | <DarkGreen> | <DarkCyan> | <DarkRed> | <DarkMagenta> | <DarkYellow> | <Gray> | <DarkGray> | <Blue> | <Green> | <Cyan> | <Red> | <Magenta> | <Yellow> | <White>}] [<CommonParameters>]
 
详细描述
使用 host 用户接口显示输入对象 . Write-Host 创建一个自定义的 host 窗口 . 你可以通过 ForegroundColor 参数指定现在该窗口中文本的颜色 , 也可以通过 BackgroundColor 参数指定显示在该窗口中本文的背景颜色 . 分隔符参数允许指定特定字符串 , 作为不同对象之间的分隔符 .
 
参数
 
-object <Object>
要显示在控制台上的对象 .
 
强制参数 ?
false
参数位置 ?
1
默认值
 
允许从管道绑定输入 ?  
true ( 传值 )
允许通配符扩展
false
 
-noNewLine <SwitchParameter>
指定显示在控制台的信息不以换行结尾 .
 
强制参数 ?
false
参数位置 ?
named
默认值
False
允许从管道绑定输入 ?  
false
允许通配符扩展
false
 
-separator <Object>
指定显示在控制台上的对象之间使用的分隔字符串 .
 
强制参数 ?
false
参数位置 ?
named
默认值
一个空格
允许从管道绑定输入 ?  
false
允许通配符扩展
false
 
-foregroundcolor <ConsoleColor>
指定文本颜色 .
 
此参数接受的取值如下 :
 
·          Black
·          DarkBlue
·          DarkGreen
·          DarkCyan
·          DarkRed
·          DarkMagenta
·          DarkYellow
·          Gray
·          DarkGray
·          Blue
·          Green
·          Cyan
·          Red
·          Magenta
·          Yellow
·          White
 
强制参数 ?
false
参数位置 ?
named
默认值
Host-defined
允许从管道绑定输入 ?  
false
允许通配符扩展
false
 
-backgroundColor <ConsoleColor>
指定背景颜色 .
 
此参数接受的取值如下 :
 
·          Black
·          DarkBlue
·          DarkGreen
·          DarkCyan
·          DarkRed
·          DarkMagenta
·          DarkYellow
·          Gray
·          DarkGray
·          Blue
·          Green
·          Cyan
·          Red
·          Magenta
·          Yellow
·          White
 
强制参数 ?
false
参数位置 ?
named
默认值
Host-defined
允许从管道绑定输入 ?   
false
允许通配符扩展
false
 
< 公共参数 >
此命令支持公共参数 : -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable. 更多信息 , 输入 , "get-help about_commonparameters".
 
输入类型
任意
 
注意
 
更多信息 , 输入 "Get-Help Write-Host -detailed". 需要技术信息 , 输入 "Get-Help Write-Host -full".
 
如果需要为该命令提供多个参数 , 请使用逗号进行分隔 . 例如 , "<parameter-name> <value1>, <value2>".
 
1
 
C:/PS>write-host "no newline test" -nonewline
 
此命令显示输入信息到控制台 , 由于制定了 nonewline 参数 , 提示符直接跟在输出信息之后 .
 
2
 
C:/PS>write-host (2,4,6,8,10,12) -Separator ", +2= "
 
此命令显示从 2 12 的所有偶数 . 分隔符参数指定使用 , +2= ( 逗号 , 空格 , +, 2, =, 空格 ) 作为分割 .
 
2, +2= 4, +2= 6, +2= 8, +2= 10, +2= 12
 
3
 
C:/PS>write-host (2,4,6,8,10,12) -Separator ", -> " -foregroundcolor DarkGreen -backgroundcolor white
 
此命令显示从 2 12 的所有偶数 . Foregroundcolor 参数指定输出文本为绿色 , Backgroundcolor 参数指定背景色为白色 .
 
相关链接
Write-Verbose
Write-Error
Write-Progress
Write-Debug
Write-Output
Write-Warning
Out-Host
 
 
# 注册DLL $dllPath ="C:\BtnDll\BTNRevers.dll" Start-Process "regsvr32.exe" -ArgumentList $dllPath -Wait # /s 表示静默注册,无用户界面 # 添加到 COM+ 应用程序 $appName = "YourCOMPlusAppName" $comClass = "BTNRevers.BTNReversClass" # 获取 COMAdminCatalog 对象 $catalog = New-Object -ComObject COMAdmin.COMAdminCatalog # 查找 COM+ 应用程序集合 $appCollection = $catalog.GetCollection("Applications") $appCollection.Populate() # 查找是否已经存在指定的应用程序 $existingApp = $appCollection | Where-Object { $_.Name -eq $appName } if (-not $existingApp) { # 如果没有找到应用程序,创建新应用程序 $newApp = $appCollection.Add() $newApp.Value("Name") = $appName $newApp.Value("Activation") = 0 $appCollection.SaveChanges() Write-Host "exsite: $appName" } else { Write-Host "exsite: $appName" } # 查找组件集合 $components = $catalog.GetCollection("Components",appCollection.Key) if ($components -ne $null) { Write-Host "Components collection found. Populating..." $components.Populate() } else { Write-Host "Components collection is null. Cannot populate." } $components.Populate() # 查找是否存在指定的组件 $existingComponent = $components | Where-Object { $_.ProgId -eq $comClass } if (-not $existingComponent) { # 如果没有找到组件,添加新组件 $newComponent = $components.Add() $newComponent.Value("ProgId") = $comClass $components.SaveChanges() Write-Host "no: $comClass" } else { Write-Host "exsite: $comClass" }
03-26
<think>嗯,用户之前已经问过如何通过脚本注册32位的COM DLL并添加到COM+应用程序,现在他们提供了一个具体的PowerShell脚本,可能是在实际应用中遇到了问题,需要检查这个脚本的正确性或者调试。我需要仔细看看用户提供的代码,找出潜在的问题,并给出改进建议。 首先看注册DLL的部分。用户用的是Start-Process来调用regsvr32.exe,但参数部分可能有问题。原来的例子中用了SysWOW64下的regsvr32,因为用户是在64位系统上注册32位DLL,但用户现在的代码中没有指定路径,直接用了regsvr32.exe,这可能会默认调用系统目录下的64位版本,导致注册到错误的路径。这应该是第一个问题。 然后,参数列表部分,用户用了$dllPath,但原来的例子中用了/s参数来静默注册,而用户的代码中没有/s,会导致弹出窗口,可能在自动化脚本中不希望有交互。所以这里需要添加/s参数,并且需要用转义符或者正确的方式传递参数。 接下来是添加到COM+应用程序的部分。用户创建了应用程序,设置Activation为0,这部分看起来没问题。但是当查找现有应用程序时,用户用了Where-Object { $_.Name -eq $appName },但可能需要检查的是每个项的名称是否正确,因为集合中的项可能有不同的属性结构。这里可能需要确认$appCollection中的项是否有Name属性,或者是否应该用Value("Name")来获取。 然后,在添加组件时,用户获取组件集合的方式是否正确?用户用了$catalog.GetCollection("Components", appCollection.Key),但appCollection是应用程序集合,而正确的应该是新创建或现有的应用程序的Key。这里可能应该使用$newApp.Key或者$existingApp.Key,而不是整个appCollection的Key,否则可能会指向错误的父级集合。 另外,在检查现有组件时,用户使用了$components | Where-Object { $_.ProgId -eq $comClass },这里是否应该用Value("ProgId")来获取属性?因为直接访问.ProgId可能不存在,应该通过Value方法访问。例如,$_.Value("ProgId") -eq $comClass。 此外,在添加新组件时,用户调用了$components.Add(),然后设置ProgId,但可能需要指定更多的属性,比如CLSID或者DLL路径?或者是否只需要ProgId即可?可能需要查阅COMAdmin的文档,确认添加组件所需的必要属性。 还有,在保存更改时,是否正确处理了错误?比如,如果SaveChanges失败,应该捕获异常或输出错误信息帮助用户诊断问题。 另外,用户脚本中的一些路径和名称需要替换,比如$appName和$comClass是否已经正确设置为实际的值?比如,用户提供的代码中$appName是"YourCOMPlusAppName",可能需要改为具体的应用名称,而$comClass是"BTNRevers.BTNReversClass",这个ProgID是否正确?需要确认DLL中的类确实有这个ProgID。 此外,权限问题,用户是否以管理员身份运行脚本?因为注册DLL和修改COM+都需要管理员权限,如果脚本没有以管理员启动,可能会失败。 最后,代码中的一些逻辑问题,比如在创建新应用程序后,是否需要重新获取应用程序集合,以确保后续操作基于最新的数据?或者在添加组件时,是否需要关联到正确的应用程序实例? 综合这些可能的错误点,我需要逐一指出并给出修改建议,确保脚本能够在32位环境下正确注册DLL,并正确添加到COM+应用程序中,同时处理静默注册、路径、参数传递、集合操作等问题。</think>以下是针对您提供的脚本的详细分析和改进建议,主要问题集中在**注册路径错误**、**COM+组件关联逻辑不完整**以及**属性访问方式不正确**三个方面: --- ### **一、注册DLL部分的问题修复** #### **问题1:未指定32位注册工具路径** 在64位系统中,`regsvr32.exe`默认会从`System32`目录(64位)加载,导致32位DLL注册失败。 **修改方案:** ```powershell # 明确调用SysWOW64目录下的32位regsvr32 Start-Process "$env:SystemRoot\SysWOW64\regsvr32.exe" -ArgumentList "/s `"$dllPath`"" -Wait ``` #### **问题2:缺少静默注册参数** 原代码未使用`/s`参数,会导致弹出确认窗口,干扰自动化流程。 **修改后完整代码:** ```powershell $dllPath = "C:\BtnDll\BTNRevers.dll" if (-not (Test-Path $dllPath)) { throw "DLL文件不存在: $dllPath" } Start-Process "$env:SystemRoot\SysWOW64\regsvr32.exe" -ArgumentList "/s `"$dllPath`"" -Wait ``` --- ### **二、COM+应用程序配置的改进** #### **问题1:应用程序与组件未正确关联** 原代码中`GetCollection("Components", appCollection.Key)`会指向应用程序集合的根键,而非具体应用程序的键。 **修改方案:** ```powershell # 创建/获取应用程序后,需使用其唯一Key关联组件 $appKey = $newApp.Key # 或 $existingApp.Key $components = $catalog.GetCollection("Components", $appKey) ``` #### **问题2:属性访问方式错误** 直接使用`$_.ProgId`会返回空值,必须通过`Value("ProgId")`访问属性。 **修改组件检查逻辑:** ```powershell $existingComponent = $components | Where-Object { $_.Value("ProgId") -eq $comClass } ``` #### **问题3:组件注册信息不完整** 仅设置`ProgId`可能不足,需明确指定`DLL`路径和`CLSID`。 **改进后的组件添加逻辑:** ```powershell $newComponent = $components.Add() $newComponent.Value("ProgId") = $comClass $newComponent.Value("DLL") = $dllPath # 需根据实际CLSID补充 $components.SaveChanges() ``` --- ### **三、完整修正脚本** ```powershell # 注册32位DLL $dllPath = "C:\BtnDll\BTNRevers.dll" if (-not (Test-Path $dllPath)) { throw "DLL文件不存在: $dllPath" } Start-Process "$env:SystemRoot\SysWOW64\regsvr32.exe" -ArgumentList "/s `"$dllPath`"" -Wait # 配置COM+应用程序 $appName = "YourCOMPlusAppName" $comClass = "BTNRevers.BTNReversClass" $catalog = New-Object -ComObject COMAdmin.COMAdminCatalog $apps = $catalog.GetCollection("Applications") $apps.Populate() $existingApp = $apps | Where-Object { $_.Value("Name") -eq $appName } if (-not $existingApp) { $newApp = $apps.Add() $newApp.Value("Name") = $appName $newApp.Value("Activation") = 0 # 0 = 服务端激活 $apps.SaveChanges() $appKey = $newApp.Key } else { $appKey = $existingApp.Key } # 添加组件 $components = $catalog.GetCollection("Components", $appKey) $components.Populate() $existingComponent = $components | Where-Object { $_.Value("ProgId") -eq $comClass } if (-not $existingComponent) { $newComponent = $components.Add() $newComponent.Value("ProgId") = $comClass $newComponent.Value("CLSID") = "{Your-Component-CLSID}" # 需替换实际CLSID $components.SaveChanges() Write-Host "组件添加成功: $comClass" } ``` --- ### **四、关键补充说明** 1. **权限要求**: - 右键以**管理员身份运行**PowerShell脚本。 2. **CLSID查找**: - 使用`regedit`搜索ProgID,或在DLL代码中查找`[Guid("...")]`属性。 3. **错误调试**: - 添加`try/catch`块捕获异常: ```powershell try { $components.SaveChanges() } catch { Write-Host "错误: $($_.Exception.Message)" } ``` 若需进一步验证,可通过**组件服务管理器**(`comexp.msc`)检查应用程序和组件的注册状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值