我想运行一个外部进程并将其命令输出捕获到PowerShell中的变量.我目前正在使用这个:
$params = "/verify $pc /domain:hosp.uhhg.org"
start-process "netdom.exe" $params -WindowStyle Hidden -Wait
Run Code Online (Sandbox Code Playgroud)
我已经确认命令正在执行但我需要将输出捕获到变量中.这意味着我无法使用-RedirectOutput,因为这只会重定向到文件.
我需要将带有特殊字符的密码从 powershell 脚本automation.ps1
传递到批处理脚本batch_script.bat
,然后通过管道将其传输到main.py
. 从batch_script.bat
to管道main.py
工作正常,即身份验证成功。但是,当我运行上述整个过程时,身份验证失败,但echo
ing the password 显示正确的密码字符串。
我的猜测是特殊字符存在问题。传递这些字符串的安全方法是什么?
背景
我想通过 Python 脚本从某些外部源自动执行每日下载main.py
。此过程需要密码。所以我写了一个batch_script.bat
在提示输入密码时将密码传递给 Python 脚本的方法。但是,我不想将密码作为纯文本存储在批处理脚本中,所以我加密了密码并编写了另一个层automation.ps1
来解密密码并将其作为纯文本传递给batch_script.bat
.
自动化.ps1
# get password
$securePassword = Get-Content "<file_path>" | ConvertTo-SecureString
$credentials = New-Object System.Management.Automation.PsCredential("<user_name>",$securePassword)
$unsecurePassword = ($credentials).GetNetworkCredential().Password
# call script
$script = "<path>\batch_script.bat"
$args = @("`"<user name>`"","`"$unsecurePassword`"")
start-process $script $args
Run Code Online (Sandbox Code Playgroud)
批处理脚本.bat
(我知道在这个例子中我丢弃了传递的用户名,只是想保留我传递多个参数的事实,以防与它有任何相关性)
@echo off
SET username=%~1
SET password=%~2
echo(%password%|python main.py
Run Code Online (Sandbox Code Playgroud) 我正在创建一个 PowerShell,它使用以下命令运行一个简单的 Java 程序:
Start-Process -filepath .\java.exe -ArgumentList "Input" -NoNewWindow
Run Code Online (Sandbox Code Playgroud)
在本例中,Input 类只是接受并打印一个字符串。
我期望长度大于cmd.exe
8191 个字符限制的字符串,因此我需要在 PS 中运行它。
当前场景:
Input
接受大于 8191 个字符的字符串。我-Wait
也尝试添加,但运气不佳。
任何想法?
我正在尝试将 Node.js 输出通过管道传输到 preatty-pino
node .\dist\GameNode.js | pino-pretty
在 CMD 中运行它我得到了我的格式化输出但是在 powershell 中运行它我什么也得不到。我读到 Powershell 在管道时正在使用对象,所以我试过了
node .\dist\GameNode.js | Out-String -Stream | pino-pretty
但这也行不通。
为什么它可以在 CMD 中工作,而不能在 Powershell 中工作?谢谢 :)
我正在创建 PowerShell 脚本来自动构建 Angular CLI 项目。当我在 .ps1 文件中使用此命令时:
Start-Process -FilePath ng build -WorkingDirectory D:\pathToAngularProject -Wait
Run Code Online (Sandbox Code Playgroud)
它工作完美,但创建的 JavaScript 构建并不丑陋。为了能够创建生产版本,我需要添加“--prod”参数。但是当我更改上面的示例时:
Start-Process -FilePath ng build -ArgumentList '--prod' -WorkingDirectory D:\pathToAngularProject -Wait
Run Code Online (Sandbox Code Playgroud)
有谁建议我如何在 PowerShell 脚本中向“ng build”命令添加参数?
我想用 Start-Process 和 -Wait 调用 ffmpeg,$ArgumentList 有问题
function convertgif {
$ArgumentList = @'
-i $($args[0]) -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" "$([IO.Path]::ChangeExtension($args[0], 'mp4'))"
'@
Start-Process -FilePath "C:\ffmpeg\bin\ffmpeg.exe" -ArgumentList $ArgumentList -Wait -NoNewWindow;
}
Run Code Online (Sandbox Code Playgroud)
我找不到如何修复错误
$args[0]: No such file or directory
Run Code Online (Sandbox Code Playgroud) 使用该程序可以得到您的支持或 GYB。我运行以下命令
Start-Process -FilePath 'C:\Gyb\gyb.exe' -ArgumentList @("--email <Email Address>", "--action backup", "--local-folder $GYBfolder", "--service-account", "--batch-size 4") -Wait
Run Code Online (Sandbox Code Playgroud)
问题是,当该过程完成时,我的脚本未完成。
$GYBfolder = $GYBfolder.Replace('"', "")
$output = [PSCustomObject]@{
Name = $SourceGYB
Folder = $GYBfolder
}
$filename = "C:\reports\" + $SourceGYB.Split("@")[0] + "_Backup.csv"
$output | Export-Csv $filename -NoTypeInformation | Format-Table text-align=left -AutoSize
Return $filename
Run Code Online (Sandbox Code Playgroud)
由于某种原因,脚本在返回之前停止。我很想知道我是否应该使用不同的命令来运行 GYB?关于为什么脚本不处理返回的任何想法?
这是在命令提示符下工作的命令。
C:\Temp\Agent.exe CustomerId={9c0-4ab1-123-102423a} ActivationId={9c0-4ab1-123-102423a} WebServiceUri=https://Agent/
Run Code Online (Sandbox Code Playgroud)
这是错误。(我尝试过调用命令和参数,但我认为 { 引起了问题。
错误:Agent.exe:已指定命令参数。
我尝试遵循 powershell-command,但随后打开了 1000 个窗口并且 powershell ISE 崩溃了。有没有办法在后台运行批处理文件 1000 次?有没有更聪明的方法来获得平均执行时间?
这是我试过的代码:
cd C:\scripts
Measure-Command {
for($i = 0;$i -lt 1000;$i++){
Start-Process -FilePath "C:\scripts\open.bat"
}
}
Run Code Online (Sandbox Code Playgroud) powershell ×9
batch-file ×2
cmd ×2
angular ×1
angular-cli ×1
average ×1
max ×1
pipe ×1
process ×1
windows ×1