Normalerweise können PowerShell-Befehle mit Semikola verkettet werden. Die folgende öffnet 2 Notizblöcke:So verketten Sie Befehle an einer speziellen PowerShell 4-Eingabeaufforderung?
PS> notepad; notepad
Sie können auch die Kette eine komplexere Erklärung:
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 aus einer CMD-Befehlszeile aufgerufen werden:
C:\> powershell notepad; notepad
This post beschreibt eine Methode zum Erstellen einer .Net 4.0 PowerShell-Eingabeaufforderung, auch wenn .Net 2.0 das aktive Framework auf Ihrem Betriebssystem ist. Sie erstellen ein .cmd-Skript und führen das aus. Jetzt befinden Sie sich in einer .NET 4.0-Umgebung.
Chaining arbeitet auch in dieser 4.0-Eingabeaufforderung:
C:\> ps4.cmd
PS> notepad; notepad
und arbeitet auch von Standard CMD-Eingabeaufforderung:
C:\> ps4 notepad; notepad
This post beschreibt einen Weg Add-Type
auf einen expliziten Pfadnamen zu tun (benötigt 4.0 referenzieren Baugruppen von der ps4-Eingabeaufforderung):
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 an der PS4 Prompt gekettet:
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 obige Aussage Verkettungs schlägt fehl, wenn ps4 bei einer Standardeingabeaufforderung (Fehler in voller Höhe am Ende der post) gestartet wird:
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)
Dennoch funktionieren alle oben genannten Methoden. Warum? Wie kann ich das schaffen?
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
genial! Deine Lösung funktioniert: entkomme den doppelten Anführungszeichen oder benutze Singles. Während ich ein richtiges Skript verwenden möchte, sehe ich nicht, wie ich kann. Mein Anwendungsfall ist: Ich muss den gesamten Prozess in einer einzigen Anweisung von VB ausführen. Ich nehme an, wenn ich die PS4.cmd in mehreren Anweisungen aufrufen, werden sie unabhängige Sitzungen sein: Hinzufügen der Assembly in einem Aufruf wird keine Verbindung zu einem nachfolgenden Aufruf haben. Außerdem muss ich die Befehle in den VB-Code einbetten, so dass es einfacher ist, sie mit ';' zu verketten als ein Skript in VB einzubetten, das Skript in einer Batch-Datei zu speichern und dann dieses Skript auszuführen - aber ich begrüße Vorschläge. Danke! –
Ich verlinkte Ihre Antwort hier: http://stackoverflow.com/a/39800779/209942 –