功能不打印变量

时间:2015-03-12 14:38:18

标签: function powershell

我有这个脚本,我已经成为一个函数,所以我可以点源它。我遇到的问题  运行脚本时未打印$computername变量。任何人都可以帮助我吗?

       param( [Parameter(Mandatory=$True)]
           $ComputerName
    )
 function Get-InstalledApps {
    $array = @()
    foreach($basekey in ('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')){

    $remoteBaseKeyObject = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername)    
    $remoteBaseKey = $remoteBaseKeyObject.OpenSubKey($basekey)            
    $subKeys = $remoteBaseKey.GetSubKeyNames()           

    foreach($key in $subKeys){           
        $thisKey=$basekey+'\'+$key         
        $thisSubKey=$remoteBaseKeyObject.OpenSubKey($thisKey)
        $psObject = New-Object PSObject
        $psObject | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $computername
        $psObject | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName")) -ea SilentlyContinue
        $psObject | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion")) 
        $psObject | Add-Member -MemberType NoteProperty -Name "InstallLocation" -Value $($thisSubKey.GetValue("InstallLocation"))
        $psObject | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $($thisSubKey.GetValue("Publisher"))
        $psObject | Add-Member -MemberType NoteProperty -Name "Uninstallstring" -Value $($thisSubKey.GetValue("UninstallString")) 
        $array += $psObject
    }          
    $array 
     }
    }

  Get-InstalledApps

1 个答案:

答案 0 :(得分:1)

如果你保存在Get-InstalledApps.ps1文件中:

param( [Parameter(Mandatory=$True)]
       $ComputerName
)

$array = @()
foreach($basekey in ('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')){

$remoteBaseKeyObject = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername)    
$remoteBaseKey = $remoteBaseKeyObject.OpenSubKey($basekey)            
$subKeys = $remoteBaseKey.GetSubKeyNames()           

foreach($key in $subKeys){           
    $thisKey=$basekey+'\'+$key         
    $thisSubKey=$remoteBaseKeyObject.OpenSubKey($thisKey)
    $psObject = New-Object PSObject
    $psObject | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $computername
    $psObject | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName")) -ea SilentlyContinue
    $psObject | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion")) 
    $psObject | Add-Member -MemberType NoteProperty -Name "InstallLocation" -Value $($thisSubKey.GetValue("InstallLocation"))
    $psObject | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $($thisSubKey.GetValue("Publisher"))
    $psObject | Add-Member -MemberType NoteProperty -Name "Uninstallstring" -Value $($thisSubKey.GetValue("UninstallString")) 
    $array += $psObject
}          
$array 
 }

你可以像这样打电话(它必须在路径中):

get-installedapps.ps1 -ComputerName mycomputername

并且无需点源。

如果您想在powershell中绝对使用函数,我建议您阅读此https://technet.microsoft.com/en-us/magazine/hh360993.aspx