我需要使用PowerShell修改现有的UTF8编码的JSON文件.我尝试使用以下代码:
$fileContent = ConvertFrom-Json "$(Get-Content $filePath -Encoding UTF8)"
$fileContent.someProperty = "someValue"
$fileContent | ConvertTo-Json -Depth 999 | Out-File $filePath
Run Code Online (Sandbox Code Playgroud)
这会将BOM添加到文件中,并以UTF16格式对其进行编码.是否可以拥有ConvertFrom-Json和ConvertTo-Json不执行编码/ BOM?
为什么.NET Framework ArrayList类.Add方法在PowerShell实现中不起作用?
除非我另外纠正,否则我认为我的故事的整体道德可能是:不要假设本机PowerShell方法将与.NET方法相同,并且在PowerShell中尝试.NET方法时要小心.
我正在寻找的原始解决方案是从一个函数返回一个日期列表,作为一个数组,用户定义的日期范围作为参数.然后引用日期数组来移动和读取以日期戳命名的文件.
我遇到的第一个问题是创建一个动态数组.我不知道我在做什么,并且错误地.Add在@()数组声明上调用.NET 方法.
使用"1"参数调用"Add"的异常:"Collection具有固定大小."
我认为我需要找到一个动态数组类型,当我真正的问题是我做得不对时.这让我走向了一个不同的方向,直到很久以后,我发现应该使用+=语法将对象添加到PowerShell数组中.
无论如何,在我回到如何正确使用PowerShell数组之前,我在其他一些切线上了.
然后我找到了.NET ArrayList类.好的.现在我有一个动态数组对象.我阅读了文档,其中说我应该使用该.Add方法向集合中添加元素.
然后开始寻求更深入的理解,因为我经历了几天头脑紧张的挫折试图解决问题.
我制作了一个最初工作的实现.它产生了一个日期范围 - 但它也产生了一些奇怪的行为.我观察到奇怪的日期返回,例如:
0001年1月1日星期一12:00:00 AM
事实证明,我发现这是你执行此操作时获得的结果:
Get-Date 0
Run Code Online (Sandbox Code Playgroud)
该ArrayList被返回时,首先,索引值的数组元素的列表,然后将数组值.这根本没有任何意义.我开始探索我是否正确地调用函数,我是否遇到某种可变范围问题,或者我是否只是疯了.
我现在相当确信我的挫败感是由于缺乏一个可靠的初学者参考,不仅仅展示了如何进行简单数组实现的几个例子,而是用其他解决方案描述了一些注意事项.
让我在这里解释一下,实现数组/集合的三种方法,以及我试图生成的解决方案 - 即日期范围内的日期列表.
出于某种原因,我最初认为在Powershell中向.NET ArrayList添加元素的正确方法是使用.Add方法.它的记载.我仍然不明白为什么这不起作用(严重 - 有人请赐教).通过实验,我发现通过使用+=将对象添加到ArrayList 的方法,我可以获得准确的结果.
不要这样做.这绝对是错的.它会产生我上面描述的错误:
Function Get-DateRangeList {
[cmdletbinding()]
Param (
[datetime] $startDate,
[datetime] $endDate
)
$datesArray = [System.Collections.ArrayList]@() # Second method
for ($d = $startDate; $d -le $endDate; $d = $d.AddDays(1)) {
if ($d.DayOfWeek -ne 'Sunday') { …Run Code Online (Sandbox Code Playgroud) 我正在努力使用PowerShell读取XML文件.
主要思想是 - 我有一个标志,表示是否读取或写入配置文件.如果需要编写文件 - 脚本将获取参数并将其保存到XML文件中.如果该标志设置为"read" - PowerShell脚本不带参数,但它应该从保存的XML中获取它们.
问题是从XML读取.我使用代码写入XML:
$Config = @{}
$Config.parameter1 = $parameterValue
$Config.dictionaryWithArray = $dictWithArray
($Config | ConvertTo-XML).Save("$path")
Run Code Online (Sandbox Code Playgroud)
保存的XML文件结果为:
<?xml version="1.0"?>
<Objects>
<Object Type="System.Collections.Hashtable">
<Property Name="Key" Type="System.String">parameter1</Property>
<Property Name="Value" Type="System.String">value_in_variable_parameter_Value</Property>
<Property Name="Key" Type="System.String">dictionaryWithArray</Property>
<Property Name="Value" Type="System.Collections.Generic.Dictionary`2[System.String,System.String[]]">
<Property Name="Key" Type="System.String">key1</Property>
<Property Name="Value" Type="System.String[]">
<Property Type="System.String">subkey1</Property>
<Property Type="System.String">subvalue1</Property>
</Property>
<Property Name="Key" Type="System.String">key2</Property>
<Property Name="Value" Type="System.String[]">
<Property Type="System.String">subkey2</Property>
<Property Type="System.String">subvalue2</Property>
</Property>
... and so on
Run Code Online (Sandbox Code Playgroud)
我尝试使用诸如http://blogs.technet.com/b/heyscriptingguy/archive/2012/09/13/powertip-use-powershell-to-easily-read-an-xml-document等方法来阅读此配置. aspx,但没有运气.
请问,请问如何读取配置文件,用这种方法编写?或者可能有更好的方法在写入之前序列化配置,所以它会更容易阅读吗?
我有这个csv文件我正在尝试删除前导零,但是我的代码中包含任何带有字母或特殊字符的数字,它会使值无效,我不希望...
column1 ,column2 ,column3 ,column4 ,column5,column6,column7,column8 ,column9,column10,column11,column12,column13 ,column14,column15,column16
0000013 , ,ShopKeep ,Store , , , , , ,1034627 , , , , , ,
0000032 , ,Test411 ,User411 ,0007643, , , , ,1034632 , , , , , ,
0000142 , ,Pinky ,Brain ,0571253, , , ,J ,1234 , , , , , ,
0000145 ,Leo@ninjaturtles.com ,Leo ,Turtle ,0661203, , , ,J ,2345 , , ,RNixon , , ,
0000264 , ,Richard ,Nixon ,0311247, , , ,J ,3456 , , …Run Code Online (Sandbox Code Playgroud) 我试图找到为可以自动完成的参数创建值的方法,或者使用tab显示所有可用选项,例如cmdlet中的值"allsigned"
Set-ExecutionPolicy -ExecutionPolicy AllSigned
Run Code Online (Sandbox Code Playgroud)
¿有关如何在程序中调用它的任何想法或我如何实现这一点?
在gvim中,当我想执行类似的操作时,:!ls它会将此命令发送到常规命令提示符cmd.exe.所以,我不能使用ls或者pwd任何常规的linux命令.但是,PowerShell支持大多数Linux类型命令.那么,我如何将PowerShell设置为vim的默认shell?
我正在尝试将SQL适配器对象传递给powershell函数,但我收到此错误:
executeQueryAndFillTable:无法处理参数'da'上的参数转换.无法将"System.Object []"类型的"System.Object []"值转换为"System.Data.SqlClient.SqlDataAdapter"类型.
这是代码
function sql_pull
{
# define Objects
$xmlDoc = New-Object System.Xml.XmlDocument
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
$sqlCommand = New-Object System.Data.SqlClient.SqlCommand
$sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$connectionString = "Password=$password;Persist Security Info=True;User ID=$userId;Data Source=$dataSource"
$counter = 0
# database queries
$queries = @(
"Select * from sys.configurations for xml Raw ('Cretiria'), type, ROOT('sys.configurations'), ELEMENTS");
$sqlConnection.ConnectionString = $connectionString
$sqlCommand.Connection = $sqlConnection
try {
$sqlConnection.Open()
foreach($q in $queries)
{
$sqlCommand.CommandText = $q
$sqlAdapter.SelectCommand = $sqlCommand.CommandText
$sqlAdapter.SelectCommand.CommandTimeout = 300
$res = executeQueryAndFillTable($sqlAdapter, $sqlCommand) …Run Code Online (Sandbox Code Playgroud) 我有这种格式的XML文档
<rss>
<channel>
<item>
<id>1</id>
<image>img_32.jpeg</image>
</item>
<item>
<id>2</id>
<image>img_42.jpeg</image>
</item>
<item>
<id>1</id>
<image>img_52.jpeg</image>
</item>
<item>
<id>3</id>
<image>img_62.jpeg</image>
</item>
<item>
<id>4</id>
<image>img_72.jpeg</image>
</item>
</channel>
</rss>
Run Code Online (Sandbox Code Playgroud)
ID节点不是唯一的.我想使用PowerShell做的是按ID分组,然后遍历每个ID的分组项.
ID 1 has two images
loop through the two images
do something with it
ID 2 has one image
loop through the one image
do something with it
etc..
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有以下内容:
[xml]$xml = (New-Object System.Net.WebClient).DownloadString("https://myfeedurl.xml")
$grouped = $xml.rss.channel.item | Group id
$grouped
Run Code Online (Sandbox Code Playgroud)
哪个回报
Count Name Group
----- ---- -----
2 1 {item}{item}
1 2 {item} … 我想在许多文件中查找并替换某个字符串.其中一些文件可能比较大,所以我使用的StreamReader是System.IO命名空间中的类.
我遇到的问题是我不想将新值写入新文件(这是我目前所拥有的).我想只是"更新"当前文件.
$currentValue = "B";
$newValue = "A";
# Loop through all of the directories and update with new details.
foreach ($file in $Directories) {
$streamReader = New-Object System.IO.StreamReader -Arg "$file"
$streamWriter = [System.IO.StreamWriter] "$file"
# Switching $streamWriter to the below works.
# $streamWriter = [System.IO.StreamWriter] "C:\Temp\newFile.txt"
while($line = $streamReader.ReadLine()){
# Write-Output "Current line value is $line";
$s = $line -replace "$currentValue", "$newValue"
# Write-Output "The new line value is $s"
$streamWriter.WriteLine($s);
}
# Close the …Run Code Online (Sandbox Code Playgroud) 如何执行 Azure Powershell 命令 New-AzWebApp 以选择 .NET Core 作为堆栈?我看不到任何可以提供帮助的参数和文档。 https://docs.microsoft.com/en-us/powershell/module/az.websites/New-AzWebApp?view=azps-2.6.0
powershell ×10
xml ×2
arraylist ×1
arrays ×1
azure ×1
c# ×1
generic-list ×1
parameters ×1
unicode ×1
vi ×1
vim ×1
xml-parsing ×1