标签: powershell

使用Powershell将文件的内容转换为可以使用JSON传输的字符串

如何将文本文件的内容转换为字符串,然后将此字符串插入JSON文件?

例如,如果文件包含:

this
is
a
sample
file
Run Code Online (Sandbox Code Playgroud)

该脚本将生成:

"this\r\nis\r\na\r\nsample\r\nfile"
Run Code Online (Sandbox Code Playgroud)

要插入JSON模板:

"something":"<insertPoint>"
Run Code Online (Sandbox Code Playgroud)

生产:

"something":"this\r\nis\r\na\r\nsample\r\nfile"
Run Code Online (Sandbox Code Playgroud)

我正在使用Powershell 5并设法加载文件,生成一些JSON并通过运行来插入它:

# get contents and convert to JSON
$contentToInsert = Get-Content $sourceFilePath -raw | ConvertTo-Json
# write in output file
(Get-Content $outputFile -Raw).replace('<insertPoint>', $contentToInsert) | Set-Content $outputFile
Run Code Online (Sandbox Code Playgroud)

但是,还添加了许多其他不需要的字段.

"something":"{
  "value":  "this\r\nis\r\na\r\nsample\r\nfile"
  "PSPath":  "C:\\src\\intro.md",
  "PSParentPath":  "C:\\src",
  "PSChildName":  "intro.md",
    etc...
Run Code Online (Sandbox Code Playgroud)

最后,我正在尝试通过JSON将小型富文本段发送到网页,但希望使用Markdown在本地编辑和存储它们.如果这没有意义,并且有更好的方式发送这些,那么请让我知道.

powershell json

3
推荐指数
1
解决办法
714
查看次数

如何将URL字符串拆分/解析为对象?

我正在尝试将URL拆分为一个对象。

$url = "https://api.somedomain.com/v2/direct-access/producing-entities-details?entity_id=104194825&format=json&page=1
Run Code Online (Sandbox Code Playgroud)

理想的结果是

PS C:\WINDOWS\system32> $url.api 
https://api.somedomain.com/v2/direct-access

PS C:\WINDOWS\system32> $url.dataset
producing-entities-details

PS C:\WINDOWS\system32> $url.params
entity_id=104194825&format=json&page=1 
Run Code Online (Sandbox Code Playgroud)

我确定可以与正则表达式结合使用,但是我也想知道是否可以仅使用内置的PowerShell功能来完成。

string url powershell object

3
推荐指数
2
解决办法
362
查看次数

如何将不同的Powershell宏合并为一个?

由于.NET Core已经出局,我一直在使用越来越多的命令行作为工作方式,一般来说我使用了很多PowerShell.

虽然Visual Studio Code非常符合命令行,但对于它的大哥来说却不能说是相同的.

为了改善这种情况,我在$ PROFILE文件中添加了以下内容.

function Execute-VisualStudioAsAdmin
{
    if ($args.Count -gt 0)
    {
        Start-Process "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\devenv.exe" $args -Verb RunAs
    }
    else
    {
        Start-Process "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\devenv.exe" -Verb RunAs
    }
}

Set-Alias vsa Execute-VisualStudioAsAdmin -Option ReadOnly

Set-Alias vs "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\devenv.exe" -Option ReadOnly
Run Code Online (Sandbox Code Playgroud)

现在我能做到

PS> vs

PS> vsa

PS> vs .\Solution.sln

PS> vsa .\Solution.sln
Run Code Online (Sandbox Code Playgroud)

一切都按预期工作,但它不如我想的那么好.也许我正在考虑它,但我想知道是否有办法创建一个更好的功能,并通过传递不同的参数使用它的两个别名.

提前致谢!

powershell

3
推荐指数
1
解决办法
41
查看次数

PowerShell - 将属性名称从Pascal案例转换为具有下划线的大写字母

假设我有一个这样的对象:

$test = @{
          ThisIsTheFirstColumn = "ValueInFirstColumn"; 
          ThisIsTheSecondColumn = "ValueInSecondColumn"
         }     
Run Code Online (Sandbox Code Playgroud)

我想最终得到:

$test = @{
          THIS_IS_THE_FIRST_COLUMN = "ValueInFirstColumn"; 
          THIS_IS_THE_SECOND_COLUMN = "ValueInSecondColumn"
         } 
Run Code Online (Sandbox Code Playgroud)

无需手动编写新列名称.

这向我展示了我想要的价值观:

$test.PsObject.Properties | where-object { $_.Name -eq "Keys" } | select -expand value | foreach{ ($_.substring(0,1).toupper() + $_.substring(1) -creplace '[^\p{Ll}\s]', '_$&').Trim("_").ToUpper()} | Out-Host
Run Code Online (Sandbox Code Playgroud)

