在 cmd 我寫這個并且它作業(這個命令從檔案中洗掉 N 第一行):
powershell.exe
$file = "C:\Test\file.txt"
$content = Get-Content $file
$content[10..($content.length-1)]|Out-File $file -Force
我想在 C# 上撰寫此代碼,但我的方式不正確。你能解釋一下為什么嗎?
using (PowerShell ps = PowerShell.Create())
{
ps.AddCommand($"$file = \"{fullPathToTxt}\"")
.AddCommand("$content = Get-Content $file")
.AddCommand($"$content[{numLine}..($content.length-1)]|Out-File $file -Force")
.Invoke();
}
uj5u.com熱心網友回復:
而不是 AddCommand,我必須使用 AddScript 方法。
uj5u.com熱心網友回復:
您可以使用此命令執行 powershell 腳本
PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"D:\PSScripts\MyScript.ps1")).Invoke();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/348798.html
上一篇:如何向AzureC#SecretClient提供憑據(DefaultAzureCredential)?它似乎忽略憑據和RBAC并拋出403
