【问题标题】:Powershell GUI for users to call functionsPowershell GUI 供用户调用函数
【发布时间】:2018-06-08 03:29:00
【问题描述】:

我希望用户能够选择他们想要运行的功能,然后单击按钮查看输出。

我的大部分功能都可以使用它,但其中一些功能似乎没有完成。我基本上创建了一个数组,其中包含他们选择的所有函数的名称,然后使用ForEach 循环来运行他们选择的每个函数。

奇怪的是我的一些函数似乎没有做任何事情,但如果我将它们输入到 powershell 控制台,它们就可以正常工作。

Screenshot of my Form

这是“运行选定”按钮的代码:

$RunSelectedButton_Click = {
    Import-Module SHB_Testing_Module 

    [array]$SelectedFunctions = @()

    If ($ActiveClientCheck.Checked -eq $true) {$SelectedFunctions += "ActiveClientCheck"}
    If ($ClientSoftwareCheck.Checked -eq $true) {$SelectedFunctions += "ClientSoftwareCheck"}
    If ($CrashControlKeyCheck.Checked) {$SelectedFunctions += "CrashControlKeyCheck"}
    If ($DiskCheck.Checked) {$SelectedFunctions += "DiskCheck"}
    If ($DriverCheck.Checked) {$SelectedFunctions += "DriverCheck"}
    If ($GPOCheck.Checked) {$SelectedFunctions += "GPOCheck"}
    If ($ListInstalledSoftware.Checked) {$SelectedFunctions += "ListInstalledSoftware"}
    If ($ListMSHotfix.Checked) {$SelectedFunctions += "ListMSHotfix"}
    If ($MSOfficePatchCheck.Checked) {$SelectedFunctions += "MSOfficePatchCheck"}
    If ($PDFDefaultSoftwareCheck.Checked) {$SelectedFunctions += "PDFDefaultSoftwareCheck"}
    If ($SCCMVersionCheck.Checked) {$SelectedFunctions += "SCCMVersionCheck"}
    If ($WebsiteCheck.Checked) {$SelectedFunctions += "WebsiteCheck"}
    If ($RunAllFunctions.Checked) {$SelectedFunctions = @('ActiveClientCheck', 'ClientSoftwareCheck', 'DiskCheck', 'DriverCheck', 'GPOCheck', 'ListInstalledSoftware', 'ListMSHotfix', 'MSOfficePatchCheck', 'PDFDefaultSoftwareCheck', 'SCCMVersionCheck', 'WebsiteCheck')}

    ForEach ($FunctionName in $SelectedFunctions) {
        Write-Host $FunctionName -ForegroundColor Yellow
        Invoke-Expression $FunctionName
        Start-Sleep 10
        Write-Host ""
    }
}

. (Join-Path $PSScriptRoot 'MainForm.designer.ps1')

$SHBTestingTool.ShowDialog() | Out-Null

我尝试将Invoke-Expression 行通过管道传递给Out-Null,并使用& 来调用函数而不是Invoke-Expression

我得到相同的结果,它会运行大部分功能,但有些不起作用。

