Powershell GUI,供用户调用函数

时间:2018-06-07 15:40:32

标签: powershell

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

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

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

Screenshot of my Form

以下是“Run Selected”按钮的代码:

$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。再次 ALL 这些功能可以在PS控制台上运行,但有些功能无法在GUI中运行。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

为了进行健全性检查,请将每个函数分隔到自己的.ps1文件中。

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

使用简单的Get-ChildItem调用管道输出到Out-GridView(作为您的GUI)。 从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

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