2017-08-05 75 views
0

UPDATE2不同的CMD不同的行为

现在,当我知道,X32是我调试到使用powershell_ise_x32剧本和发现,这是$Word.Documentsnull问题。 因此,Powershell-API for Word在x32 PowerShell中有不同的行为,然后是64位。

enter image description here


更新:发生

错误,使用PowerShell的X32时并发生不是在PowerShell的64位。那是真的。 Powershell x32被执行是因为我从Total Commander 32bit开始。

现在的问题是 - 为什么32位和64位PowerShell有不同的行为?


最初的问题:

我写了一个PowerShell脚本,以将我WordDocuments并将它们合并成一个。
我写了一个批处理脚本,启动这个PowerShell脚本。

当我执行直接在“PowerShell ISE中工作罚款脚本中的脚本。

当我通过上下文菜单中执行批处理脚本为管理员,脚本报告错误。在这种情况下,执行C:\ WINDOWS \ SysWOW64 \ cmd.exe

当我执行我的系统上找到以管理员身份另一个cmd.exe的 - 一切工作罚款: “C:\ WINDOWS \ WinSxS文件\ Amd64_microsoft窗口 - commandprompt_31bf3856ad364e35_10.0.15063.0_none_9c209ff6532b42d7 \ cmd.exe的

为什么我在不同的cmd.exe中有不同的行为?什么是不同的cmd.exe?

enter image description here

enter image description here

enter image description here


批处理脚本:

cd /d "%~dp0" 

powershell.exe -noprofile -executionpolicy bypass -file "%~dp0%DocxToPdf.ps1" 
pause 

PowerShell脚本

$FilePath = $PSScriptRoot 
$Pdfsam = "D:\Programme\PDFsam\bin\run-console.bat" 




$Files = Get-ChildItem "$FilePath\*.docx" 
$Word = New-Object -ComObject Word.Application 
if(-not $?){ 
    throw "Failed to open Word" 
} 

# Convert all docx files to pdf 
Foreach ($File in $Files) { 
    Write-Host "Word Object: " $Word 
    Write-Host "File Object: " $Word $File 
    Write-Host "FullName prop:" $File.FullName 

    # open a Word document, filename from the directory 
    $Doc = $Word.Documents.Open($File.FullName) 

    # Swap out DOCX with PDF in the Filename 
    $Name=($Doc.FullName).Replace("docx","pdf") 

    # Save this File as a PDF in Word 2010/2013 
    $Doc.SaveAs([ref] $Name, [ref] 17) 
    $Doc.Close() 
} 

# check errors 
if(-not $?){ 
    Write-Host("Stop because an error occurred") 
    pause 
    exit 0 
} 

# wait until the conversion is done 
Start-Sleep -s 15 


# Now concat all pdfs to one single pdf 
$Files = Get-ChildItem "$FilePath\*.pdf" | Sort-Object 

Write-Host $Files.Count 

if ($Files.Count -gt 0) { 

    $command = "" 
    Foreach ($File in $Files) { 
     $command += " -f " 
     $command += "`"" + $File.FullName + "`"" 
    } 

    $command += " -o `"$FilePath\Letter of application.pdf`" -overwrite concat" 
    $command = $Pdfsam + $command 
    echo $command 

    $path = Split-Path -Path $Pdfsam -Parent 
    cd $path 

    cmd /c $command 

}else{ 
    Write-Host "No PDFs found for concatenation" 
} 




Write-Host -NoNewLine "Press any key to continue..."; 
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown"); 
+2

阅读Microsoft有关[文件系统重定向器](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187.aspx)和其他WoW64页面的文章。当启动它的应用程序是一个32位应用程序时,将启动目录'%SystemRoot%\ Syswow64'中的32位'cmd.exe'。在32位应用程序中,可以执行'%SystemRoot%\ Sysnative \ cmd.exe'来在64位Windows上启动64位'cmd.exe'。另请参阅['以管理员身份运行'更改批处理文件当前目录(有时)]的回答](https://stackoverflow.com/a/31655249/3074564)。 – Mofi

+0

对不起,但是我的问题与32位和64位版本的cmd存在有什么关系?并且在我的批处理脚本中通过'cd/d“%〜dp0”'修改目录。 – Skip

+1

WinSxS目录中应该有两个cmd.exe文件,它们都与其正常位置硬连接,64位版本在System32和SysWOW64中的32位版本。切勿直接从系统的WinSxS目录中使用文件。 – eryksun

回答

0

我发现$PSScriptRoot是不可靠的。

$FilePath = $PSScriptRoot; 
$CurLocation = Get-Location; 
$ScriptLocation = Split-Path $MyInvocation.MyCommand.Path 

Write-Host "FilePath = [$FilePath]"; 
Write-Host "CurLocation = [$CurLocation]"; 
Write-Host "ScriptLocation = [$ScriptLocation]"; 

结果:

O:\Data>powershell ..\Script\t.ps1 
FilePath = [] 
CurLocation = [O:\Data] 
ScriptLocation = [O:\Script] 

至于各种cmd.exe实现之间的差异,我真的不能回答这个问题。我应该认为它们在功能上是相同的,但也许有32/64位差异很重要。

0

使用PowerShell x32时发生错误,并且在PowerShell 64位上发生错误。

我使用powershell_ise_x32调试脚本,发现,$Word.Documentsnull

这是因为在我的系统上安装了Word 64bit。

+0

'$ Word = New-Object -ComObject Word.Application'是否成功?如果不是,也许它是一个进程内COM组件,它只能在64位DLL中使用。 – eryksun

+0

它成功了。看看screnshot,$ Word不是null – Skip