So verketten Sie Befehle an einer speziellen PowerShell 4-Eingabeaufforderung?

Normalerweise können PowerShell-Befehle mit Semikolons verkettet werden. Folgendes öffnet 2 Notizblöcke:

PS> notepad; notepad

Sie können auch eine komplexere Anweisung verketten:

PS> Add-Type -AssemblyName System.IO.Compression; `
> $src = "C:\aFolder"; $zip="C:\my.zip"; `
> [io.compression.zipfile]::CreateFromDirectory($src, $zip)

Chained PowerShell-Befehle können auch über eine CMD-Befehlszeile aufgerufen werden:

C:\> powershell notepad; notepad

Dieser Beitra beschreibt eine Methode zum Erstellen einer .Net 4.0 PowerShell-Eingabeaufforderung, auch wenn .Net 2.0 das aktive Framework Ihres Betriebssystems ist. Sie erstellen ein .cmd-Skript und führen es aus. Jetzt befinden Sie sich in einer .Net 4.0-Umgebung.

Chaining funktioniert auch an dieser 4.0-Eingabeaufforderung:

C:\> ps4.cmd
PS> notepad; notepad

Und funktioniert auch mit der Standard-CMD-Eingabeaufforderung:

C:\> ps4 notepad; notepad

Dieser Beitra beschreibt eine VorgehensweiseAdd-Type auf einen expliziten Pfadnamen (erforderlich, um über die ps4-Eingabeaufforderung auf 4.0-Assemblys zu verweisen):

Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.IO.Compression.FileSystem\v4.0_4.0.0.0__b77a5c561934e089\System.IO.Compression.FileSystem.dll"

Das funktioniert, auch wenn es an der ps4-Eingabeaufforderung angekettet ist:

C:\> ps4
PS> Add-Type -Path "C:\x\System.IO.Compression.FileSystem.dll"; `
> $src = "C:\x\xl"; $zip="C:\x\xl.zip"; `
> [io.compression.zipfile]::CreateFromDirectory($src, $zip)

Problem Die Verkettung der obigen Anweisung schlägt fehl, wenn ps4 an einer Standard-Eingabeaufforderung gestartet wird (Fehler vollständig am Ende des Beitrags):

C:\> ps4 Add-Type -Path "C:\x\System.IO.Compression.FileSystem.dll"; $src = "C:\x\xl"; $zip="C:\x\xl.zip"; [io.compression.zipfile]::CreateFromDirectory($src, $zip)

Ja, alle oben genannten Methoden funktionieren. Warum? Wie kann ich das zum Laufen bringen?

The term 'C:\x\xl' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:73
+ Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll; $src = C:\x\xl <<<< ; $zip=C:\x\xl.zip;
[io.compression.zipfile]::CreateFromDirectory($src, $zip)
    + CategoryInfo          : ObjectNotFound: (C:\x\xl:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'C:\x\xl.zip' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:91
+ Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll; $src = C:\x\xl; $zip=C:\x\xl.zip <<<< ;
[io.compression.zipfile]::CreateFromDirectory($src, $zip)
    + CategoryInfo          : ObjectNotFound: (C:\x\xl.zip:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Exception calling "CreateFromDirectory" with "2" argument(s): "The path is not
of a legal form."
At line:1 char:138
+ Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll; $src = C:\x\xl; $zip=C:\x\xl.zip;
[io.compression.zipfile]::CreateFromDirectory <<<< ($src, $zip)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Antworten auf die Frage(2)

Ihre Antwort auf die Frage