【问题标题】:powershell sort-object and keeps window openpowershell 排序对象并保持窗口打开
【发布时间】:2021-02-25 07:39:19
【问题描述】:

我有以下脚本来检查本地和远程机器上安装的软件。 通常它工作正常,但我有两个问题。它仅在我在 ISE 中打开时才有效。如果我在普通的 powershell 中打开它,脚本会立即关闭。即使是暂停或读取主机命令也不起作用。 例如,这是我的本地机器脚本。希望大家能帮帮我。

Function Get-InstalledSoftware {
Param(
    [Alias('Computer','ComputerName','HostName')]
    [Parameter(
        ValueFromPipeline=$True,
        ValueFromPipelineByPropertyName=$true,
        Position=1
    )]
    [string]$Name = $env:COMPUTERNAME
)
Begin{
    $lmKeys = "Software\Microsoft\Windows\CurrentVersion\Uninstall","SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
    $lmReg = [Microsoft.Win32.RegistryHive]::LocalMachine
    $cuKeys = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
    $cuReg = [Microsoft.Win32.RegistryHive]::CurrentUser
}
Process{
    if (!(Test-Connection -ComputerName $Name -count 1 -quiet)) {
        Write-Error -Message "Unable to contact $Name. Please verify its network connectivity and try again." -Category ObjectNotFound -TargetObject $Computer
        Break
    }
    $masterKeys = @()
    $remoteCURegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($cuReg,$computer)
    $remoteLMRegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($lmReg,$computer)
    foreach ($key in $lmKeys) {
        $regKey = $remoteLMRegKey.OpenSubkey($key)
        foreach ($subName in $regKey.GetSubkeyNames()) {
            foreach($sub in $regKey.OpenSubkey($subName)) {
                $masterKeys += (New-Object PSObject -Property @{
                    "ComputerName" = $Name
                    "Name" = $sub.getvalue("displayname")
                    "SystemComponent" = $sub.getvalue("systemcomponent")
                    "ParentKeyName" = $sub.getvalue("parentkeyname")
                    "Version" = $sub.getvalue("DisplayVersion")
                    "UninstallCommand" = $sub.getvalue("UninstallString")
                    "InstallDate" = $sub.getvalue("InstallDate")
                    "RegPath" = $sub.ToString()
                })
            }
        }
    }
    foreach ($key in $cuKeys) {
        $regKey = $remoteCURegKey.OpenSubkey($key)
        if ($regKey -ne $null) {
            foreach ($subName in $regKey.getsubkeynames()) {
                foreach ($sub in $regKey.opensubkey($subName)) {
                    $masterKeys += (New-Object PSObject -Property @{
                        "ComputerName" = $Name
                        "Name" = $sub.getvalue("displayname")
                        "SystemComponent" = $sub.getvalue("systemcomponent")
                        "ParentKeyName" = $sub.getvalue("parentkeyname")
                        "Version" = $sub.getvalue("DisplayVersion")
                        "UninstallCommand" = $sub.getvalue("UninstallString")
                        "InstallDate" = $sub.getvalue("InstallDate")
                        "RegPath" = $sub.ToString()
                    })
                }
            }
        }
    }
    $woFilter = {$null -ne $_.name -AND $_.SystemComponent -ne "1" -AND $null -eq $_.ParentKeyName}
    $props = 'Name','Version','ComputerName','Installdate','UninstallCommand','RegPath'
    $masterKeys = ($masterKeys | Where-Object $woFilter | Select-Object $props | Sort-Object Name)
    $masterKeys
}
End{}
} 
  Get-InstalledSoftware | select-object name | sort-object

【问题讨论】:

  • 您是直接从资源管理器启动脚本,还是从准备就绪的打开的 PowerShell 窗口启动脚本? (您至少应该尝试后者以确定发生了什么。)
  • 将我的脚本保存为 .ps1 文件并直接从资源管理器启动
  • 您是否以管理员身份运行它?
  • 在脚本底部试试Start-Sleep -Seconds 10 :D

标签: powershell


【解决方案1】:

将您的输出发送到某处,而不仅仅是发送到主机窗口。我怀疑窗口关闭或您的暂停命令在检索结果之前启动,因此它们不会发送到主机窗口。

以下对我来说可以作为脚本运行:

Get-InstalledSoftware | Select-Object name | Sort-Object | Out-Gridview

【讨论】:

  • 真的不明白。它仍然是同样的问题。在ISE没有问题,如果我正常启动它,它会立即弹出并关闭
  • 你允许未签名的脚本吗?如果不是,那么它不会作为脚本运行。如果你运行 Get-ExecutionPolicy -List 会得到什么
猜你喜欢
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
  • 2013-05-10
  • 2014-04-20
  • 1970-01-01
相关资源
最近更新 更多