2017-08-05 70 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