电源壳| 合并对象 Excel | 打开前景?

XXI*_*aXX 5 com excel powershell

问题仅在未配置邮件帐户时出现 - 但我仍然希望有解决方案

我需要帮助。我发现这个小脚本有一个非常奇怪的习惯。我完全不知道为什么会发生这种情况。如果我运行下面发布的代码,Microsoft Outlook 将启动。而只要我不终止展望过程中的脚本被卡住了!为什么这段代码会启动 Outlook?我搞不清楚了!

$Path     = "C:\test.xls"
#Excelvar:
$Row                 = [int] 2
$Excel               = New-Object -ComObject Excel.Application
$Excel.Visible       = $true
$Excel.DisplayAlerts = $false
        #Sheets:
        $ADUsers     = "Active Directory Users"
        $Groups      = "Create Groups"
        $UsertoGroup = "User to groups"
        $DNS         = "DNS"
#$Worksheet = $Workbook.Sheets.Add()
$checkxls = test-path -pathtype Any $Path
if ($checkxls -eq $false) {  
    $wb = $Excel.Workbooks.Add()

         $wb.Worksheets.add()

$wb.SaveAs($Path)
    $wb.Close()
    $Excel.Quit()
Run Code Online (Sandbox Code Playgroud)

提前谢谢!

Outlook 终止后的 Powershell 输出:

Application                       : Microsoft.Office.Interop.Excel.ApplicationClass
Creator                           : 1480803660
Parent                            : System.__ComObject
CodeName                          : 
_CodeName                         : 
Index                             : 1
Name                              : Tabelle4
Next                              : System.__ComObject
OnDoubleClick                     : 
OnSheetActivate                   : 
OnSheetDeactivate                 : 
PageSetup                         : System.__ComObject
Previous                          : 
ProtectContents                   : False
ProtectDrawingObjects             : False
ProtectionMode                    : False
ProtectScenarios                  : False
Visible                           : -1
Shapes                            : System.__ComObject
TransitionExpEval                 : False
AutoFilterMode                    : False
EnableCalculation                 : True
Cells                             : System.__ComObject
CircularReference                 : 
Columns                           : System.__ComObject
ConsolidationFunction             : -4157
ConsolidationOptions              : {False, False, False}
ConsolidationSources              : 
DisplayAutomaticPageBreaks        : False
EnableAutoFilter                  : False
EnableSelection                   : 0
EnableOutlining                   : False
EnablePivotTable                  : False
FilterMode                        : False
Names                             : System.__ComObject
OnCalculate                       : 
OnData                            : 
OnEntry                           : 
Outline                           : System.__ComObject
Rows                              : System.__ComObject
ScrollArea                        : 
StandardHeight                    : 15
StandardWidth                     : 10,71
TransitionFormEntry               : False
Type                              : -4167
UsedRange                         : System.__ComObject
HPageBreaks                       : System.__ComObject
VPageBreaks                       : System.__ComObject
QueryTables                       : System.__ComObject
DisplayPageBreaks                 : False
Comments                          : System.__ComObject
Hyperlinks                        : System.__ComObject
_DisplayRightToLeft               : False
AutoFilter                        : 
DisplayRightToLeft                : False
Scripts                           : System.__ComObject
Tab                               : System.__ComObject
MailEnvelope                      : 
CustomProperties                  : System.__ComObject
SmartTags                         : System.__ComObject
Protection                        : System.__ComObject
ListObjects                       : System.__ComObject
EnableFormatConditionsCalculation : True
Sort                              : System.__ComObject
PrintedCommentPages               : 0
Run Code Online (Sandbox Code Playgroud)

Chr*_*ber 5

这里的问题是,当您运行$wb.Worksheets.add()它时,它会将新工作表返回到管道(这就是当您终止 Outlook 时显示工作表属性的原因)。

我相信工作表的 MailEnvelope 属性是导致 Outlook 打开的原因(如果您存储工作表并返回 MailEnvelope 属性,则会发生相同的行为)。

为了解决这个问题,你可以存储在返回的纸张,使用外空cmdlet或投工作表无效:$ws = $wb.Worksheets.add()$wb.Worksheets.add() | Out-Null[void] $wb.Worksheets.add()