简体   繁体   中英

Powershell parameters and function

I use function and parameters, but without result, if I used only code, who start on Invoke, I get result to satisfaction to me.

my code:

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

}

 }
  }
  } 

after entering value, it doesn't get the expected result. Can anyone help?

Try this one:

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

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM