【问题标题】:Powershell parameters and functionPowershell参数和功能
【发布时间】:2020-01-30 10:43:37
【问题描述】:

我使用函数和参数,但没有结果,如果我只使用代码,从 Invoke 开始,我会得到令我满意的结果。

我的代码:

function Test-RegistryValue {

param (

 [parameter(position=0, Mandatory=$true)]
 [ValidateNotNullOrEmpty()]$pcs,

 [parameter(position=1, Mandatory=$true)]
 [ValidateNotNullOrEmpty()]$PathTAG

 [parameter(position=2, Mandatory=$true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]$PathUNIN

)

{
Invoke-Command -ComputerName $PCs  -ScriptBlock { 


If (Get-ItemProperty -Path 'HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$PathTAG ) {

    Write-host "$env:computername Value TAG exists" -ForegroundColor green


} Else {

    Write-host "$env:computername Value TAG DOES NOT exist"  -ForegroundColor red

} 

If (Get-ItemProperty -Path $PathUNIN -ErrorAction SilentlyContinue  ) {

    Write-host "$env:computername Value Uninstall exists" -ForegroundColor green

} Else {

    Write-Host "$env:computername Value uninstal DOES NOT exist"  -ForegroundColor red

}

 }
  }
  } 

输入值后,并没有得到预期的结果。有人可以帮忙吗?

【问题讨论】:

  • 您能指定您期望的结果吗?还有实际结果。
  • 两个$Path* 变量没有被发送到脚本块供 I-C 使用。您需要将它们传递给 ... ,最简单的方法是使用 $Using: 范围修饰符。脚本块内的 $Using:PathTAG 之类的东西应该可以工作。

标签: function powershell parameters


【解决方案1】:

试试这个:

function Test-RegistryValue {

    param (

    [parameter(position=0, Mandatory=$true)]
    [ValidateNotNullOrEmpty()]$pcs,

    [parameter(position=1, Mandatory=$true)]
    [ValidateNotNullOrEmpty()]$PathTAG,

    [parameter(position=2, Mandatory=$true, ValueFromPipelineByPropertyName = $true)]
    [ValidateNotNullOrEmpty()]$PathUNIN

    )


Invoke-Command -ComputerName $PCs  -ScriptBlock { 

    param(  $pcs,
            $PathTAG,
            $PathUNIN )

    if (Get-ItemProperty -Path "HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$PathTAG" ) {

        Write-host "$env:computername Value TAG exists" -ForegroundColor green }

    else {

        Write-host "$env:computername Value TAG DOES NOT exist"  -ForegroundColor red
    } 

    if (Get-ItemProperty -Path $PathUNIN -ErrorAction SilentlyContinue  ) {

        Write-host "$env:computername Value Uninstall exists" -ForegroundColor green }

    else {

        Write-Host "$env:computername Value uninstal DOES NOT exist"  -ForegroundColor red

    }

 } -ArgumentList $pcs, $PathTAG, $PathUNIN

 }

【讨论】:

    猜你喜欢
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2010-10-12
    相关资源
    最近更新 更多