这导致:

THIS_IS_THE_FIRST_COLUMN
THIS_IS_THE_SECOND_COLUMN
Run Code Online (Sandbox Code Playgroud)

但现在我似乎无法弄清楚如何将这些新值分配回对象.

powershell powershell-4.0

3
推荐指数
1
解决办法
220
查看次数

在Azure DevOps的发布管道中的作业代理的两个任务之间共享PowerShell变量

我在Azure DevOps的发布管道中创建了两个PowerShell任务。第一个任务包含一个内联PowerShell脚本,如下所示:

$ciVersion = "v2.1"
Write-Host $ciVersion
Run Code Online (Sandbox Code Playgroud)

在第二个PowerShell任务中,我只想读取在第一个PowerShell任务中声明的变量。

Write-Host $ciVersion
Run Code Online (Sandbox Code Playgroud)

运行发行过程后,控制台v2.1在第一个任务的控制台窗口中显示给我,但在第二个任务的控制台窗口中什么都不显示。我也尝试过声明一个环境变量并在任务中修改它的值,但是这对我也不起作用。有任何想法吗?干杯

powershell azure-devops

3
推荐指数
1
解决办法
231
查看次数

有没有简单的方法在Powershell中调用"AD Object Picker"?

我正在编写一个Powershell 5.0脚本,其中包含一个用户可以选择AD对象的表单.

单击按钮后,我需要使用"AD Object Picker"对话框显示它们(我不确定它是否被称为),例如:

在此输入图像描述

...并将所选对象存储在变量中.

我不确定如何在Powershell中正确调用此对话框,因为我甚至不知道它的正式命名(这使得研究变得困难),更不用说是否有.NET控件或包装器.

你能告诉我一个调用这个对话框的方法(实际上调用了什么东西)?

.net powershell active-directory

3
推荐指数
1
解决办法
212
查看次数

Powershell在String中搜索并提取字符串的特定值

我有一个包含许多行的大文件.例如:

ts=2019-01-16 network=1.1.1.1 system=irgendwas pid=100 bugReq=dasf something=else maybe=this
Run Code Online (Sandbox Code Playgroud)

从每一行我想提取以下信息:

ts=,system= & something=,但=总是改变后的价值观.

我试过这个,但无法让它起作用:

$found = $string -match '.*system="(\d+)".*' if ($found) { $system= $matches[1]}
Run Code Online (Sandbox Code Playgroud)

regex string powershell

3
推荐指数
1
解决办法
232
查看次数

Powershell Count行非常大的文件

我有一个非常大的文本文件,大小为250 GB,由供应商提供给我们.它们还为我们提供了一个控制文件,该文件应该包含大文件中的行数.有时会出现不匹配的情况.我如何计算Powershell中的线条?我尝试了这个命令,它运行了半个多小时,但还没有完成.

Get-content C:\test.txt | Measure-Object –Line

(gc C:\test.txt | Measure-object | select count).count
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏谢谢MR

powershell performance

3
推荐指数
3
解决办法
488
查看次数

Powershell中的缩短功能

当我开始进行大面积ping请求时,我首先开始在excel工作表中“写下”所有IP地址,以获取csv输出以将其导入到powershell中。

然后,我想到了通过更少的工作来使Powershell中的这些事情变得更加容易。

$range = 1..254

$ips1 = ForEach-Object -Process {'10.10.1.' + $Range}
$ips2 = ForEach-Object -Process {'10.10.2.' + $Range}
$ips3 = ForEach-Object -Process {'10.10.3.' + $Range}
$ips4 = ForEach-Object -Process {'10.10.4.' + $Range}
$ips5 = ForEach-Object -Process {'10.10.5.' + $Range}
$ips6 = ForEach-Object -Process {'10.10.6.' + $Range}
$ips7 = ForEach-Object -Process {'10.10.7.' + $Range}
$ips8 = ForEach-Object -Process {'10.10.8.' + $Range}
$ips9 = ForEach-Object -Process {'10.10.9.' + $Range}
$ips10 = ForEach-Object -Process {'10.10.10.' + $Range}
$ips11 = ForEach-Object -Process {'10.10.11.' …
Run Code Online (Sandbox Code Playgroud)

powershell

3
推荐指数
1
解决办法
48
查看次数

Powershell中的Write-Verbose与Write-Host

当我Write-Verbose在Powershell命令窗口中使用时,控制台中没有任何显示。但是,我团队中的devops工程师使用它进行持续集成,构建脚本。

Write-Verbose和之间有什么区别Write-Host

powershell

3
推荐指数
2
解决办法
1084
查看次数