那些不会从 PS 控制台运行的。以下是不想工作的函数:

 Function DriverCheck
    {
    Get-WmiObject Win32_PnPSignedDriver| Select-Object devicename, driverversion | Where-Object {$_.devicename -like "*"}
    }

   #http://jongurgul.com/blog/installedsoftware/ 
   Function ListInstalledSoftware{
        Param([String[]]$Computers)
        If (!$Computers) {$Computers = $ENV:ComputerName}
        $Base = New-Object PSObject;
        $Base | Add-Member Noteproperty ComputerName -Value $Null;
        $Base | Add-Member Noteproperty Name -Value $Null;
        $Base | Add-Member Noteproperty Publisher -Value $Null;
        $Base | Add-Member Noteproperty InstallDate -Value $Null;
        $Base | Add-Member Noteproperty EstimatedSize -Value $Null;
        $Base | Add-Member Noteproperty Version -Value $Null;
        $Base | Add-Member Noteproperty Wow6432Node -Value $Null;
        $Results =  New-Object System.Collections.Generic.List[System.Object];

        ForEach ($ComputerName in $Computers){
            $Registry = $Null;
            Try{$Registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$ComputerName);}
            Catch{Write-Host -ForegroundColor Red "$($_.Exception.Message)";}

            If ($Registry){
                $UninstallKeys = $Null;
                $SubKey = $Null;
                $UninstallKeys = $Registry.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall",$False);
                $UninstallKeys.GetSubKeyNames()|ForEach-Object{
                    $SubKey = $UninstallKeys.OpenSubKey($_,$False);
                    $DisplayName = $SubKey.GetValue("DisplayName");
                    If ($DisplayName.Length -gt 0){
                        $Entry = $Base | Select-Object *
                        $Entry.ComputerName = $ComputerName;
                        $Entry.Name = $DisplayName.Trim();
                        $Entry.Publisher = $SubKey.GetValue("Publisher");
                        [ref]$ParsedInstallDate = Get-Date
                        If ([DateTime]::TryParseExact($SubKey.GetValue("InstallDate"),"yyyyMMdd",$Null,[System.Globalization.DateTimeStyles]::None,$ParsedInstallDate)){
                        $Entry.InstallDate = $ParsedInstallDate.Value
                        }
                        $Entry.EstimatedSize = [Math]::Round($SubKey.GetValue("EstimatedSize")/1KB,1);
                        $Entry.Version = $SubKey.GetValue("DisplayVersion");
                        [Void]$Results.Add($Entry);
                    }
                }

                    If ([IntPtr]::Size -eq 8){
                    $UninstallKeysWow6432Node = $Null;
                    $SubKeyWow6432Node = $Null;
                    $UninstallKeysWow6432Node = $Registry.OpenSubKey("Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall",$False);
                        If ($UninstallKeysWow6432Node) {
                            $UninstallKeysWow6432Node.GetSubKeyNames()|ForEach-Object {
                            $SubKeyWow6432Node = $UninstallKeysWow6432Node.OpenSubKey($_,$False);
                            $DisplayName = $SubKeyWow6432Node.GetValue("DisplayName");
                            If ($DisplayName.Length -gt 0){
                                $Entry = $Base | Select-Object *
                                $Entry.ComputerName = $ComputerName;
                                $Entry.Name = $DisplayName.Trim();
                                $Entry.Publisher = $SubKeyWow6432Node.GetValue("Publisher");
                                [ref]$ParsedInstallDate = Get-Date
                                If ([DateTime]::TryParseExact($SubKeyWow6432Node.GetValue("InstallDate"),"yyyyMMdd",$Null,[System.Globalization.DateTimeStyles]::None,$ParsedInstallDate)){
                                $Entry.InstallDate = $ParsedInstallDate.Value
                                }
                                $Entry.EstimatedSize = [Math]::Round($SubKeyWow6432Node.GetValue("EstimatedSize")/1KB,1);
                                $Entry.Version = $SubKeyWow6432Node.GetValue("DisplayVersion");
                                $Entry.Wow6432Node = $True;
                                [Void]$Results.Add($Entry);
                                }
                            }
                        }
                    }
            }
        }
        $Results
    }

    Function ListMSHotfix
    {
        $outputs = Invoke-Expression "wmic qfe list"
        $outputs = $outputs[1..($outputs.length)]


        foreach ($output in $Outputs) {
            if ($output) {
                $output = $output -replace 'y U','y-U'
                $output = $output -replace 'NT A','NT-A'
                $output = $output -replace '\s+',' '
                $parts = $output -split ' '
                if ($parts[5] -like "*/*/*") {
                    $Dateis = [datetime]::ParseExact($parts[5], '%M/%d/yyyy',[Globalization.cultureinfo]::GetCultureInfo("en-US").DateTimeFormat)
                } elseif (($parts[5] -eq $null) -or ($parts[5] -eq ''))
                {
                    $Dateis = [datetime]1700
                }

                else {
                    $Dateis = get-date([DateTime][Convert]::ToInt64("$parts[5]", 16))-Format '%M/%d/yyyy'
                }
                New-Object -Type PSObject -Property @{
                    KBArticle = [string]$parts[0]
                    Computername = [string]$parts[1]
                    Description = [string]$parts[2]
                    FixComments = [string]$parts[6]
                    HotFixID = [string]$parts[3]
                    InstalledOn = Get-Date($Dateis)-format "dddd d MMMM yyyy"
                    InstalledBy = [string]$parts[4]
                    InstallDate = [string]$parts[7]
                    Name = [string]$parts[8]
                    ServicePackInEffect = [string]$parts[9]
                    Status = [string]$parts[10]
                }
            }
        }
    }

    function MSOfficePatchCheck
    {
    $a = Get-WmiObject -Class "win32_quickfixengineering"
    Write-Output $a
    }

