【发布时间】:2018-06-08 03:29:00
【问题描述】:
我希望用户能够选择他们想要运行的功能,然后单击按钮查看输出。
我的大部分功能都可以使用它,但其中一些功能似乎没有完成。我基本上创建了一个数组,其中包含他们选择的所有函数的名称,然后使用ForEach 循环来运行他们选择的每个函数。
奇怪的是我的一些函数似乎没有做任何事情,但如果我将它们输入到 powershell 控制台,它们就可以正常工作。
这是“运行选定”按钮的代码:
$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