小编Gre*_*SAT的帖子

添加XML子元素

使用PowerShell,我想在XML树中添加几个子元素.
我知道添加一个元素,我知道添加一个或几个属性,但我不明白如何添加几个元素.

一种方法是对子级来写一个子XML树作为文本
但是因为元素没有立即加入,我不能用这种方法.

要添加一个元素,我这样做:

[xml]$xml = get-content $nomfichier
$newEl = $xml.CreateElement('my_element')
[void]$xml.root.AppendChild($newEl)
Run Code Online (Sandbox Code Playgroud)

工作良好.这给了我这个XML树:

$xml | fc
class XmlDocument
{
  root =
    class XmlElement
    {
      datas =
        class XmlElement
        {
          array1 =
            [
              value1
              value2
              value3
            ]
        }
      my_element =     <-- the element I just added
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想在'my_element'中添加一个子元素.我使用类似的方法:

$anotherEl = $xml.CreateElement('my_sub_element')
[void]$xml.root.my_element.AppendChild($anotherEl) <-- error because $xml.root.my_element is a string
[void]$newEl.AppendChild($anotherEl)               <-- ok
$again = $xml.CreateElement('another_one')
[void]$newEl.AppendChild($again)
Run Code Online (Sandbox Code Playgroud)

这给了这个XML树(部分显示):

my_element =
  class XmlElement
  {
    my_sub_element =
    another_one =
  }
Run Code Online (Sandbox Code Playgroud)

这些是属性,而不是子元素. …

xml powershell

25
推荐指数
3
解决办法
6万
查看次数

使用StreamWriter对象后,为什么我的文本文件不完整?

我写了一个小程序,生成大文本文件.我发现使用StreamWriter比我知道的其他方法快得多.但是每个文本文件的末尾都缺失了.

我将程序缩减为一个非常简单的片段来发现问题,但我仍然无法理解如何解决它.

#$stream = [System.IO.StreamWriter] "test.txt"
# also tested with  $stream = New-Object System.IO.StreamWriter("test.txt")

$i = 1
while($i -le 500) {
    $stream.WriteLine("$i xxxxxx")
    $i++
}
$stream.flush        # flushing don't change anything
$stream.close        # also tested with   $stream.dispose

exit 0
Run Code Online (Sandbox Code Playgroud)

问题1:
文件末尾丢失.根据线长度,最后一行约为495,通常在线的中间切割.

问题2:
程序完成后,文本文件仍然被锁定(我们可以读取它,但不能删除/重命名).我们必须退出PowerShell才能获得对该文件的完全访问权限.

在Windows 2003和Windows 2008上测试,结果完全相同.

编辑
dugas发现了问题:我忘记了一些括号.哪个解决了我的代码片段显示的问题.
但我的原始程序有括号.所以我将这个问题标记为已解决,并且当我为这个特定问题找到更好的代码片段时,我会打开一个新问题.

编辑2
知道了.我有一个隐藏的例外.非常感谢 !

powershell streamwriter

6
推荐指数
1
解决办法
7497
查看次数

Powershell:如何从多维数组中获取单列?

是否有函数、方法或语言结构允许从 Powershell 中的多维数组中检索单列?

$my_array = @()
$my_array += ,@(1,2,3)
$my_array += ,@(4,5,6)
$my_array += ,@(7,8,9)

# I currently use that, and I want to find a better way:
foreach ($line in $my_array) {
    [array]$single_column += $line[1]    # fetch column 1
}
# now $single_column contains only 2 and 5 and 8
Run Code Online (Sandbox Code Playgroud)

我的最终目标是从一列中找到不重复的值。

powershell multidimensional-array

4
推荐指数
1
解决办法
2万
查看次数

功能访问单元格范围

我没有找到如何使用函数的单元格范围.

我徒劳地搜索了一些例子.

我写了以下测试.我在两个"for"行中得到"Object variable not set"错误(一个没有"RangeAddress",第二个是它,因为我不确定正确的语法):

function CHECKBZRANGE(cellRange) as integer
    dim nCol as integer
    dim nLine as integer
    dim i as integer

    for nCol = cellRange.StartColumn to cellRange.EndColumn
        for nLine = cellRange.RangeAddress.StartRow to cellRange.RangeAddress.EndRow
            i = i + 1      ' placeholder for some computation
        next nLine
    next nCol
    checkBZ_range = i
end function
Run Code Online (Sandbox Code Playgroud)

使用像这样的单元格调用此函数 =CHECKBZRANGE(A6:C9)

有人可以解释如何使用参数传递的单元格区域吗?

libreoffice-basic libreoffice-calc

4
推荐指数
1
解决办法
2461
查看次数

Dot采购不以.ps1结尾的PowerShell脚本

从PowerShell程序,我可以"点源"另一个PowerShell程序.即我可以执行它,就像它写在第一个内部一样.
例:

Write-Host 'before'
. MyOtherProgram.ps1
Write-Host 'after'
Run Code Online (Sandbox Code Playgroud)

主程序中"包含"的MyOtherProgram,就像其内容已被复制/粘贴一样.

问题是:我只能点源文件名完成,.ps1
我不能用MyOtherProgram.libMyOtherProgram.whatever

任何人都有一种方法来点源PowerShell脚本而不是以.ps1?结尾?

powershell

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

如何退出PowerShell中的try-catch块?

我想从try块内退出:

function myfunc
{
   try {
      # Some things
      if(condition) { 'I want to go to the end of the function' }
      # Some other things
   }
   catch {
      'Whoop!'
   }

   # Other statements here
   return $whatever
}
Run Code Online (Sandbox Code Playgroud)

我用a测试过break,但这不起作用.如果任何调用代码在循环内,它会打破上层循环.

powershell try-catch

0
推荐指数
1
解决办法
1万
查看次数