在PowerShell中同时运行后台后台作业非常简单,但我似乎无法使其与新(在v3中)cmdlet Invoke-WebRequest一起使用。
我有数千个文件正在通过PowerShell脚本下载。效果很好,但是连续进行需要几天的时间:
for($f=0;$f -lt $urlList.Count;$f++)
{
$remote = $urlList[$f] + $fileList[$f]
$local = 'C:\folder\' + $fileList[$f]
Invoke-WebRequest $remote -Method Get -OutFile $local -UserAgent FireFox
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过利用“ AsJob”方法进行多次尝试,但是它们要么出错,要么完全正常,但是没有保存任何本地文件。这是后者的一个示例:
for($f=0;$f -lt $urlList.Count;$f++)
{
$remote = $urlList[$f] + $fileList[$f]
$local = 'C:\folder\' + $fileList[$f]
$command = "Invoke-WebRequest $remote -Method Get -OutFile $local -UserAgent FireFox"
Start-Job {Invoke-Expression -Command $command}
}
Get-Job|Wait-Job
Run Code Online (Sandbox Code Playgroud)
输出示例:
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
339 Job339 BackgroundJob Running True localhost Invoke-Expression …Run Code Online (Sandbox Code Playgroud) powershell download background-process cmdlet powershell-3.0
在我在教程和书籍中看到的大多数示例中,默认情况下几乎从不使用 -LiteralPath 选项(似乎首选 -Path 选项)。因为 -LiteralPath 选项允许使用保留字符(例如 []),我不明白为什么不经常使用它(如果不是,则一直使用)。是因为更喜欢手动转义保留字符,因为它具有很高的性能成本,因为不是所有的cmdlet都支持这个选项还是因为别的什么?
我正在用 C# 为 PowerShell 编写一个 Cmdlet。我正在继承 aCmdlet而不是 a PSCmdlet。
有没有办法从 PowerShell 获取当前目录?我可以PSCmdlet使用 using这样做GetVariableValue("pwd")。但是在 Cmd 类中我没有那个可用。
Environment.CurrentDiretory 将我指向 powershell 启动的路径,而不是 PowerShell 本身当前所在的位置。
编辑
例子:
我通过 - say - 启动powershell powershell_ise.exe。它在C:\Windows\System32\WindowsPowerShell\v1.0. 然后我使用更改路径cd c:\my\folder并运行我的命令Do-Something。在“Do-Something”(C# 端)的实现中,我希望能够检索当前路径 => c:\my\folder。
如果可能,我想避免使用PSCmdlet.
我试图编写一个简单的PS脚本来检查大型.txt日志文件中的短字符串:“ SRVE0242I:”
$lines = Select-String -Path $logDir -Pattern "SRVE0242I:" | Select-Object line | Out-String
Run Code Online (Sandbox Code Playgroud)
但是在输出时,它仅显示以下内容:
行
[28/06/17 13:48:27:839] 00000020 ServletWrappe I SRVE0242I:[User] [User] [com_xxxxxxx _...
而不是全线。提取的字符数是否有限制?我找不到有关Select-String cmdlet的任何限制的任何信息。有没有更好的方法可以做到这一点,所以我不会a)在行列表中拉标题“ Line”(不想真的为这种简单的输出创建表格格式),b)获得整行当我提取信息时?
我有一个查询,例如当我尝试执行包含带空格的路径的表达式时,出现如下错误。
代码:
$path="E:\Test\My space\Log"
Invoke-Expression $path
E:\Test\My: The term 'E:\test\My' 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:1
+ E:\Test\My space\Log
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (E:\Test\My :String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)
你能帮我解决这个问题吗?
如果我有一个文本文件,C:\ USER\Documents\Collections\collection.txt具有以下信息:
collectionA.json
collectionB.json
collectionC.json
collectionD.json
Run Code Online (Sandbox Code Playgroud)
我想知道如何通过Powershell,我能够将文本文件中的每一行存储为数组的元素,如...
array arrayFromFile = new Array;
foreach(line x in collection.txt)
{
arrayFromFile.Add(x);
}
Run Code Online (Sandbox Code Playgroud)
..最终目标是:
foreach(string x in arrayFromFile)
{
newman run x;
}
Run Code Online (Sandbox Code Playgroud)
我为这个看似简单的问题道歉 - 我以前从未处理过Powershell.
是否可以从powershell打开资源管理器窗口并将在资源管理器中选择的路径存储到变量?
从powershell打开资源管理器窗口
PS C:>探险家
以下代码是一个 powerShell 脚本,用于发送 POST 请求并获取响应并将其存储到$response. 当我尝试获取表单值并将其存储到 时$wa,它会自动打开浏览器。
$destination="https://adfs.somedomain.com/adfs/ls/";
$response = '';
$username = 'some username';
$password = 'some password';
$AuthMethod = 'FormsAuthentication';
$postParams = @{
destination="$destination";
flags='0';
forcedownlevel='0';
trusted='0'
UserName="$($username)";
Password="$($password)";
AuthMethod = "$($AuthMethod)";
isUtf8='1'
}
$url = "$($destination)?wa=wsignin1.0&wtrealm=urn:federation:cas"
$response = Invoke-WebRequest -uri $url -Method POST -body $postParams;
$wa = $response.InputFields.value[0];
Run Code Online (Sandbox Code Playgroud)
如何让脚本不打开浏览器?
注意:它发送 post 请求,该请求通过 get 请求返回到相同 uri 的重定向,并使用我试图存储在变量中的一些表单值进行响应。
我已经启动了PowerShell cmdlet,并想提供参数的帮助消息。我试过使用ParameterAttribute.HelpMessage这个:
[Cmdlet(VerbsCommon.Get, "Workspace", SupportsShouldProcess = true)]
public class GetWorkspace : PSCmdlet
{
[Parameter(
Mandatory = true,
Position = 1,
HelpMessage = "The path to the root directory of the workspace.")]
public string Path { get; set; }
protected override void ProcessRecord()
{
base.ProcessRecord();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用PowerShell Get-Help命令时,它不会显示该参数的HelpMessage:
get-help Get-Workspace -det
NAME
Get-Workspace
SYNTAX
Get-Workspace [-Path] <string> [-WhatIf] [-Confirm] [<CommonParameters>]
PARAMETERS
-Confirm
-Path <string>
-WhatIf
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种从“读取主机” cmdlet隐藏用户输入的方法。
我知道我可以使用-assecurestring做到这一点,但是我想将输入保存为纯文本格式。
有没有办法做到这一点?