似乎Get-WmiObject 是其中的一个共同主题,但我的一些确实可以使用的函数使用相同的 cmdlet。再次所有这些功能在 PS 控制台上工作,但有些不能在 GUI 上工作。有什么想法吗?

【问题讨论】:

  • 我将从一些基本的故障排除开始。放入一些 Write-Host 或 Out-File 语句,以确保代码完全运行,并查看函数中是否正在生成数据。
  • 它实际上在 While 循环中有一个 Write-Host 输出,它正在输出未运行的函数的名称。另外,如果我只输入函数的名称,函数就会运行。对于不运行的功能,我绝对没有错误输出。起初我认为他们只是需要更多的时间来完成,所以我添加了 start-sleep 行。即使在 30 岁时它仍然没有显示任何内容,所以我认为不是这样。函数本身一定有一些东西。我将尝试在函数本身中添加一些 Write host 行,看看它们是否在做任何事情。
  • 我在函数中添加了一些 Write-Host 行,它们确实出现了,所以函数似乎正在启动,并且可能正在运行它,但只是没有给我输入PS控制台中的函数名。
  • 我还认为它可能不喜欢 pipped 命令,因为所有不起作用的功能至少有几个管道。所以我在 DriverCheck 函数中注释掉了管道,但仍然没有输出。从控制台它工作正常。

标签: powershell


【解决方案1】:

我建议采取更直接的方法来解决问题。

为了进行完整性检查,请将您的每个函数分离到它们自己的 .ps1 文件中。

将每个 .ps1 文件放在同一个文件夹中。

使用通过管道传输到 Out-GridView(作为您的 GUI)的简单 Get-ChildItem 调用。 从 OGV 中选择所需的函数并在 ForLoop 中处理它们。

$RunActions = Get-ChildItem -Path D:\Scripts\*.* | Out-GridView -Title 'Select the action items to run' -PassThru
$RunActions.Fullname | %{ &$_}

这是一篇关于我正在谈论的更详细内容的文章。

使用 Out-GridView 创建简单的 GUI 界面 http://mikefrobbins.com/2014/09/11/creating-a-simplistic-gui-interface-with-out-gridview

如果他们从那个简单的方法运行,那么它可以用作检查/指针/指示器来查看可能是根本原因的图片的其他部分。

【讨论】:

  • 感谢您的想法,我会尝试并发布结果!
  • 我为每个函数创建了一个不同的脚本,并且使用您的 Get-Child/GridView 想法确实允许所有函数工作。完成该测试后,我想我对为什么它在我的 GUI 中不起作用感到更加茫然。当我有时间(希望今晚)时,我会查看您共享的 Mike Robbins 链接。
  • 非常感谢后记!我们最终只使用了这种技术,而不是我创建的自定义 GUI。它要简单得多,并且所有功能都可以运行。此外,如果我们在未来的任何时候添加新脚本,我们不必对主脚本进行任何更改,只需将新脚本添加到文件夹中即可。
猜你喜欢
  • 1970-01-01
  • 2011-05-09
  • 2015-03-17
  • 1970-01-01
  • 2016-07-31
  • 2011-08-13
  • 2020-07-10
  • 2017-05-15
  • 2018-07-03
相关资源
最近更新 更多