将函数放在文件中并调用它

时间:2019-04-09 14:29:22

标签: windows powershell install remote-access sysadmin

im试图在计算机上执行此功能。 来源:https://github.com/gangstanthony/PowerShell/blob/master/Get-InstalledApps.ps1

我将其放在一个名为Get-InstalledApps.ps1的文件中,并试图在Windows 10上提升权限的命令提示符下运行它,但没有任何反应。如何正确调用该函数?如果我将整个函数复制粘贴到外壳中,它将起作用,但这不适用于我的用例,因为我正在尝试使用WPKG远程执行它。

Get-InstalledApps -ComputerName $env:COMPUTERNAME
# a more complete script can be found here:
# https://gist.github.com/indented-automation/32efb05a5fb67ef9eed02bbb8fe90691
# https://community.spiceworks.com/scripts/show_download/2170-get-a-list-of-installed-software-from-a-remote-computer-fast-as-lightning

# note: might have to use enable-psremoting first if no results are returned

<#
friends don't let friends use win32_product to query for installed programs
https://sdmsoftware.com/group-policy-blog/wmi/why-win32_product-is-bad-news/
https://blogs.technet.microsoft.com/heyscriptingguy/2011/12/14/use-powershell-to-find-and-uninstall-software/
https://blogs.technet.microsoft.com/heyscriptingguy/2011/11/13/use-powershell-to-quickly-find-installed-software/
http://powershell.org/wp/forums/topic/alternatives-to-win32_product/
this is what i use to get installed programs
https://github.com/gangstanthony/PowerShell/blob/master/Get-InstalledApps.ps1
example usage
    Get-InstalledApps -ComputerName $env:COMPUTERNAME -NameRegex '^java'
you will get an uninstallstring returned from that as well. you can use one of the methods below to run the uninstaller on the remote computer
https://www.reddit.com/r/PowerShell/comments/4l5kkm/what_are_all_or_at_least_the_most_common_ways_to/
#>

function Get-InstalledApps {
    param (
        [Parameter(ValueFromPipeline=$true)]
        [string[]]$ComputerName = $env:COMPUTERNAME,
        [string]$NameRegex = ''
    )

    foreach ($comp in $ComputerName) {
        $keys = '','\Wow6432Node'
        foreach ($key in $keys) {
            try {
                $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $comp)
                $apps = $reg.OpenSubKey("SOFTWARE$key\Microsoft\Windows\CurrentVersion\Uninstall").GetSubKeyNames()
            } catch {
                continue
            }

            foreach ($app in $apps) {
                $program = $reg.OpenSubKey("SOFTWARE$key\Microsoft\Windows\CurrentVersion\Uninstall\$app")
                $name = $program.GetValue('DisplayName')
                if ($name -and $name -match $NameRegex) {
                    [pscustomobject]@{
                        ComputerName = $comp
                        DisplayName = $name
                        DisplayVersion = $program.GetValue('DisplayVersion')
                        Publisher = $program.GetValue('Publisher')
                        InstallDate = $program.GetValue('InstallDate')
                        UninstallString = $program.GetValue('UninstallString')
                        Bits = $(if ($key -eq '\Wow6432Node') {'64'} else {'32'})
                        Path = $program.name
                    }
                }
            }
        }
    }
}

更新: 解决了它,我应该在脚本文件的末尾添加 Get-InstalledApps-计算机名$ env:COMPUTERNAME

0 个答案:

没有答案