member-enumeration

Get-ChildItem.Length is Wrong

微笑、不失礼 提交于 2019-12-17 09:56:29
问题 I am writing a recursive function that goes through a directory and copies every file and folder in it. The first check I have in the function is to see if the path passed in has children. To find this out, I use the following method: [array]$arrExclude = @("Extras") Function USBCopy { Param ([string]$strPath, [string]$strDestinationPath) try { $pathChildren = Get-ChildItem -Path $strPath if($pathChildren.Length -gt 0) { foreach($child in $pathChildren) { if($arrExclude -notcontains $child) {

How to add hashtable to multidimensional array? Cannot assign values via member enumeration

无人久伴 提交于 2019-11-28 06:03:51
问题 I'm having trouble with adding hashtables to a multidimensional array. I coded the following: $Data = @{BIBs = @( @{$BIB = @{BIBName=$BIBName}, @{Standort = $Standort}, @{Bücher = @( @{BuchName = $BuchName; Autor = $Autor }) }} )} This code is functioning and creates an output, which I store in a JSON: { "BIBs": [ { "BIB1": [ { "BIBName": "123" }, { "Standort": "123" }, { "Bücher": [ { "Autor": "123", "BuchName": "123" } ] } ] }, { "BIB2": [ { "BIBname": "345" }, { "Standort": "345" }, {

Get-ChildItem.Length is Wrong

a 夏天 提交于 2019-11-27 09:21:47
I am writing a recursive function that goes through a directory and copies every file and folder in it. The first check I have in the function is to see if the path passed in has children. To find this out, I use the following method: [array]$arrExclude = @("Extras") Function USBCopy { Param ([string]$strPath, [string]$strDestinationPath) try { $pathChildren = Get-ChildItem -Path $strPath if($pathChildren.Length -gt 0) { foreach($child in $pathChildren) { if($arrExclude -notcontains $child) { $strPathChild = "$strPath\$child" $strDestinationPathChild = "$strDestinationPath\$child" Copy-Item

Select the values of one property on all objects of an array in PowerShell

只愿长相守 提交于 2019-11-26 11:19:45
Let's say we have an array of objects $objects. Let's say these objects have a "Name" property. This is what I want to do $results = @() $objects | %{ $results += $_.Name } This works, but can it be done in a better way? If I do something like: $results = objects | select Name $results is an array of objects having a Name property. I want $results to contain an array of Names. Is there a better way? Scott Saad I think you might be able to use the ExpandProperty parameter of Select-Object . For example, to get the list of the current directory and just have the Name property displayed, one

Powershell: null file always generated (output of Compare-Object)

旧时模样 提交于 2019-11-26 02:11:22
The most popular answer for this question involves the following Windows powershell code (edited to fix a bug): $file1 = Get-Content C:\temp\file1.txt $file2 = Get-Content C:\temp\file2.txt $Diff = Compare-Object $File1 $File2 $LeftSide = ($Diff | Where-Object {$_.SideIndicator -eq '<='}).InputObject $LeftSide | Set-Content C:\temp\file3.txt I always get a zero byte file as the output, even if I remove the $Diff line. Why is the output file always null, and how can it be fixed? PetSerAl , as he routinely does, has provided the crucial pointer in a comment on the question (with no intent of

Select the values of one property on all objects of an array in PowerShell

ぐ巨炮叔叔 提交于 2019-11-26 01:59:02
问题 Let\'s say we have an array of objects $objects. Let\'s say these objects have a \"Name\" property. This is what I want to do $results = @() $objects | %{ $results += $_.Name } This works, but can it be done in a better way? If I do something like: $results = objects | select Name $results is an array of objects having a Name property. I want $results to contain an array of Names. Is there a better way? 回答1: I think you might be able to use the ExpandProperty parameter of Select-Object . For

Powershell: null file always generated (output of Compare-Object)

梦想与她 提交于 2019-11-26 01:38:14
问题 The most popular answer for this question involves the following Windows powershell code (edited to fix a bug): $file1 = Get-Content C:\\temp\\file1.txt $file2 = Get-Content C:\\temp\\file2.txt $Diff = Compare-Object $File1 $File2 $LeftSide = ($Diff | Where-Object {$_.SideIndicator -eq \'<=\'}).InputObject $LeftSide | Set-Content C:\\temp\\file3.txt I always get a zero byte file as the output, even if I remove the $Diff line. Why is the output file always null, and how can it be fixed? 回答1: