Я очень недавно использую PowerShell, я хотел бы сделать следующий скрипт:
Copy-Item -Path args[0]\index.html -Destination args[0]\print.html
Get-Content args[0]\print.html | %{$_ -replace "root","print"}
Я хочу сделать копию index.html
и назвать ее print.html
.затем я хочу получить содержимое этого print.html
(по тому же пути, что и выше) и заменить все «root» на «print».
У меня проблема в том, что PowerShell не распознает аргументы [0] с помощью Get-Content
, поэтому мне нужен способ передать этот путь динамически, а не жестко, так как путь может меняться в зависимости от пользователя.
Это ошибка:
Get-Content : An object at the specified path args[0]\print.html does not
exist, or has been filtered by the -Include or -Exclude parameter.
At C:\Users\Gabriel\Documents\officine\prepare-print-page.ps1:2 char:1
+ Get-Content args[0]\print.html | %{$_ -replace "root","print"}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
+ FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Обновление:
ОК, извините, я не очень ясно, я видел другой пост stackoverflow и я нашел о параметре, так что теперь мой скрипт выглядит так:
param ([string]$path)
Copy-Item $path\index.html -Destination $path\print.html
Get-Content $path\print.html | %{$_ -replace "root","print"}
и далеемой файл js, когда я создаю свое приложение, я передаю аргумент так:
`prepare-print-page.ps1 ${outputPath}`
переменная outputPath содержит путь к index.html, который выглядит следующим образом: C: \ Users \ Gabriel \ Documents\ myapp \ build
, но это не копирование и создание print.html, я в основном пытаюсь сделать то же самое, что и этот файл bash:
#!/bin/bash
# Duplicate the index page (which contains all the React scripts) and
# rename the id of the main element from "root" to "print". index.jsx
# will then render the correct component.
echo "Generating print.html in directory $1 ..."
cp $1/index.html $1/print.html
sed -i.bak 's/id="root"/id="print"/g' $1/print.html
rm $1/print.html.bak